feat(tgbot): add /broadcast to relay an admin message to all clients (#6510)

* feat(tgbot): add /broadcast to relay an admin message to all clients

Admins had no way to reach every client at once: notifications only
cover exhausted quotas, so an operator had to copy a message to each
client chat by hand. Add an admin-only /broadcast flow to the bot:

- /broadcast asks for a message; any message the admin sends — text,
  rich text, photo, video, file, sticker or a whole album — becomes the
  broadcast by reference (admin chat + message ids), and a preview
  self-copy shows the admin exactly what recipients will get while
  rejecting content Telegram cannot copy before the run starts.
- The draft references the original instead of parsing its content, so
  copyMessage/copyMessages deliver everything 1:1 on behalf of the bot
  with no forward header (the admin's identity stays private), no
  caption length pitfalls, and future Telegram message types work
  without new parsing.
- A media group arrives as separate updates; its ids are buffered with
  a short debounce, sorted, and delivered as one copyMessages call so
  recipients see the original album.
- Delivery runs in a background goroutine (common.GoRecover): sequential
  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, a cancel button checked
  between sends, and a final delivered/failed/skipped summary. The
  summary is edited into the card (only sent separately if the card is
  gone), so it is never duplicated.
- 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.

New i18n keys are added to all 13 locales.

* fix(tgbot): harden broadcast composition per review

- Key the composition per admin chat instead of one process-wide draft:
  two admins can now compose at once without dropping each other's
  drafts, and one admin's /broadcast no longer wipes another chat's
  half-collected album.
- Bind each preview card to its own draft via a random token carried in
  the confirm callback, so a stale Send tap is answered with an error
  instead of delivering a newer, unapproved draft.
- Ignore non-admin senders while a chat composes: the awaiting state is
  keyed by chat id, and in a group that chat is shared.
- Check the cancel flag inside the flood-control retry loop, so a 429
  with a long retry_after no longer holds the single broadcast slot
  after the admin cancelled.
- Scale the per-recipient pause by the copied batch size, so an album
  keeps the same per-second ceiling as a single message.
- Trim the comment blocks that exceeded the two-line cap.

* fix(tgbot): reset broadcast state on stop and classify 403 as skipped

- Clear compositions and cancel the active run from StopBot, next to the
  per-chat draft resets: an album debounce timer, a confirmable token or
  a held runner slot must not outlive the receiver that created them.
- Sleep flood-control waits in 5 s slices and re-check cancel and bot
  state between them, so a minutes-long retry_after no longer parks the
  single-runner slot after the admin cancelled or the bot stopped.
- Count Telegram 403 (the chat never started the bot, or blocked it) as
  skipped instead of failed, log it at debug rather than one warning per
  recipient, and append one line to the summary naming the reason.
- Trim the remaining comment blocks over the two-line cap.

* fix(tgbot): count unreachable recipients in broadcast progress throttle

The progress card refresh was keyed on sent+failed, which a 403 does not
advance since unreachable chats were split out of the failure count. A
streak of unreachable recipients while that sum sat on a multiple of
broadcastProgressEvery (0 included, so from the very first recipient)
edited the card once per chat, doubling the request rate the send delay
is sized for and defeating the throttle. Count processed recipients.

* fix(tgbot): key broadcast compositions by admin, not chat

After #6604 moved conversation state to the admin (chatUser), the
broadcast draft map stayed keyed by chat. Two admins composing in one
group then shared a slot: the second admin's message dropped the first
admin's draft, whose Send tap answered "went wrong" while only the other
draft could go out - the same class #6604 fixed for the add-client
wizard. Drafts, album buffers and confirm tokens now live under the
admin who ran /broadcast.

The router now hands handleBroadcastInput only the admin whose own
/broadcast is awaiting input, so its sender re-check and the test that
fed it a non-admin message directly (an input no route can deliver)
are removed.

---------

Co-authored-by: MHSanaei <ho3ein.sanaei@gmail.com>
This commit is contained in:
DIMFLIX
2026-09-27 02:43:32 +03:00
committed by GitHub
parent 71e38367c1
commit 0ef94b686e
17 changed files with 1666 additions and 0 deletions
+14
View File
@@ -2425,6 +2425,7 @@
"usageDesc": "查看客户端用量:/usage 邮箱",
"inboundDesc": "搜索入站:/inbound 备注(管理员)",
"restartDesc": "重启 Xray 内核(管理员)",
"broadcastDesc": "向所有客户端群发消息(管理员)",
"clearallDesc": "重置所有客户端流量(管理员)"
},
"messages": {
@@ -2481,6 +2482,15 @@
"error_add_client": "⚠️ 错误:\n\n {{ .error }}",
"using_default_value": "好的,我会使用默认值。 😊",
"incorrect_input": "您的输入无效。\n短语应连续输入,不能有空格。\n正确示例: aaaaaa\n错误示例: aaa aaa 🚫",
"broadcastAskText": "📤 群发:请把要发给客户的消息发给我 — 文本、照片、视频、文件或整个相册都可以。",
"broadcastAlreadyRunning": "❗ 已有群发正在进行,请等待其完成。",
"broadcastNoRecipients": "❗ 没有绑定 Telegram ID 的客户端。",
"broadcastPreview": "📤 该消息将发送给 {{ .Count }} 位接收者。发送吗?",
"broadcastNotCopyable": "❗ 该消息无法复制用于群发,请发送其他消息。",
"broadcastUnreachable": "ℹ️ {{ .Count }} 位接收者从未启动过机器人(或已将其拉黑),无法发送消息 — 请让他们点击 Start。",
"broadcastProgress": "📤 已发送 {{ .Sent }}/{{ .Total }}(失败:{{ .Failed }})\r\n",
"broadcastFinished": "✅ 群发完成。\r\n👥 接收者:{{ .Total }}\r\n📨 已送达:{{ .Sent }}\r\n🚫 失败:{{ .Failed }}\r\n⏭ 已跳过:{{ .Skipped }}\r\n⏱ 耗时:{{ .Time }}\r\n",
"broadcastCanceled": "🛑 群发已取消。\r\n👥 接收者:{{ .Total }}\r\n📨 已送达:{{ .Sent }}\r\n🚫 失败:{{ .Failed }}\r\n⏭ 已跳过:{{ .Skipped }}\r\n⏱ 耗时:{{ .Time }}\r\n",
"AreYouSure": "你确定吗?🤔",
"SuccessResetTraffic": "📧 邮箱: {{ .ClientEmail }}\n🏁 结果: ✅ 成功",
"FailedResetTraffic": "📧 邮箱: {{ .ClientEmail }}\n🏁 结果: ❌ 失败 \n\n🛠️ 错误: [ {{ .ErrorMessage }} ]",
@@ -2499,6 +2509,7 @@
"buttons": {
"closeKeyboard": "❌ 关闭键盘",
"cancel": "❌ 取消",
"broadcastSend": "📤 立即发送",
"cancelReset": "❌ 取消重置",
"cancelIpLimit": "❌ 取消 IP 限制",
"confirmResetTraffic": "✅ 确认重置流量?",
@@ -2539,6 +2550,9 @@
"SortedTrafficUsageReport": "排序的流量使用报告"
},
"answers": {
"broadcastStarted": "🚀 群发已开始。",
"broadcastCanceling": "🛑 群发将在当前接收者之后停止。",
"broadcastCanceled": "❌ 群发已取消。",
"successfulOperation": "✅ 成功!",
"errorOperation": "❗ 操作错误。",
"getInboundsFailed": "❌ 获取入站信息失败。",