I’m building a Telegram bot using their API and I have a webhook endpoint that processes incoming messages. My server responds with 200 OK to every request it receives.
When I shut down my bot, I properly remove the webhook URL so Telegram stops sending updates. But here’s my problem: every time I restart the bot and register the webhook again, Telegram sends me a bunch of old messages that accumulated while the bot was offline.
Is there a way to skip these old messages without having to call /getUpdates multiple times to clear the queue?
Here’s my basic setup:
const https = require('https');
const request = require('request');
const botToken = 'YOUR_BOT_TOKEN';
// Register webhook
request.post('https://api.telegram.org/bot' + botToken + '/setWebhook', {
form: {
url: 'https://myserver.com/webhook/telegram'
}
});
process.on('SIGINT', function() {
// Remove webhook on shutdown
request.post('https://api.telegram.org/bot' + botToken + '/setWebhook', {
form: {
url: ''
}
});
});
// Webhook handler
const express = require('express');
const app = express();
app.post('/webhook/telegram', (req, res) => {
res.status(200).send('OK');
});
app.listen(3000);
Any suggestions would be helpful.