go-telegram-bot-api / telegram-bot-api

Golang bindings for the Telegram Bot API
https://go-telegram-bot-api.dev
MIT License
5.67k stars 875 forks source link

Bot is unable to tackle update ID randomization process after 1 week of bot inactivity #156

Open milanaleksic opened 6 years ago

milanaleksic commented 6 years ago

Hi,

I think I have encountered a bug in the getUpdates handler. What happens is following: if my bot doesn't receive a message for a longer period of time, then it breaks on a first message by going into endless loop.

Behavior

Here is the app log (with debug turned on):

# this is happily repeated over and over and over
2018/03/20 14:33:34 getUpdates req : map[offset:[727334708] timeout:[60]]
2018/03/20 14:33:34 getUpdates resp: []
2018/03/20 14:34:25 getUpdates resp: {"ok":true,"result":[]}
...
# BUT!
2018/03/20 14:37:18 getUpdates req : map[timeout:[60] offset:[727334708]]
2018/03/20 14:37:18 getUpdates resp: [{UpdateID:117314199 Message:0x10a89540 EditedMessage:<nil> ChannelPost:<nil> EditedChannelPost:<nil> InlineQuery:<nil> ChosenInlineResult:<nil> CallbackQuery:<nil> ShippingQuery:<nil> PreCheckoutQuery:<nil>}]
2018/03/20 14:37:18 getUpdates resp: {"ok":true,"result":[{"update_id":117314199,
"message":{"message_id":64,"from":{"id":protected,"is_bot":false,"first_name":"protected","last_name":"protected","username":"protected","language_code":"en-BE"},"chat":{"id":protected,"first_name":"protected","last_name":"protected","username":"protected","type":"private"},"date":1521553038,"text":"protected"}}]}

Only after the restart is this message processed and new messages can come in.

Cause

Based on this text from page https://core.telegram.org/bots/api I believe this is the cause (it's beside the update_id)

The update‘s unique identifier. Update identifiers start from a certain positive number and increase sequentially. This ID becomes especially handy if you’re using Webhooks, since it allows you to ignore repeated updates or to restore the correct update sequence, should they get out of order. If there are no new updates for at least a week, then identifier of the next update will be chosen randomly instead of sequentially.

So, as you can see the id of the update is not any more higher than the offset, but lower, thus a problem arises since the message is not sent at all to the update message handler

I think there are 2 bugs here in fact:

  1. endless loop could be avoided
  2. the offset value can be ignored and smaller value can come in
Syfaro commented 6 years ago

I don't know if it needs the ID check in the updates loop, has anyone ever seen their bot get the same update twice?

If that's not an issue, removing that should fix it.

variar commented 6 years ago

I've been trying to track down this issue for several months now. Reimplemented GetUpdatesChan in my own code without id check, everything seems to be working fine.

I've checked several other bot frameworks (https://github.com/tucnak/telebot/, https://github.com/olebedev/go-tgbot) and it looks like there is no id checking there.

ilyaglow commented 6 years ago

Any updates here? Is id check gonna be removed in master branch? It really bites.

Syfaro commented 5 years ago

Closed by https://github.com/go-telegram-bot-api/telegram-bot-api/commit/5f38203a155afa901c7deb70596a088a8424a9ee

lukaszraczylo commented 5 years ago

Skipping updates became way too annoying. If anyone have an idea how we can tackle the issue it'd be greatly appreciated.

teror4uks commented 5 years ago

what's a problem use != in if statement instead > ? I mean use in getUpdateChan func

if update.UpdateID != config.Offset {
    config.Offset = update.UpdateID
    ch <- update
}
HirbodBehnam commented 4 years ago

Hello Kinda late to the party. But once again, is there a problem with removing this if statement? Along side what @variar said, I checked the C# Library and Python and there is no sign of comparing the update ID.