Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get message from Telegram groups by API - Python

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

like image 949
RedVelvet Avatar asked May 24 '19 15:05

RedVelvet


People also ask

How can I get Telegram API messages?

Follow the link to your bot by clicking the link that looks like t.me/{yourBotUsername}. This is where you will receive messages. Go ahead and send a message to your bot. To prevent spam, bots cannot initiate a chat, so we need to send it a message to enable the bot to message us back.

How can I get Telegram group data?

c) On the desktop app's homescreen, click on the three horizontal lines on the top left corner to open the app menu. Once the menu pops up, select "Settings". d) On the screen that shows the 'Settings' options, scroll down to the 'Privacy and Security' option, then select 'Export Telegram Data'.


1 Answers

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()
like image 57
Martin Olivari Avatar answered Sep 18 '22 13:09

Martin Olivari