[PR #6490] [MERGED] fix(tgbot): send the admin traffic reports as one message #6303

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

📋 Pull Request Information

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

Base: main ← Head: fix/tgbot-report-batching


📝 Commits (3)

  • a50fd20 fix(tgbot): send the admin traffic reports as one message
  • e74be3f Merge branch 'main' into fix/tgbot-report-batching
  • a960ac3 Merge remote-tracking branch 'upstream/main' into fix/tgbot-report-batching

📊 Changes

2 files changed (+217 additions, -21 deletions)

View changed files

➕ internal/web/service/tgbot/tgbot_report_batching_test.go (+189 -0)
📝 internal/web/service/tgbot/tgbot_router.go (+28 -21)

📄 Description

Summary

Two details the batching must not lose are part of this change: the reset report
still answers when the panel has no clients, and both reports are HTML-escaped
as a whole, because a single stray < in a remark or an email now costs the
~15-client page it lands on instead of one client's message.

Both admin reports looped over every client and called SendMsgToTgbot inside
the loop, so one tap produced one Telegram message per client. They now
accumulate a single report, which SendMsgToTgbot already pages at 2000
characters per message.

Why

Two callbacks built their output by sending inside the loop:

  • reset_all_traffics_c — /clearall → Confirm, plus the Reset all
    traffics
    button (tgbot_router.go:1259)
  • get_sorted_traffic_usage_report — the Sorted traffic usage report button
    on the bot's main keyboard (tgbot_router.go:1287)

What the operator sees today

An operator with a few hundred clients taps Sorted traffic usage report (the
button exists precisely for that size of panel). The bot then issues one
sendMessage per client, back to back, with 100 ms between them.

  • Telegram throttles a chat far below that burst rate. The excess comes back
    as 429 Too Many Requests, and SendMsgToTgbot classifies a rate-limit
    answer as neither connection nor timeout nor closed, so it stops
    retrying and logs Error sending telegram message: once per message
    (tgbot_send.go:154). The report arrives in fragments, silently missing the
    clients whose messages were refused, and the log fills with one warning line
    per refused message.
  • The same tap on a 500-client panel is ~500 API calls, each one holding a
    worker slot in the 10-goroutine pool that also serves every other callback.

The single-message path avoids all of it: SendMsgToTgbot already splits a
long report with pageMessage, so the same content goes out as a handful of
paged messages, and the page ceiling is the documented 2000-character message
limit rather than the client count.

This is the same burst pattern as the merged #6340 (per-refresh log spam);
only the loop was left.

The two things batching changes

A panel with no clients. Reset all traffics → Confirm always ended with
the "process finished" reply and its ReplyKeyboardRemove. Guarding the whole
send with report.Len() > 0 made an empty panel answer nothing: the prompt had
already been deleted, so the operator tapped Confirm and watched the message
vanish. FinishProcess is appended unconditionally again and that guard is gone
from the path.

An unescaped <. Both reports go out through SendMsgToTgbot, which sets
ParseMode: "HTML", and the text carries operator input — the inbound remark
(clientInfoMsg → describeAttachedInbounds) and the email, both raw. Before,
a remark like DE <fast> broke that one client's message and the rest of the
report still arrived; with ~15 clients per page it now costs the whole page, and
the 400 matches none of the connection/timeout/closed substrings the retry
logic looks for, so the page is dropped with one warning.

Both reports are therefore escaped as a whole before they are sent. That is safe
because none of the i18n values on these two paths carry markup in any of the 13
locales (checked across internal/web/translation/*.json); the bot's HTML markup
lives in the command help and welcome strings, not in the report strings.

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Documentation
  • Tests only
  • Build / CI / tooling
  • Other

Areas affected

  • Frontend (UI / panel pages)
  • Backend (API endpoints, login, settings)
  • Xray config generation
  • Subscription (share links / Clash / JSON)
  • Statistics / traffic counters — report delivery only, reset semantics untouched
  • Database / migrations
  • Install / upgrade script
  • Docker image
  • Multi-node (sub-nodes)
  • Telegram bot

How was this tested?

internal/web/service/tgbot/tgbot_report_batching_test.go seeds one inbound with
three clients — each with its traffic row, its clients/client_inbounds link
and its settings entry, so all three reach the report — and drives the admin
callbacks through answerCallback against a mocked Telegram API that records
what Telegram would have received:

  • TestTrafficUsageReportIsOneMessage — exactly one sendMessage for the
    whole report. On the old code that tap sends three, so it fails before the fix.
  • TestResetAllTrafficsAnswersWithNoClients — an empty panel. Asserts one
    message, non-empty text and {"remove_keyboard":true}. Fails on the version
    that guarded the whole send.
  • TestTrafficUsageReportEscapesHtml — an inbound remarked DE <fast>. Asserts
    the text carries &lt;fast&gt; and no raw <fast>. Fails without the escape.

The message text is otherwise unchanged: the per-client blocks are the same
clientInfoMsg output, now separated by a blank line and joined into one
report, and the "no result" entries for emails without a traffic row are
appended to the same report.

Both loops share the mechanism, but the tests drive the read-only report path and
the empty-panel answer; reset_all_traffics_c performs real resets through the
traffic writer once clients exist, which the throwaway-DB test environment does
not stand up.

Screenshots / recordings

N/A — bot messages, no UI.

Breaking changes

None for the values the bot itself formats. Values carrying <, &, >, "
or ' are now escaped, which is what Telegram's HTML parse requires — such a
report previously lost the page it landed on. The reply keyboard is still
removed, and SendMsgToTgbot applies it to the last page.

Checklist

  • I tested the change locally and confirmed the described behavior. — no C compiler on this machine, see above; CI runs the new test.
  • I added or updated tests for the new behavior (when applicable).
  • go build ./... and the test suite pass locally. — same reason as above.
  • For frontend changes: npm run lint, npm run typecheck, and npm run build pass. — no frontend files touched.
  • I updated the Wiki / README / API docs if user-facing behavior changed. — no endpoint or wire-format change.
  • My commits follow the project's existing message style.
  • I have no unrelated changes mixed into this PR.

🔄 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/6490 **Author:** [@BlindMaster24](https://github.com/BlindMaster24) **Created:** 9/12/2026 **Status:** ✅ Merged **Merged:** 9/13/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/tgbot-report-batching` --- ### 📝 Commits (3) - [`a50fd20`](https://github.com/MHSanaei/3x-ui/commit/a50fd20990ca9a65c1fa850c3cb659c065d4868b) fix(tgbot): send the admin traffic reports as one message - [`e74be3f`](https://github.com/MHSanaei/3x-ui/commit/e74be3f27e43fb80d7c6042f8c3e0f42b3b4433c) Merge branch 'main' into fix/tgbot-report-batching - [`a960ac3`](https://github.com/MHSanaei/3x-ui/commit/a960ac36abf0e1a97f60b5e37aed8c44cd0648eb) Merge remote-tracking branch 'upstream/main' into fix/tgbot-report-batching ### 📊 Changes **2 files changed** (+217 additions, -21 deletions) <details> <summary>View changed files</summary> ➕ `internal/web/service/tgbot/tgbot_report_batching_test.go` (+189 -0) 📝 `internal/web/service/tgbot/tgbot_router.go` (+28 -21) </details> ### 📄 Description ## Summary Two details the batching must not lose are part of this change: the reset report still answers when the panel has no clients, and both reports are HTML-escaped as a whole, because a single stray `<` in a remark or an email now costs the ~15-client page it lands on instead of one client's message. Both admin reports looped over every client and called `SendMsgToTgbot` inside the loop, so one tap produced one Telegram message per client. They now accumulate a single report, which `SendMsgToTgbot` already pages at 2000 characters per message. ## Why Two callbacks built their output by sending inside the loop: - `reset_all_traffics_c` — `/clearall` → *Confirm*, plus the *Reset all traffics* button (`tgbot_router.go:1259`) - `get_sorted_traffic_usage_report` — the *Sorted traffic usage report* button on the bot's main keyboard (`tgbot_router.go:1287`) ### What the operator sees today An operator with a few hundred clients taps *Sorted traffic usage report* (the button exists precisely for that size of panel). The bot then issues one `sendMessage` per client, back to back, with 100 ms between them. - Telegram throttles a chat far below that burst rate. The excess comes back as `429 Too Many Requests`, and `SendMsgToTgbot` classifies a rate-limit answer as neither `connection` nor `timeout` nor `closed`, so it stops retrying and logs `Error sending telegram message:` once per message (`tgbot_send.go:154`). The report arrives in fragments, silently missing the clients whose messages were refused, and the log fills with one warning line per refused message. - The same tap on a 500-client panel is ~500 API calls, each one holding a worker slot in the 10-goroutine pool that also serves every other callback. The single-message path avoids all of it: `SendMsgToTgbot` already splits a long report with `pageMessage`, so the same content goes out as a handful of paged messages, and the page ceiling is the documented 2000-character message limit rather than the client count. This is the same burst pattern as the merged `#6340` (per-refresh log spam); only the loop was left. ### The two things batching changes **A panel with no clients.** *Reset all traffics* → *Confirm* always ended with the "process finished" reply and its `ReplyKeyboardRemove`. Guarding the whole send with `report.Len() > 0` made an empty panel answer nothing: the prompt had already been deleted, so the operator tapped *Confirm* and watched the message vanish. `FinishProcess` is appended unconditionally again and that guard is gone from the path. **An unescaped `<`.** Both reports go out through `SendMsgToTgbot`, which sets `ParseMode: "HTML"`, and the text carries operator input — the inbound remark (`clientInfoMsg` → `describeAttachedInbounds`) and the email, both raw. Before, a remark like `DE <fast>` broke that one client's message and the rest of the report still arrived; with ~15 clients per page it now costs the whole page, and the 400 matches none of the `connection`/`timeout`/`closed` substrings the retry logic looks for, so the page is dropped with one warning. Both reports are therefore escaped as a whole before they are sent. That is safe because none of the i18n values on these two paths carry markup in any of the 13 locales (checked across `internal/web/translation/*.json`); the bot's HTML markup lives in the command help and welcome strings, not in the report strings. ## Type of change - [x] Bug fix - [ ] New feature - [ ] Refactoring (no behavior change) - [ ] Documentation - [ ] Tests only - [ ] Build / CI / tooling - [ ] Other ## Areas affected - [ ] Frontend (UI / panel pages) - [ ] Backend (API endpoints, login, settings) - [ ] Xray config generation - [ ] Subscription (share links / Clash / JSON) - [x] Statistics / traffic counters — report delivery only, reset semantics untouched - [ ] Database / migrations - [ ] Install / upgrade script - [ ] Docker image - [ ] Multi-node (sub-nodes) - [x] Telegram bot ## How was this tested? `internal/web/service/tgbot/tgbot_report_batching_test.go` seeds one inbound with three clients — each with its traffic row, its `clients`/`client_inbounds` link and its settings entry, so all three reach the report — and drives the admin callbacks through `answerCallback` against a mocked Telegram API that records what Telegram would have received: - `TestTrafficUsageReportIsOneMessage` — exactly **one** `sendMessage` for the whole report. On the old code that tap sends three, so it fails before the fix. - `TestResetAllTrafficsAnswersWithNoClients` — an empty panel. Asserts one message, non-empty text and `{"remove_keyboard":true}`. Fails on the version that guarded the whole send. - `TestTrafficUsageReportEscapesHtml` — an inbound remarked `DE <fast>`. Asserts the text carries `&lt;fast&gt;` and no raw `<fast>`. Fails without the escape. The message text is otherwise unchanged: the per-client blocks are the same `clientInfoMsg` output, now separated by a blank line and joined into one report, and the "no result" entries for emails without a traffic row are appended to the same report. Both loops share the mechanism, but the tests drive the read-only report path and the empty-panel answer; `reset_all_traffics_c` performs real resets through the traffic writer once clients exist, which the throwaway-DB test environment does not stand up. ## Screenshots / recordings N/A — bot messages, no UI. ## Breaking changes None for the values the bot itself formats. Values carrying `<`, `&`, `>`, `"` or `'` are now escaped, which is what Telegram's HTML parse requires — such a report previously lost the page it landed on. The reply keyboard is still removed, and `SendMsgToTgbot` applies it to the last page. ## Checklist - [ ] I tested the change locally and confirmed the described behavior. — no C compiler on this machine, see above; CI runs the new test. - [x] I added or updated tests for the new behavior (when applicable). - [ ] `go build ./...` and the test suite pass locally. — same reason as above. - [ ] For frontend changes: `npm run lint`, `npm run typecheck`, and `npm run build` pass. — no frontend files touched. - [ ] I updated the Wiki / README / API docs if user-facing behavior changed. — no endpoint or wire-format change. - [x] My commits follow the project's existing message style. - [x] I have no unrelated changes mixed into this PR. --- <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:48 +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#6303
No description provided.