Skip to content Skip to sidebar Skip to footer

Telethon Python - Store Incoming Message In File And Read Back

I'm using Telethon for a simple python script able to manage the telegram icoming message for a user and to store them inside a file. Then I would like to scroll the file and send

Solution 1:

its been 10 months but i just readed :) im using this code:

from datetime import datetime
import time
import requests
import configparser
import json
import re
from telethon.errors import SessionPasswordNeededError
from telethon import TelegramClient, events, sync
from telethon.tl.functions.messages import (GetHistoryRequest)
from telethon.tl.types import (
PeerChannel
)

api_id = Yourapiid
api_hash = 'yourhash'
client = TelegramClient('name',api_id,api_hash)

vip=[-236123612,-100236123612] # some id of channels (i typed radnom numbers here)@client.on(events.NewMessage(outgoing=False)) # all incoming messagesasyncdefmy_event_handler(event):  
    newMessage = event.message.message # this is only a telegram message content, its what u see on telegram 
    FullMessage = event.message # this is full message event, with technical things undergoingm like some True, False, etc, and with buttons if any.if event.message.sender_id in vip:
        time = datetime.now().strftime("%d-%m-%Y %H:%M:%S") 
        printRAW1("====================================== " + time)
        printRAW1(newMessage)
with client:
    client.run_until_disconnected()

i use function like this to print me messages from telegram to a file, one under another:

`defprintRAW1(*Text):
    RAWOut = open('file.txt', 'a', encoding='utf8', closefd=True)
    print(*Text, file=RAWOut)
    RAWOut.flush()
    RAWOut.close()  `

mayby that would be helpful for someone. i run it in anaconda prompt, using comand "activate" before python script, for functions to work propertly.

Post a Comment for "Telethon Python - Store Incoming Message In File And Read Back"