0

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 the message to someone else.

How can I do this?

In my opinion this issue is a type issue: when I write the file appending a new incoming message, I stringify the event message {Message}. When I read a file line I have no longer a {Message} struct but I read a String. How can I "parse" this String according to the {Message} format? I also tried with JSON but I failed!

<<Sorry, I'm a python noob, C rules!>>

  • I recommend you stick with something simple, like JSON, but store only what you need. That is, make your own dictionary with just the plain data you need and save that, then load it and use it as a dict. While `message.to_dict()` exists, there's no method to do the opposite. – Lonami Dec 05 '20 at 16:00

2 Answers2

0

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 messages
async def my_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:

`def printRAW1(*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.

ruskiem
  • 25
  • 6
0

You can use smth'g list like that. It can be id, name, or link.

chats= ['https://t.me/channel_name', 'channel_name','320323232']

@client.on(events.NewMessage(chats=chats))
async def message_handler(message):
    #do your stuff
thx4u
  • 1