node-telegram-bot-api icon indicating copy to clipboard operation
node-telegram-bot-api copied to clipboard

Updates processed if the script crashed

Open timothe opened this issue 7 years ago • 5 comments

Hi,

I'm very happy with this package and kudos to the team to maintain it. Although I am encountering an issue, and I looked for a solution for a long time, but couldn't find someone pointing that out. Sorry if it's a duplicate.

I integrated the latest version of the library, and execute some code in reply of some messages. My script (integrating more than the lib), or my VM sometimes crashes, and when I start it again, the bot processes the commands I have sent to it in the meantime.

Example:

teleMessenger.onText(/stats/iu, msg => {
  getStats(); // <= this gets executed if I send 'stats' to the bot while the script is stopped
});

So my question is:

  • is there a way to ignore the messages sent before the script starts,
  • or clear the queue?

Thanks

timothe avatar Nov 16 '18 09:11 timothe

Shortly: nope. Telegram says this:

Incoming updates are stored on the server until the bot receives them either way, but they will not be kept longer than 24 hours.

alexandercerutti avatar Nov 16 '18 10:11 alexandercerutti

Just found this: https://stackoverflow.com/questions/31707256/prevent-getting-old-updates-from-telegram-bot-api-using-a-web-hook

How can I integrate this solution with the module?

timothe avatar Nov 16 '18 10:11 timothe

That might be a good solution. You'd have to set the start up timestamp (in milliseconds) and set last received message timestamp in the same file (you can fetch from Message.date) on something static (text file/JSON file or database). Then check on new message if the startup time is >= last message, and if the condition is true, you ignore the message.

alexandercerutti avatar Nov 16 '18 10:11 alexandercerutti

It was so bloody simple... I wonder why I didn't think of it earlier:

teleMessenger.onText(/stats/iu, msg => {
  if (Date.now() - msg.date*1000 < 1000) { // Check if the message is not old 
    getStats();
  }
});

Wonder if I might have race conditions here, in which case I would increase the number to more than 1 second. So far it works!

Thanks for the quick reply ;)

timothe avatar Nov 16 '18 10:11 timothe

Updates processed if the script crashed

This is the default behavior when polling. Any update which is not processed is stored on Telegrams side for around 24 hrs. You can use the negative offset to get the last item in the update queue and the rest of the updates will be forgotten. Here is a detailed description from the official Telegram docs:

Identifier of the first update to be returned. Must be greater by one than the highest among the identifiers of previously received updates. By default, updates starting with the earliest unconfirmed update are returned. An update is considered confirmed as soon as getUpdates is called with an offset higher than its update_id. The negative offset can be specified to retrieve updates starting from -offset update upto the end of the updates queue. All previous updates will forgotten.

To do this with ntba you will have to switch to long polling with the getUpdates method.

I have never tested this approach, but it may work theoretically.

kamikazechaser avatar Dec 07 '18 16:12 kamikazechaser