Skip to content

feat(telegram): alert when a bot's webhook points somewhere else - #527

Open
setkyar wants to merge 1 commit into
mainfrom
feat/telegram-webhook-watch
Open

setkyar wants to merge 1 commit into
mainfrom
feat/telegram-webhook-watch

Conversation

@setkyar

@setkyar setkyar commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Problem

A Telegram bot has exactly one webhook, and anything holding its token can replace it with a single setWebhook call. Telegram tells nobody: updates simply go somewhere else, and our ingress route sees silence, not errors.

In production (2026-11-20 → 11-26), another app that shared a workspace's bot token re-registered its own webhook. The inbox missed six days of customer messages, and nothing on our side noticed: no failed requests, no errors, no alert.

Fix

Every 10 minutes (first check 1 minute after start) the API asks Telegram getWebhookInfo for each connected Telegram bot in workspaces with Telegram enabled.

  • If the registered URL is not under our ingress prefix (CHANNEL_INGRESS_PUBLIC_URL || APP_URL + /api/channel-ingress/telegram_bot/), the account is marked status = error, provider_status = webhook_taken_over.
  • If no webhook is registered at all, it is marked webhook_missing.
  • Workspace owners and admins get an in-app system notification and an email (verified addresses only) that link to Connections. Members are not told, because they cannot reconnect.
  • On Connections, the account card explains what happened and the menu offers Reconnect. That is the existing POST /channel-accounts/:id/resume call, which already accepts any account that is not connected and registers our webhook again with a fresh route key and secret.

Safety

  • Only evidence changes state. A network error, a rate limit, a revoked token or an unreadable credential is logged and skipped. The account stays connected.
  • No overwrite race. The mark is a conditional update: the account must still be connected with the same connected_at it had when checked. A reconnect that lands while Telegram is being asked is left alone. The same condition means only one API replica flips the row and sends the alert.
  • No repeat alerts. A flagged account is no longer connected, so later cycles skip it until someone reconnects.
  • Prefix match, not exact match. Only the route key's hash is stored, so the full URL cannot be rebuilt. A lookalike host (app.example.com.evil.test/...) still fails the prefix check, and there's a test for it.
  • channelIngressBaseUrl moved to lib/channel-ingress-url.ts, so the connect/resume routes and the watch share one definition of our URL.

Tests

  • packages/adapter-telegram: getTelegramWebhookInfo returns the URL and pending count, handles an empty URL, and rejects malformed answers.
  • New telegram-webhook-watch.integration.test.ts (9 tests): classification (ours, foreign, lookalike, removed). A healthy bot is untouched. A taken-over bot is marked and only the owner is alerted, once; the next cycle does not ask again. A removed webhook is marked. An unreachable Telegram changes nothing. A reconnect during the check is not overwritten.
  • Locales: two new strings in all 9 bundles; the locale-parity test passes.
  • Local: typecheck clean, biome clean. Unit tests: api 1701, web 799, telegram 28, 0 fail. Integration suites passed on a fresh Postgres 18: the new suite, channel-accounts, channel-accounts quota, Telegram ports and connection-email-alerts.
  • Checked against production (read-only): the live webhook URL of the bot repaired on 2026-11-26 matches the computed prefix, so it will not be flagged.

Release

No migration. API and web change; worker: skip.

A Telegram bot has one webhook, and anything holding its token can replace
it with one call. Telegram tells nobody: updates simply go elsewhere and
our ingress route sees silence, not errors. In production a second app
re-registered a shared bot's webhook and the inbox missed six days of
messages unnoticed.

Every ten minutes the API now asks Telegram (getWebhookInfo) where each
connected bot delivers. If the URL is not under our ingress prefix, or no
webhook is set, the account is marked error with provider_status
webhook_taken_over or webhook_missing, and owners and admins get an
in-app notification and an email. The card on Connections explains it
and offers Reconnect, which is the existing resume call and registers our
webhook again.

- Only a successful Telegram answer changes anything; network errors,
  revoked tokens and unreadable credentials are logged and skipped.
- The mark is conditional on the account still being connected with the
  same connected_at, so a reconnect during the check is never
  overwritten, and only one API replica alerts.
- A flagged account is no longer connected, so it is not re-checked and
  the alert is not repeated.
- channelIngressBaseUrl moves to lib/channel-ingress-url.ts so the route
  and the watch share one definition of our URL.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant