yagop / node-telegram-bot-api

Telegram Bot API for NodeJS
MIT License
8.3k stars 1.5k forks source link

how to use startPolling with offset option #957

Open akabab opened 2 years ago

akabab commented 2 years ago

Bug Report

I have read:

I am using the latest version of the library.

Expected Behavior

const bot = new TelegramBot(Env.TELEGRAM_BOT_TOKEN, { polling: false })

// Ignore all previous updates
const updates = await bot.getUpdates()
const pollingOffset = updates.pop()?.update_id + 1 || 0
const pollingOptions = { params: { offset: pollingOffset } }
bot.startPolling(pollingOptions)  

Expecting bot to ignore stacked updates and to start polling from new ones

Actual Behavior

Bot keeps receiving previous updates on starting, thoses sent when the script was not running

Question

Since I get the last update_id when script starts const bot = new TelegramBot(Env.TELEGRAM_BOT_TOKEN, { polling: { params: { offset: pollingOffset } } }) can't be used. Is it possible to startPolling manually with an offset param ? Thanks you

paquoc commented 7 months ago

It seems options.polling.params does not work as described in the documentation. You can use this method to customize offset parameter when sending a getUpdates request.

let originalGetUpdates = bot.getUpdates;
bot.getUpdates = function (form){
    form.offset = pollingOffset;
    return originalGetUpdates.call(bot, form);
}