Develop a Telegram bot with webhooks without fighting over setWebhook

· 4 min read

A Telegram bot has one webhook URL. When two developers each start a tunnel and call setWebhook, the second call silently takes the bot away from the first, and the updates for the rest of the afternoon go to one laptop. This guide points the bot at one URL once, verifies Telegram's secret token at the edge, and gives every developer the updates for their own test chat.

Bot API facts below are quoted from the Telegram Bot API as of 2026-10-14.

Why one bot is hard to share

  • One webhook per bot. setWebhook sets the URL that receives the bot's updates; the last call wins.
  • No polling on the side. While a webhook is set, getUpdates is off: "You will not be able to receive updates using getUpdates for as long as an outgoing webhook is set up."
  • Failed deliveries are given up. For a response outside the 2XX range, Telegram will "repeat the request and give up after a reasonable amount of attempts". An update that hit a closed laptop is not coming back.
  • Ports. Webhooks are accepted only on ports 443, 80, 88 and 8443, so a random high port from a local tunnel is not an option.

The usual workaround is a bot per developer — n8n's docs, for example, recommend a separate bot per environment. That works, at the price of a BotFather registration, a token and a configuration per person, and a bot username that differs from the one your team tests against.

One inbox, set once

A Tunnex inbox is a stable HTTPS URL owned by a team (port 443). It stores every update, answers Telegram immediately and delivers a copy to every developer who listens, including the updates that arrived while they were offline.

1. Create a secret token and the inbox. secret_token may be 1–256 characters from A-Z, a-z, 0-9, _ and -; 64 hex characters fit:

pip install tunnex && tunnex login
tunnex teams create acme
export TUNNEX_INBOX_SECRET=$(openssl rand -hex 32)
tunnex inboxes create acme-bot --team acme --provider telegram

2. Point the bot at the inbox — once, for the whole team:

curl -s "https://api.telegram.org/bot$BOT_TOKEN/setWebhook" \
  -d url=https://acme-bot.tunnex.org/telegram \
  -d secret_token="$TUNNEX_INBOX_SECRET"
curl -s "https://api.telegram.org/bot$BOT_TOKEN/getWebhookInfo"

From now on nobody calls setWebhook again. Put that in the README.

3. Every developer listens (after tunnex teams join <link> from tunnex teams invite acme):

tunnex listen acme-bot --forward 8080

Updates arrive at http://localhost:8080/telegram with Telegram's headers and the unchanged JSON body.

The secret token is checked twice

Telegram sends the token in the X-Telegram-Bot-Api-Secret-Token header of every webhook request. With --provider telegram, the inbox compares it with its secret in constant time; a request without the right token gets 401 and is stored but never delivered, so someone who guesses the URL cannot drive your bot.

Keep the same check in the bot, as in production — the header is forwarded unchanged:

import hmac
import os

SECRET = os.environ["TELEGRAM_SECRET_TOKEN"]

def is_from_telegram(headers) -> bool:
    return hmac.compare_digest(headers.get("X-Telegram-Bot-Api-Secret-Token", ""), SECRET)

The token carries no timestamp, so caught-up and replayed updates keep a valid header and need no re-signing.

Reply with API calls, not in the webhook response

The Bot API lets a bot answer an update by putting a method call in the webhook's HTTP response. Behind an inbox that does not work: the inbox has already answered Telegram (200 {"ok":true} by default) by the time your code runs, and your response goes back to your listener's delivery log, not to Telegram. Send replies with a normal request to https://api.telegram.org/bot<token>/sendMessage. Telegram's documentation notes that with the in-response method "it's not possible to know that such a request was successful" anyway.

Every developer gets every update — route by chat

Fan-out has a consequence in a chat bot: if three developers run the bot and someone writes to it, the message gets three replies. Give each developer a test chat of their own (a private chat with the bot, or a test group) and filter on its id:

tunnex inboxes events acme-bot              # find your message
tunnex inboxes event acme-bot 77            # its body shows message.chat.id
tunnex listen acme-bot --forward 8080 --filter json.message.chat.id=123456789

Filters are combined with AND, and button presses carry the chat under callback_query.message.chat.id. To receive both, describe two listeners in a tunnex.yaml and run them with tunnex start:

version: 1
listeners:
  messages:
    inbox: acme-bot
    forward: 8080
    filters: ["json.message.chat.id=123456789"]
    name: alice-messages
  buttons:
    inbox: acme-bot
    forward: 8080
    filters: ["json.callback_query.message.chat.id=123456789"]
    name: alice-buttons

tunnex start --check validates the file without connecting. The event list shows each update's kind (message, callback_query, edited_message, …), which makes the right filter easy to spot.

Missed updates and replays

Stop the bot, change the handler, start it again: the updates that arrived meanwhile are delivered in order from your listener's cursor, within the inbox's retention (7 days by default). To run a handler again against the same update:

tunnex inboxes replay acme-bot 77

Deliveries are at least once, so deduplicate on update_id. If your bot is down (connection refused), the delivery is retried with backoff while tunnex listen stays connected.

Staging and production

A staging deployment listens like a developer, with a token limited to listening and a fixed name so its cursor survives restarts:

tunnex tokens create staging-bot --scope listen --expires-in 90

Keep the inbox for development and staging bots. Your production bot keeps its own webhook pointing at your production service; the inbox is not meant to sit in front of it.

When you do not need this

Alone on a bot, the simplest loop is long polling: no webhook, no public URL, getUpdates from your laptop. The inbox earns its place once several people develop the same bot, a staging deployment must not lose updates, or you want to replay the exact update that broke the handler.

Edit this page on GitLab