How to get message from Telegram groups by API - Python

RedVelvet picture RedVelvet · May 24, 2019 · Viewed 7.5k times · Source

I was looking for some way in order to listen and catch new message provide by telegram gropus.

I have not found libraries or API in order to do this in python.

Someone have any suggestion?

Thank you,

RV

Answer

Martin Olivari picture Martin Olivari · Mar 9, 2020

Using Telethon

Replace channel_name with your telegram channel.

from telethon import TelegramClient, events, sync

# Remember to use your own values from my.telegram.org!
api_id = ...
api_hash = '...'
client = TelegramClient('anon', api_id, api_hash)

@client.on(events.NewMessage(chats='channel_name'))
async def my_event_handler(event):
    print(event.raw_text)

client.start()
client.run_until_disconnected()