Skip to content

Instantly share code, notes, and snippets.

@md-farhan-memon
Last active March 18, 2023 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save md-farhan-memon/4ff51b81cba44bffce48f39ad3c0540f to your computer and use it in GitHub Desktop.
Save md-farhan-memon/4ff51b81cba44bffce48f39ad3c0540f to your computer and use it in GitHub Desktop.
Script to listen to Telegram messages as a user, filter by keywords and push it to slack or do whatever you need. Using Telethon.
from telethon import TelegramClient, events, sync
import re, requests
# Remember to use your own values from my.telegram.org!
api_id = '...'
api_hash = '...'
username = '...'
channel_name = '...'
slack_webhook_url = '...'
client = TelegramClient(username, api_id, api_hash)
@client.on(events.NewMessage(chats=channel_name))
async def my_event_handler(event):
text = event.raw_text
if re.search("(any|keyword|to|filter)", text, re.I):
print(text)
obj = {'text': "https://t.me/" + channel_name + "/" + str(event.message.id)}
requests.post(slack_webhook_url, json = obj)
print('slacked!')
client.start()
client.run_until_disconnected()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment