[PR #6510] [MERGED] feat(tgbot): add /broadcast to relay an admin message to all clients #6319

Closed
opened 2026-11-02 04:07:55 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6510
Author: @DIMFLIX
Created: 9/13/2026
Status: ✅ Merged
Merged: 9/26/2026
Merged by: @MHSanaei

Base: main ← Head: feat/tgbot-broadcast


📝 Commits (6)

  • 72e5f73 feat(tgbot): add /broadcast to relay an admin message to all clients
  • 088a32e fix(tgbot): harden broadcast composition per review
  • e4b2a13 fix(tgbot): reset broadcast state on stop and classify 403 as skipped
  • 804508c fix(tgbot): count unreachable recipients in broadcast progress throttle
  • cc13eb5 Merge remote-tracking branch 'origin/main' into pr/6510
  • ac2cde4 fix(tgbot): key broadcast compositions by admin, not chat

📊 Changes

17 files changed (+1666 additions, -0 deletions)

View changed files

📝 internal/web/service/tgbot/tgbot.go (+2 -0)
➕ internal/web/service/tgbot/tgbot_broadcast.go (+525 -0)
➕ internal/web/service/tgbot/tgbot_broadcast_test.go (+941 -0)
📝 internal/web/service/tgbot/tgbot_router.go (+16 -0)
📝 internal/web/translation/ar-EG.json (+14 -0)
📝 internal/web/translation/en-US.json (+14 -0)
📝 internal/web/translation/es-ES.json (+14 -0)
📝 internal/web/translation/fa-IR.json (+14 -0)
📝 internal/web/translation/id-ID.json (+14 -0)
📝 internal/web/translation/ja-JP.json (+14 -0)
📝 internal/web/translation/pt-BR.json (+14 -0)
📝 internal/web/translation/ru-RU.json (+14 -0)
📝 internal/web/translation/tr-TR.json (+14 -0)
📝 internal/web/translation/uk-UA.json (+14 -0)
📝 internal/web/translation/vi-VN.json (+14 -0)
📝 internal/web/translation/zh-CN.json (+14 -0)
📝 internal/web/translation/zh-TW.json (+14 -0)

📄 Description

What

An admin-only /broadcast command for the Telegram bot. The admin sends any message — text, rich text, photos, a video, a file, a sticker or a whole album — confirms it with an inline button, and the bot delivers an exact copy of it to every client that has a Telegram ID linked, on behalf of the bot.

Why

There is currently no way to reach all clients at once: notifications only cover exhausted quotas, so an operator who wants to announce maintenance or a new feature has to copy the message to each client chat by hand.

How it works

  1. /broadcast asks for a message (one broadcast at a time; a second command is refused while one runs).
  2. The admin's message becomes the broadcast by reference (admin chat + message ids) — the bot never parses the content. A preview self-copy shows the admin exactly what recipients will get and rejects content Telegram cannot copy before the run starts.
  3. Delivery runs in a background goroutine: sequential copyMessage sends with a small pause, 429 retry_after honored per recipient, failures counted without stopping the run, progress edited into one card at most every 25 sends or 3 seconds, and a cancel button checked between sends. The final delivered/failed/skipped summary is edited into the card so it is never duplicated.
  4. Recipients repeat the notifyExhausted walk: clients with a linked tg_id, deduplicated, admins excluded — they already receive the reports. The message content is never logged.

Design note: copy instead of content parsing

The first draft of this feature parsed the admin's message into a draft (text/media kind/file id) and re-sent it with sendMessage/sendPhoto/... That broke on the new rich_message format, would need a new parser for every Telegram message type, and ran into caption length limits. copyMessage/copyMessages avoid all of that: content arrives 1:1 with formatting preserved, future message types work for free, and — unlike forwardMessage — there is no "Forwarded from" header, so the admin's identity stays private.

Albums arrive as separate updates sharing a media_group_id; their ids are buffered with a short debounce, sorted (copyMessages requires strictly increasing ids), and delivered as one copyMessages call so recipients see the original album.

Notes / limitations

  • Telegram only delivers to chats where the client has started a dialog with the bot; everything else is counted as "failed" in the summary.
  • Interactive rich-message blocks (buttons, maps) that a client may include are delivered as-is by Telegram's copy; the bot adds no controls of its own to the relayed content.

Testing

  • New tgbot_broadcast_test.go (stdlib testing, table-driven): recipient collection against a real DB (dedup, admin exclusion, tg_id == 0), run counters (delivered/failed), mid-run cancel, 429 retry pauses via a swappable sender, single-slot runner, admin-only command and callbacks, album buffering with out-of-order updates, wire assertions for copyMessage/copyMessages bodies, and an end-to-end confirm through answerCallback against a mock API.
  • go build ./..., go test ./internal/web/service/tgbot/ -count=1, make test-go, golangci-lint run all pass.
  • The new i18n keys are present in all 13 locales and referenced from the Go sources.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/6510 **Author:** [@DIMFLIX](https://github.com/DIMFLIX) **Created:** 9/13/2026 **Status:** ✅ Merged **Merged:** 9/26/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `feat/tgbot-broadcast` --- ### 📝 Commits (6) - [`72e5f73`](https://github.com/MHSanaei/3x-ui/commit/72e5f73125f2596271fed844859070335fa80fed) feat(tgbot): add /broadcast to relay an admin message to all clients - [`088a32e`](https://github.com/MHSanaei/3x-ui/commit/088a32e254fdb8dbd8f0774546772603b509f397) fix(tgbot): harden broadcast composition per review - [`e4b2a13`](https://github.com/MHSanaei/3x-ui/commit/e4b2a13bef19f1c4a1f6c72aeb2b51f64fde66d5) fix(tgbot): reset broadcast state on stop and classify 403 as skipped - [`804508c`](https://github.com/MHSanaei/3x-ui/commit/804508cdc7c5331aaa7c5e1cf75e403deeffde8a) fix(tgbot): count unreachable recipients in broadcast progress throttle - [`cc13eb5`](https://github.com/MHSanaei/3x-ui/commit/cc13eb55dd018a166df12ee5f546bddf3acda68a) Merge remote-tracking branch 'origin/main' into pr/6510 - [`ac2cde4`](https://github.com/MHSanaei/3x-ui/commit/ac2cde4058c240622dc71cdd5a8c33bd543f2f2b) fix(tgbot): key broadcast compositions by admin, not chat ### 📊 Changes **17 files changed** (+1666 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `internal/web/service/tgbot/tgbot.go` (+2 -0) ➕ `internal/web/service/tgbot/tgbot_broadcast.go` (+525 -0) ➕ `internal/web/service/tgbot/tgbot_broadcast_test.go` (+941 -0) 📝 `internal/web/service/tgbot/tgbot_router.go` (+16 -0) 📝 `internal/web/translation/ar-EG.json` (+14 -0) 📝 `internal/web/translation/en-US.json` (+14 -0) 📝 `internal/web/translation/es-ES.json` (+14 -0) 📝 `internal/web/translation/fa-IR.json` (+14 -0) 📝 `internal/web/translation/id-ID.json` (+14 -0) 📝 `internal/web/translation/ja-JP.json` (+14 -0) 📝 `internal/web/translation/pt-BR.json` (+14 -0) 📝 `internal/web/translation/ru-RU.json` (+14 -0) 📝 `internal/web/translation/tr-TR.json` (+14 -0) 📝 `internal/web/translation/uk-UA.json` (+14 -0) 📝 `internal/web/translation/vi-VN.json` (+14 -0) 📝 `internal/web/translation/zh-CN.json` (+14 -0) 📝 `internal/web/translation/zh-TW.json` (+14 -0) </details> ### 📄 Description ## What An admin-only `/broadcast` command for the Telegram bot. The admin sends any message — text, rich text, photos, a video, a file, a sticker or a whole album — confirms it with an inline button, and the bot delivers an exact copy of it to every client that has a Telegram ID linked, on behalf of the bot. ## Why There is currently no way to reach all clients at once: notifications only cover exhausted quotas, so an operator who wants to announce maintenance or a new feature has to copy the message to each client chat by hand. ## How it works 1. `/broadcast` asks for a message (one broadcast at a time; a second command is refused while one runs). 2. The admin's message becomes the broadcast **by reference** (admin chat + message ids) — the bot never parses the content. A preview self-copy shows the admin exactly what recipients will get and rejects content Telegram cannot copy before the run starts. 3. Delivery runs in a background goroutine: sequential `copyMessage` sends with a small pause, `429 retry_after` honored per recipient, failures counted without stopping the run, progress edited into one card at most every 25 sends or 3 seconds, and a cancel button checked between sends. The final delivered/failed/skipped summary is edited into the card so it is never duplicated. 4. Recipients repeat the `notifyExhausted` walk: clients with a linked `tg_id`, deduplicated, admins excluded — they already receive the reports. The message content is never logged. ### Design note: copy instead of content parsing The first draft of this feature parsed the admin's message into a draft (text/media kind/file id) and re-sent it with `sendMessage`/`sendPhoto`/... That broke on the new `rich_message` format, would need a new parser for every Telegram message type, and ran into caption length limits. `copyMessage`/`copyMessages` avoid all of that: content arrives 1:1 with formatting preserved, future message types work for free, and — unlike `forwardMessage` — there is no "Forwarded from" header, so the admin's identity stays private. Albums arrive as separate updates sharing a `media_group_id`; their ids are buffered with a short debounce, sorted (`copyMessages` requires strictly increasing ids), and delivered as one `copyMessages` call so recipients see the original album. ## Notes / limitations - Telegram only delivers to chats where the client has started a dialog with the bot; everything else is counted as "failed" in the summary. - Interactive rich-message blocks (buttons, maps) that a client may include are delivered as-is by Telegram's copy; the bot adds no controls of its own to the relayed content. ## Testing - New `tgbot_broadcast_test.go` (stdlib `testing`, table-driven): recipient collection against a real DB (dedup, admin exclusion, `tg_id == 0`), run counters (delivered/failed), mid-run cancel, 429 retry pauses via a swappable sender, single-slot runner, admin-only command and callbacks, album buffering with out-of-order updates, wire assertions for `copyMessage`/`copyMessages` bodies, and an end-to-end confirm through `answerCallback` against a mock API. - `go build ./...`, `go test ./internal/web/service/tgbot/ -count=1`, `make test-go`, `golangci-lint run` all pass. - The new i18n keys are present in all 13 locales and referenced from the Go sources. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:07:55 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#6319
No description provided.