Skip to content

feat(triggers): add contact blocked/unblocked the bot events for telegram - #1691

Merged
sung17 merged 1 commit into
mainfrom
feat/telegram-contact-blocked-bot-trigger
Nov 11, 2026
Merged

sung17 merged 1 commit into
mainfrom
feat/telegram-contact-blocked-bot-trigger

Conversation

@sung17

@sung17 sung17 commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator

A customer asked for a trigger that fires when a contact blocks the Telegram bot.
Telegram already tells us: a block arrives as a my_chat_member update for the
private chat (bot status goes to kicked), an unblock as kicked → member. We
received it but only fed it to the leaderboard group picker.

  • New trigger/webhook events contactBlockedBot / contactUnblockedBot
    (webhook contact_blocked_bot / contact_unblocked_bot, payload adds
    channel). Simple conditions, listed under a new Telegram group in the
    Triggers and Webhooks menus; the public API, CLI and MCP accept them through
    allConditions; templates copy them as they are. All 21 locales translated.
  • The Telegram webhook forwards the old status and the user on
    my_chat_member. The worker sends private chats to telegram-bot-block.ts:
    it fires only for an existing contact (never creates one, no MAC cost),
    claims each update once in Redis so a Telegram retry does not fire twice, and
    emits inside a channel-originated context so outbound webhooks deliver it.
    Private chats are no longer written to TelegramChat; groups are unchanged.
  • Older bots never deliver the update: bots connected before 2026-10-16 are
    not subscribed to my_chat_member, and bots without a webhook secret have
    their audience updates dropped (an unverified one could fire forged
    triggers). apps/worker/scripts/reregister-telegram-webhooks.ts re-registers
    every connected bot with the current allowed_updates and gives secretless
    bots a secret: it calls setWebhook first and saves the secret after, so
    updates in between are still accepted. A bot that already has a secret keeps
    it, revoked tokens are skipped, and the script is safe to re-run.

No migration. Docs: docs/bot-block-trigger-events.md.

Deploy notes

After deploying, run once:

pnpm --filter worker backfill:telegram-webhooks

It needs NEXT_PUBLIC_BROKER_URL or NEXT_PUBLIC_BUILDER_URL, so it builds the
same webhook URL the builder registers on connect. It exits with code 1 if any
bot failed; re-run it to pick those up.

Limits

  • Blocks that happened before this ships never fire: Telegram does not resend
    old updates.
  • No "blocked the bot" contact state, filter or broadcast skip. Tag the contact
    from the trigger if needed.

Testing

  • pnpm lint and type checks for builder, worker, business, events, sdk,
    telegram, database pass. Full test suites for builder, worker, business,
    events, sdk and telegram pass.
  • New tests cover: block, unblock, a retry firing once, an unknown user, a
    non-transition update, the group path, the webhook payloads, the condition
    menu order, the public API schemas, template copy, and the re-register
    script.
  • The jsonb_set that saves the secret was checked on Postgres: it keeps
    secretText and botId, creates metadata when missing, and skips a bot
    that already has a secret.
  • Not yet tested against a real Telegram bot: block it from a phone, check
    that a "Contact blocked the bot → add tag" trigger runs, then unblock with
    /start.

🤖 Generated with Claude Code

…gram

Telegram reports a block as a my_chat_member update in the private chat
(bot status -> kicked) and an unblock as kicked -> member. The webhook now
forwards the old status and the user, and the worker fires the new
contactBlockedBot / contactUnblockedBot trigger and webhook events for the
existing contact (never creating one), deduping Telegram retries in Redis.

Older bots never deliver the update (subscribed before my_chat_member was
added, or without a webhook secret), so a one-off script
`pnpm --filter worker backfill:telegram-webhooks` re-registers every
connected bot and gives secretless bots a secret token.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the feature New feature or request label Nov 10, 2026
@sung17
sung17 merged commit f90838a into main Nov 11, 2026
15 checks passed
hello-jackytruong referenced this pull request in hello-jackytruong/ChatWorkAI Nov 22, 2026
…gram (#1691)

Telegram reports a block as a my_chat_member update in the private chat
(bot status -> kicked) and an unblock as kicked -> member. The webhook now
forwards the old status and the user, and the worker fires the new
contactBlockedBot / contactUnblockedBot trigger and webhook events for the
existing contact (never creating one), deduping Telegram retries in Redis.

Older bots never deliver the update (subscribed before my_chat_member was
added, or without a webhook secret), so a one-off script
`pnpm --filter worker backfill:telegram-webhooks` re-registers every
connected bot and gives secretless bots a secret token.

Co-authored-by: Deathgiver <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant