0

I am a part of a public telegram channel with too much noise and only some meaningful important updates. I want to filter out noise and need to view real time updates(new messages) with specific words in text body of message of this channel using python.

I followed the code from below post by Lonami to try and get messages and it works beautifully, but it brings in all the historical messages as well.

Telegram get chat messages /posts - python Telethon

Could someone guide me to get the new messages real time one by one as they are posted ?

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
Mihirmt
  • 1
  • 1
  • Welcome to Stack Overflow. Please take a moment to read [help/on-topic] and [ask] for tips on how to improve your question so that it attracts the answers you are looking for. – Code-Apprentice Jul 29 '20 at 16:57
  • https://stackoverflow.com/questions/56295761/how-to-get-message-from-telegram-groups-by-api-python You can follow this question and just do a simple ```if your_wanted_word in message:``` statement to see if the new message meets your requirements... – Andrej Jul 29 '20 at 17:55
  • Hi @Andrej - thanks a lot this works well. the issue observed now is this code only retrieves new messages at set intervals- for instance i tried today and depending on the script execution time it only returns information at HH:MM:56 instead of realtime- can we reduce the polling period checking new messages for events.NewMessage ? i looked up but couldnt find much - also please answer this question so i can mark your answer as helpful instead of comment :) – Mihirmt Jul 30 '20 at 07:19
  • You can try using a ```while True:``` loop. And running the code inside of it. – Andrej Jul 30 '20 at 11:36
  • i tried but it got stuck in While true loop insteading of now sending any update at all: here is my code - can you show me how ? client = TelegramClient(username, api_id, api_hash) @client.on(events.NewMessage(chats='CHANNELNAME')) async def my_event_handler(event): now = datetime.now() current_time = now.strftime("%H:%M:%S") print("Current Time = ", current_time) print(event.raw_text) client.start() client.run_until_disconnected() – Mihirmt Jul 30 '20 at 12:10
  • Put your code in function, let's call it main. And then ```while True: sleep(5) # Gets new messages every 5 seconds main()``` – Andrej Jul 31 '20 at 13:05

0 Answers0