Using Telethon to read messages from a particular group

43 Views Asked by At

I am very new to Telethon and does not really know how it works or how to make it work. I have tried reading the documentations but do not understand most part of it.

I'm trying to print out messages from a particular group chat in Telegram, but when I run my piece of code, there is no output at all.

I am also not very sure on what to input for the parameters of TelegramClient and iter_messages.

Please help me out, much appreciated!

from telethon.sync import TelegramClient

api_id = '123'
api_hash = '123213'

with TelegramClient('name', api_id, api_hash) as client:
    for message in client.iter_messages('me'):
        print(message.sender_id, ':', message.text)
1

There are 1 best solutions below

1
0stone0 On

The me in client.iter_messages('me') gets the 'Saved Messages' group, you need to place a group-name there.

For example, to show all the messages in the FOOBAR group, I'd do:

from telethon import TelegramClient
import asyncio

client = TelegramClient('anon', '1234567', 'e40ef8.......')
client.start()

async def main():
    async for message in client.iter_messages('FOOBAR', limit=10, reverse=False):
        print(message.id, message.text)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())