[PR #6493] [MERGED] fix(tgbot): answer the callbacks the bot cannot route #6306

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

📋 Pull Request Information

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

Base: main ← Head: fix/tgbot-answer-declined-callbacks


📝 Commits (1)

  • 29a14e9 fix(tgbot): answer the callbacks the bot cannot route

📊 Changes

2 files changed (+92 additions, -0 deletions)

View changed files

➕ internal/web/service/tgbot/tgbot_declined_callback_test.go (+81 -0)
📝 internal/web/service/tgbot/tgbot_router.go (+11 -0)

📄 Description

Summary

Three paths through answerCallback returned without calling
AnswerCallbackQuery: a payload that matches no case in the email-scoped
switch, one that matches nothing in the switch after it, and a button whose hash
had aged out of the 20-minute storage, which replied with a chat message only.
All three answer now, and the expired-hash path keeps its notice.

Why

Telegram keeps the tapped button in a loading state until the bot calls
answerCallbackQuery; the Bot API documents answering as mandatory for every
callback query. A callback that is never answered leaves that indicator running
until the client gives up, and the user is told nothing about what happened.

The reachable half: a button older than its payload

Payloads longer than 64 characters are not sent as button data — encodeQuery
replaces them with an MD5 hash kept in hashStorage (tgbot.go:493). That
storage is in-memory and TTL-limited: Start creates it with a 20-minute
expiration (tgbot.go:245) and a cron sweeps it every two minutes.

Long payloads are ordinary, because the payload carries the client email:
client_individual_links <email> crosses 64 characters for an email of about 40
(tgbot_client.go:241), limit_traffic_in <email> ... at about 43, and the
same for every other per-client button.

So an admin opens a client card, comes back to it later — or the panel is
restarted, which empties the storage outright — and taps Refresh:

  • decodeQuery fails, the admin gets a "no query" chat message, and the button
    keeps spinning: the tap is never resolved.
  • That message was the only feedback, so the reason for the dead tap is easy
    to miss among the bot's other messages.

It now answers with the same text, shown right where the tap happened, which
clears the indicator, and keeps the chat message: sendCallbackAnswerTgBot is a
single unretried call while SendMsgToTgbot retries connection errors, and the
only explanation for a dead button should not depend on one attempt succeeding.

The unroutable payloads

Three places dropped a tap on data the bot could not route:

  • the email-scoped switch (tgbot_router.go:329), reached by an admin's payload
    that carries arguments. An unknown action there falls out of the switch into a
    return that says nothing, and the two-token shape is exactly what an older
    panel version's buttons look like;
  • the default: fall-through of the switch that follows it
    (tgbot_router.go:1324): a payload that matches no case at all, including the
    single-token button names an older version used;
  • the admin decode path (tgbot_router.go:316): a payload longer than 64
    characters travels as a hash, and once that hash ages out of hashStorage
    (20 minutes, or a panel restart, which empties it) decodeQuery fails and the
    tap is left unresolved.

All three now answer with the bot's generic error text. The expired-hash path
also keeps the chat message it already sent, because sendCallbackAnswerTgBot
is one unretried call while SendMsgToTgbot retries connection errors — after a
restart that notice is the only explanation the admin gets.

The numeric-pad answers this PR originally carried are gone, as the review
thread says: add_client_limit_traffic_in, add_client_reset_exp_in and
add_client_ip_limit_in only fall out of their parse on a payload the bot itself
never generates, so the branch was unreachable and its test certified nothing.
The admin-only gate is left alone on purpose — refusing a non-admin silently is
the deliberate default-deny side of that check, and whether a client should be
told that a button exists but is not theirs is a product decision, not a routing
bug.

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
  • Database / migrations
  • Install / upgrade script
  • Docker image
  • Multi-node (sub-nodes)
  • Telegram bot

How was this tested?

internal/web/service/tgbot/tgbot_declined_callback_test.go drives
answerCallback as an admin against a mocked Telegram API, with isRunning set
so a posted message would be counted:

  • TestUnroutableCallbackIsAnswered — three payloads that match nothing, each
    answered with nothing at all before the change: no_such_callback and
    get_backup_legacy (single token, the shape an older panel's button names
    had), and add_client_legacy_step 5 (the two-token shape, which reaches the
    email-scoped switch). Asserts exactly one answer and zero posted messages.
  • TestExpiredCallbackHashIsAnsweredAndReported — a 32-hex payload that was
    never stored, with an empty hashStorage. Zero answers before the change; one
    answer and one notice after it.

The two-token case is what found the third path: it was the one case that stayed
red on the first push of this test, because that payload returns out of the
email-scoped switch before reaching the answer added for the other two.

Screenshots / recordings

N/A — bot callbacks, no UI.

Breaking changes

One feedback detail changes on the expired-hash path: it gains a callback answer
in addition to the chat notice it already sent, so the admin sees a toast as well
as the message. Nothing is removed, and no other callback changes.

Checklist

  • I tested the change locally and confirmed the described behavior. — no C compiler on this machine (the CGo SQLite driver needs one), so go test cannot run the packages importing internal/database, this one included; gofmt is clean and CI runs the new tests.
  • 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/6493 **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-answer-declined-callbacks` --- ### 📝 Commits (1) - [`29a14e9`](https://github.com/MHSanaei/3x-ui/commit/29a14e932afbaf5b30585a8681e8da8776daea27) fix(tgbot): answer the callbacks the bot cannot route ### 📊 Changes **2 files changed** (+92 additions, -0 deletions) <details> <summary>View changed files</summary> ➕ `internal/web/service/tgbot/tgbot_declined_callback_test.go` (+81 -0) 📝 `internal/web/service/tgbot/tgbot_router.go` (+11 -0) </details> ### 📄 Description ## Summary Three paths through `answerCallback` returned without calling `AnswerCallbackQuery`: a payload that matches no case in the email-scoped switch, one that matches nothing in the switch after it, and a button whose hash had aged out of the 20-minute storage, which replied with a chat message only. All three answer now, and the expired-hash path keeps its notice. ## Why Telegram keeps the tapped button in a loading state until the bot calls `answerCallbackQuery`; the Bot API documents answering as mandatory for every callback query. A callback that is never answered leaves that indicator running until the client gives up, and the user is told nothing about what happened. ### The reachable half: a button older than its payload Payloads longer than 64 characters are not sent as button data — `encodeQuery` replaces them with an MD5 hash kept in `hashStorage` (`tgbot.go:493`). That storage is in-memory and TTL-limited: `Start` creates it with a 20-minute expiration (`tgbot.go:245`) and a cron sweeps it every two minutes. Long payloads are ordinary, because the payload carries the client email: `client_individual_links <email>` crosses 64 characters for an email of about 40 (`tgbot_client.go:241`), `limit_traffic_in <email> ...` at about 43, and the same for every other per-client button. So an admin opens a client card, comes back to it later — or the panel is restarted, which empties the storage outright — and taps *Refresh*: - `decodeQuery` fails, the admin gets a "no query" chat message, and the button keeps spinning: the tap is never resolved. - That message was the *only* feedback, so the reason for the dead tap is easy to miss among the bot's other messages. It now answers with the same text, shown right where the tap happened, which clears the indicator, and keeps the chat message: `sendCallbackAnswerTgBot` is a single unretried call while `SendMsgToTgbot` retries connection errors, and the only explanation for a dead button should not depend on one attempt succeeding. ### The unroutable payloads Three places dropped a tap on data the bot could not route: - the email-scoped switch (`tgbot_router.go:329`), reached by an admin's payload that carries arguments. An unknown action there falls out of the switch into a `return` that says nothing, and the two-token shape is exactly what an older panel version's buttons look like; - the `default:` fall-through of the switch that follows it (`tgbot_router.go:1324`): a payload that matches no case at all, including the single-token button names an older version used; - the admin decode path (`tgbot_router.go:316`): a payload longer than 64 characters travels as a hash, and once that hash ages out of `hashStorage` (20 minutes, or a panel restart, which empties it) `decodeQuery` fails and the tap is left unresolved. All three now answer with the bot's generic error text. The expired-hash path also keeps the chat message it already sent, because `sendCallbackAnswerTgBot` is one unretried call while `SendMsgToTgbot` retries connection errors — after a restart that notice is the only explanation the admin gets. The numeric-pad answers this PR originally carried are gone, as the review thread says: `add_client_limit_traffic_in`, `add_client_reset_exp_in` and `add_client_ip_limit_in` only fall out of their parse on a payload the bot itself never generates, so the branch was unreachable and its test certified nothing. The admin-only gate is left alone on purpose — refusing a non-admin silently is the deliberate default-deny side of that check, and whether a client should be told that a button exists but is not theirs is a product decision, not a routing bug. ## 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) - [ ] Statistics / traffic counters - [ ] Database / migrations - [ ] Install / upgrade script - [ ] Docker image - [ ] Multi-node (sub-nodes) - [x] Telegram bot ## How was this tested? `internal/web/service/tgbot/tgbot_declined_callback_test.go` drives `answerCallback` as an admin against a mocked Telegram API, with `isRunning` set so a posted message would be counted: - `TestUnroutableCallbackIsAnswered` — three payloads that match nothing, each answered with nothing at all before the change: `no_such_callback` and `get_backup_legacy` (single token, the shape an older panel's button names had), and `add_client_legacy_step 5` (the two-token shape, which reaches the email-scoped switch). Asserts exactly one answer and zero posted messages. - `TestExpiredCallbackHashIsAnsweredAndReported` — a 32-hex payload that was never stored, with an empty `hashStorage`. Zero answers before the change; one answer and one notice after it. The two-token case is what found the third path: it was the one case that stayed red on the first push of this test, because that payload returns out of the email-scoped switch before reaching the answer added for the other two. ## Screenshots / recordings N/A — bot callbacks, no UI. ## Breaking changes One feedback detail changes on the expired-hash path: it gains a callback answer in addition to the chat notice it already sent, so the admin sees a toast as well as the message. Nothing is removed, and no other callback changes. ## Checklist - [ ] I tested the change locally and confirmed the described behavior. — no C compiler on this machine (the CGo SQLite driver needs one), so `go test` cannot run the packages importing `internal/database`, this one included; `gofmt` is clean and CI runs the new tests. - [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:50 +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#6306
No description provided.