How to stop Telegram Bot API from sending outdated messages when using webhooks?

I’m working on a Telegram bot and I’ve run into a problem. Every time I restart my bot and set up the webhook again, I get flooded with old messages. It’s really annoying!

Here’s what’s happening:

  1. I set up a webhook server to handle incoming updates
  2. When I shut down the bot, I remove the webhook
  3. But when I start it up again and set the webhook URL, I get tons of old messages

Is there a way to fix this without constantly checking /getUpdates until I catch up? I’ve tried looking through the docs but couldn’t find anything helpful.

Here’s a basic example of what my code looks like:

const express = require('express')
const axios = require('axios')
const app = express()
const token = 'my_secret_token'

// Set up webhook
axios.post(`https://api.telegram.org/bot${token}/setWebhook`, {
  url: 'https://mybot.com/updates'
})

// Handle updates
app.post('/updates', (req, res) => {
  console.log('Got an update!')
  res.sendStatus(200)
})

// Remove webhook on exit
process.on('SIGINT', () => {
  axios.post(`https://api.telegram.org/bot${token}/setWebhook`, {
    url: ''
  })
  process.exit()
})

app.listen(3000)

Any ideas on how to solve this? Thanks!

I’ve faced this issue too, and there’s a simple solution that doesn’t require changing your code much. When you set up the webhook, add the ‘drop_pending_updates’ parameter and set it to true. This tells Telegram to discard any pending updates when setting up the webhook.

Here’s how you can modify your webhook setup:

axios.post(`https://api.telegram.org/bot${token}/setWebhook`, {
  url: 'https://mybot.com/updates',
  drop_pending_updates: true
})

This way, when you restart your bot, it won’t receive old messages. It’s a clean slate every time. Just be aware that you might miss some messages if your bot was down for a while. If that’s a concern, you might want to implement a more robust solution with offset tracking, but for most cases, this simple fix should do the trick.

I’ve encountered this issue before, and it can be frustrating. One solution that worked for me was to implement an offset mechanism. Here’s how you can modify your code:

When you set up the webhook, include an ‘offset’ parameter in your request to the Telegram API. This offset should be the ID of the last update you processed before shutting down.

let lastUpdateId = 0; // Store this persistently

axios.post(`https://api.telegram.org/bot${token}/setWebhook`, {
  url: 'https://mybot.com/updates',
  offset: lastUpdateId + 1
})

In your update handler, keep track of the highest update ID you’ve seen:

app.post('/updates', (req, res) => {
  const updateId = req.body.update_id;
  if (updateId > lastUpdateId) {
    lastUpdateId = updateId;
    // Process the update
  }
  res.sendStatus(200);
})

This way, when you restart your bot, it will only receive updates with IDs higher than the last one it processed. Remember to persist the lastUpdateId value between restarts, perhaps in a database or file.

Hope this helps solve your problem!

hey mike, i’ve had this prob too. what worked 4 me was using the ‘allowed_updates’ parameter when settin up the webhook. it lets u specify which types of updates u want. like this:

axios.post(https://api.telegram.org/bot${token}/setWebhook, {
url: ‘https://mybot.com/updates’,
allowed_updates: [‘message’]
})

this way u only get new messages. hope it helps!