Search code examples
telegramtelethon

How to monitor Telegram message reactions using Telethon?


I am trying to monitor new reactions to messages in a Telegram chat using the Telethon library. Ideally, I would like to receive notifications for these reactions, rather than continuously checking all messages in the chat.

I am currently able to handle new messages using the following example code:

@client.on(events.NewMessage())
async def handle_new_message(event):
    print(f"User {event.sender_id} sent message {event.message.id}: {event.message.text}")

with client:
    client.run_until_disconnected()

Is there an event or method in Telethon that would allow me to monitor message reactions in a similar way? Any suggestions or guidance would be greatly appreciated.

Thank you.


Solution

  • Telethon v1 does not offer a custom event for this, but you should be able to use UpdateMessageReactions directly:

    from telethon.tl.types import UpdateMessageReactions
    
    @client.on(events.Raw(UpdateMessageReactions))
    async def handler(event):
        print(event)
    

    Be sure to check the link to learn about the attributes it has to offer (or see the output from the code above to get a feel for it).

    Update: it seems Telegram may still send "update message edited" when reactions occur (as of Telethon v1.28). Unfortunately, you will probably need to handle both events (you may check the message's edit date to determine whether the edit means it was a message edit, or the amount of reactions has changed).