Telegram bots2 minutes to read

Handle duplicate Telegram bot updates after a restart

Distinguish Telegram retries from duplicate handlers. Keep recent update IDs across restarts and protect actions with a durable uniqueness check.

Find out whether both replies came from the same update

For one repeatable command, log the numeric update_id and when the handler starts. Use the same log format before and after restarting. Repeated work for the same ID points to a delivery retry or repeated processing. If a single handler start produces two replies, trace where the handler and its middleware call the reply function.

Telegram retries failed webhook requests. In polling mode, it acknowledges an update once a later getUpdates call supplies an offset higher than that update’s ID. Keep one active polling copy for the same bot token while you reproduce the behavior. Telegram update IDs, offsets and delivery.

Keep the starter’s recent update history on disk

The Bothosting Node.js and Python Telegram starters store recent processed IDs in .telegram-state/updates.json, grouped by the bot’s identity. Each starter keeps the last 512 processed IDs and reloads them on startup. Include the directory when moving a deployment that uses the same bot, and keep the live state file out of Git.

On a test bot, send a private command and verify the reply, then restart. Check that the history file still exists in the same working directory. For a webhook, use getWebhookInfo to see whether a retry is pending. A process can finish an external action and exit before its update ID is saved, so protect important effects in durable application storage. Bothosting Telegram starter setup.

Record repeat-sensitive actions with a unique key

For an internal database action such as creating a reminder, use the bot ID and update ID as a unique key. Insert the key and create the reminder in the same transaction. When the key already exists, return success for the webhook or advance the polling offset without creating another reminder.

For an external request, use the downstream service’s idempotency key support where available. Test the same update twice against a development bot and check the resulting record count. This exercises the condition that a single manual Telegram message usually misses. Connect a deployment to a database.