James
James

Reputation: 63

Downloading in real-time 1000s of Telegram chats with Python Telethon?

I have set up a script to download messages from thousands of Telegram supergroups (named chatlist). This works fine when I use a few groups, however, allowing 1000< groups seems to break it and no messages are collected at all. Is this the correct way to approach it, or is there a more efficient way? I don't think it's a network issue my end as I have GB internet

from telethon import TelegramClient, events, sync

client = TelegramClient('test', api_id, api_hash)


@client.on(events.NewMessage(chats=chatlist))
async def my_event_handler(event):
    
    message = event.message.to_dict()
    print(message['message'])
    
await client.start()
await client.run_until_disconnected()

Upvotes: 1

Views: 548

Answers (1)

Nicky Harpor
Nicky Harpor

Reputation: 46

A simple workaround is to remove the NewMessage event filter i.e.:

@client.on(events.NewMessage())

and filter messages inside the method yourself:

async def my_event_handler(event):
    message = event.message.to_dict()
    input_chat = await event.get_input_chat()
    if input_chat in chatlist:
        print(message['message'])

You didn't mention what's inside chatlist, so I assumed it's a list of InputChat objects.

Note that if your chat list is a list of @username strings, you'll soon reach Telegram username resolve limits. Always have InputChat or long ids in the chatlist.

Upvotes: 1

Related Questions