atipugin / telegram-bot-ruby

Ruby wrapper for Telegram's Bot API
https://core.telegram.org/bots/api
Do What The F*ck You Want To Public License
1.35k stars 217 forks source link

Reaction emoji of old message interpreted as new message #308

Closed TheNeedful-DO closed 2 months ago

TheNeedful-DO commented 2 months ago

When a user reacts to an old message the bot interprets this as an incoming message from the original sender.

This occurs in chatrooms but not in direct message.

Reacting to recent messages does not trigger the behavior. I don't know how old the message must be. At least hours or days old maybe.

To reproduce:

In a chatroom, react to an old bot command message (i.e Thumbs Up 👍)

Screenshot shows reacting to an old bot command message and the bot logs the action as an "Incoming message"

telegram bot reaction bug

TheNeedful-DO commented 2 months ago

I think I see a solution to my problem.

There exists a variable allowed_updates

Including this variable when instantiating the bot should allow the message_reaction event to be properly parsed instead of as an edited_message event.

My code now looks like this:

allowed_updates = ["message", 
  "edited_message",
  "message_reaction",
  "inline_query",
  "chosen_inline_result",
  "callback_query"]

Telegram::Bot::Client.run(token, allowed_updates: allowed_updates, logger: Logger.new($stderr)) do |bot|

Telegram API list of allowed_update types: https://core.telegram.org/bots/api#getupdates

I will continue working on this tomorrow and update my results. I think it's going to work 🤞 and if so I will close this issue.

TheNeedful-DO commented 2 months ago

Here is my solution.

  1. Detect if an update is a MessageReaction.
  2. Skip it.
bot.listen do |message|
  # ignore reactions
  next if bot.api.getUpdates.map{|update| !update.message_reaction.nil?}.any?

This code works by scanning the message for a reaction which consists of two update events. The method bot.api.getUpdates fetches the current update(s). If any of the updates is a Message Reaction, the message is skipped by way of the next method.

Picture shows log of the two parts to a Message Reaction Update Screenshot_2024-06-22-08-41-01-770

My solution feels a little sloppy but it seems good enough for now.

TheNeedful-DO commented 2 months ago

Previous solution is unreliable.

Now skipping all message edits. This is non-ideal but the only solution I see at this time.

next unless bot.api.getUpdates[0].edited_message.nil?