Skip to content
Rezha Julio

telegram

How telegateway Avoids Losing Telegram Webhooks

7 min read

Telegram retries a failed webhook, then gives up. telegateway moves that promise onto your own disk: write the update first, answer 200 second, deliver third. Here is the design, a walkthrough you can click through, and where it stops.

A person at a small desk by a village road writes a letter into a ledger and stamps it, while a paper plane waits at an open gate

A bot that listens for webhooks has one job: be there when Telegram knocks.

Telegram does not leave you empty-handed if you are not. Its setWebhook docs say that when a request gets anything other than a 2XX answer, Telegram repeats it and gives up after “a reasonable amount of attempts”. That is a real safety net. It is also a promise with no number in it. You do not choose the schedule, you cannot see the queue, and “reasonable” is Telegram’s call.

telegateway is a small Rust gateway that moves that promise onto your own disk, where you can read it.

Accepted means kept

The gateway sits between Telegram and your bots. Telegram posts to the gateway, and the gateway forwards to the bot, which stays on a private network. The order inside the gateway is the whole design:

For a new update that passes the checks, the gateway writes it down, answers 200, and only then tries to hand it to the bot. If the write fails, it answers 503, so Telegram’s own retry takes over. The 200 is the point where the gateway starts owning the update, and for a new, admitted update it never comes before the row is in SQLite.

Not every 200 means “stored”, though. A 200 also goes back for a duplicate of an update that is already in the outbox, and for an update dropped by the rate limit, which is deliberately not stored (more on that below). The HTTP status tells Telegram to stop retrying. Whether the update was kept depends on which path produced it.

That changes who is responsible. With a webhook pointed straight at the bot, the question “was this update handled?” lives in Telegram’s retry logic. With the gateway, it lives in a SQLite file you can open.

Follow one update through

Pick a scenario below and step through it. Each one is a path the README and ARCHITECTURE doc describe.

Illustrative: one click is one step, not real traffic

The steps follow the behavior described in the telegateway README, ARCHITECTURE.md and CHANGELOG. Row numbers, update ids and the retry waits shown are examples, and nothing here is timed.

The slow path is the interesting one. When the bot does not answer, the update waits in the outbox and a dispatcher retries with growing pauses, starting around 5 seconds, doubling with jitter, capped at an hour. After 25 attempts it becomes a dead letter that you can list and replay. A circuit breaker watches each bot too: after a run of failures it stops trying and lets rows wait, so a dead endpoint does not eat the retry budget of every queued update.

The other paths are smaller choices that add up. A 4xx from the bot, other than 408, 425 or 429, goes straight to dead letters, because the gateway treats most 4xx answers as permanent rejections, not temporary trouble. The secret is checked before the body is parsed, so an unauthenticated caller never reaches the JSON.

Why a flood still gets a 200

The rate limit is a token bucket per chat, 30 updates a minute by default. When a chat goes over, the gateway drops the update and answers 200 anyway.

That feels wrong until you ask what the alternative does. A 429 or 5xx tells Telegram the update failed, and Telegram retries. The flood gets bigger. A 200 says “handled”, which is true in the sense that the gateway made a decision about it. This is the one 200 that deliberately does not mean “kept”: the update is gone. Duplicates are checked before the quota is spent, so a Telegram redelivery does not eat a chat’s budget.

Private bots, and the price of it

Only the gateway faces the internet. The bots listen on the private network and trust the gateway to have done the admission work. That is also the weak spot. Anyone who can reach a bot directly skips the secret check and the rate limit. The architecture doc says it plainly: bots must never be exposed publicly.

Adding a bot is a few lines of config:

[[bots]]
id = "mediadownloadbot"
downstream_url = "http://127.0.0.1:8081/webhook"
webhook_secret = "a-long-random-value"
rate_limit_per_minute = 30

With auto_register = true, a public_url and a bot token, the gateway also calls setWebhook itself at startup.

What it does not promise

The repo is clear about its limits, and they are worth repeating:

  • At-least-once, not exactly-once. A retry can deliver an update twice, and retries can reorder updates. The bot has to deduplicate on update_id, which arrives in the X-Telegateway-Update-Id header.
  • Ordering is per chat. A failed row blocks later rows in the same chat until it is retried. Across chats there is no ordering.
  • The dedupe window is the retention window. Delivered rows are pruned after 7 days by default, so a much older redelivery would be processed again.
  • Rate-limiter and breaker state live in memory. A restart resets them. Rows left in flight by a crash are requeued at startup.
  • It is one node. Running several gateways would need shared outbox storage.
  • Replay is one update at a time. There is no bulk replay for dead letters.

What the changelog admits

The first releases had bugs, and v0.1.4 fixed the instructive ones. A row left in flight after a failed mark_delivered could block its chat’s queue until the next restart, so a periodic sweeper now requeues stale in-flight rows. The queue cap was check-then-insert, which a concurrent burst could overshoot, so it is now one statement. Rate-limit quota was spent before the dedupe and cap checks, burning tokens that cannot be refunded, so it now comes after them. And a half-open breaker probe that got a 4xx back used to dead-letter the row, when a 4xx actually proves the bot is reachable.

None of that is exotic. It is the kind of bug you find when you read your own retry logic as if someone else wrote it.

The rest of the early releases were dependency work. v0.1.1 bumped rustls, webpki and OpenTelemetry to clear 7 of 8 flagged CVEs. The eighth, in rsa, had no fixed release then. v0.1.3 upgraded sqlx to 0.9, and the changelog says the rsa dependency went away with it.

Does it work?

I ran telegateway live on my own server with PythonID-bot, and all of the bot’s features work well through it. That is a functional test. This draft has no load numbers, no latency overhead and no outage drill to report, and the README’s “around 6 MB RSS at idle” is the README’s claim, not something shown here.

The code is AGPL-3.0-or-later, on GitHub and crates.io. The CHANGELOG is the best map of what changed and why.

This post was drafted by an AI assistant from the repo’s own docs and history. The live test is mine.


Related Posts


Previous Post
Trying bun lint on my Astro blog