mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[GH-ISSUE #6274] [Bug]: Telegram bot silently drops replies over 4096 chars when text has no \r\n\r\n separators (400: message is too long) #4255
Labels
No labels
backlog
bug
clarification needed
documentation
duplicate
enhancement
github_actions
good first issue
invalid
javascript
New Feature
PR welcomed
pull-request
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
MHSanaei/3x-ui#4255
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @Artepin on GitHub (Aug 20, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6274
Describe the bug
Bot buttons that reply with client-specific content (e.g. "individual
subscription links") can silently stop responding — no error visible in
Telegram, the button just appears dead.
Server log at the time of the click:
How to reproduce the problem
subscription links (or a few sufficiently long ones — e.g. VLESS/Reality
links with ECH/post-quantum ML-KEM768 params, which routinely run
800–1900+ characters each) that the batch sent by "individual
subscription links" exceeds Telegram's 4096-char limit. In practice this
is close to unconditional at the default batch size (see root cause
below): with the fixed overhead of the header/markup, the per-link
budget is well under the length of a minimal
vless://link with just aUUID.
(
client_individual_links/admin_client_individual_links).Expected behavior
The bot should page the reply into multiple messages, as
SendMsgToTgbot'spaging logic is apparently intended to do for any oversized reply.
Actual behavior
The message is sent once, whole, and rejected by the Telegram Bot API with
400: message is too long. The rejection is only logged server-side(
logger.Warning) — from the Telegram side, the button just does nothing.Relevant logs
3x-ui version
3.6.0
Xray-core version
26.7.28
How did you install 3x-ui?
install.sh script
Operating system
Ubuntu 24.04
Which parts of the panel are affected?
Telegram bot
Is the panel behind a reverse proxy or CDN?
No — direct access
Before submitting
@github-actions[bot] commented on GitHub (Aug 20, 2026):
Confirmed — both halves of your analysis hold against the current default branch (
19e71d9a), and the fixed-count batching is worse than "a handful of long links": withmaxPerMessage = 50the message exceeds Telegram's 4096 limit for essentially any realistic link length, not just Reality/ECH ones.The pager's contract.
SendMsgToTgbotpages on a\r\n\r\nboundary only:That separator is a real convention elsewhere in the package, which is why the pager works for other long replies.
getInboundUsagesends each inbound block withinfo.WriteString("\r\n")(internal/web/service/tgbot/tgbot_inbound.go:42) on top of a template that already ends in one —"expire": "📅 Expire Date: {{ .Time }}\r\n"(internal/web/translation/en-US.json:1987) — producing the\r\n\r\nthe split needs.Why this path never pages.
sendClientIndividualLinksemits single-\r\nseparators, and blank lines are stripped beforehand atinternal/web/service/tgbot/tgbot_client.go:304-309, so no\r\n\r\ncan occur:strings.Splitthen returns a one-element slice, the first loop iteration takes thelen(allMessages) == 0branch attgbot_send.go:100-102and appends the whole oversized string, and it goes out as onesendMessagecall — exactly the 400 in your log.Sizing, re-derived from the literals above. Each entry adds 15 bytes of markup (
<code>= 6,</code>\r\n= 9); the header is"Individual links"(internal/web/translation/en-US.json:90) plus":\r\n"= 19 bytes in en-US. At 50 links the fixed overhead is 769 bytes, leaving an average budget of ~66 bytes per link before 4096 is crossed — below the length of a minimalvless://link with a UUID alone. So a client with 50 links overflows unconditionally. Using your figures, 800-char links cross at 6 links and 1900-char links at 3. Note the panel's own cap islimit := 2000(tgbot_send.go:92), so these messages were meant to be paged well before Telegram ever sees them.A second, latent case in the same loop. Even when
\r\n\r\nseparators are present, a single segment longer thanlimitis appended whole and never split:One caveat on the fallback you describe:
ParseMode: "HTML"is set attgbot_send.go:117, so a hard character cut that lands inside<code>…</code>turns the 400 intocan't parse entitiesrather than fixing it. Any hard splitter needs to be tag-aware, or the split has to happen at the call site where the entry boundaries are still known.Verdict: Confirmed bug
Severity: High
Confidence: High
Root cause:
SendMsgToTgbot(internal/web/service/tgbot/tgbot_send.go:96-106) pages only on a\r\n\r\nboundary and never hard-splits, so a message with no such boundary — or a single boundary-delimited segment overlimit— is sent whole. Triggered bysendClientIndividualLinks(internal/web/service/tgbot/tgbot_client.go:316-330), which batches by a fixed count of 50 and separates entries with a single\r\n. The count-based batching arrived withed96fa09("tgbot: subscription,qrcode, link", 2025-09-14); the\r\n\r\n-only pager predates it (ceee1e42, 2024-01-01). Both files moved to their current paths in41645255(#5167) without behaviour change.Already fixed: Still present on the default branch at
19e71d9a;git log -- internal/web/service/tgbot/tgbot_send.go internal/web/service/tgbot/tgbot_client.goshows no change to either since v3.6.0.Duplicate of: None. No other open or closed issue matches on
gh search issues --repo MHSanaei/3x-ui "message is too long"or--search "telegram message long" --state all.Evidence:
tgbot_send.go:92limit := 2000;tgbot_send.go:96messages := strings.Split(msg, "\r\n\r\n");tgbot_send.go:100-102appends an oversized segment unsplit;tgbot_send.go:117ParseMode: "HTML";tgbot_client.go:304-309strips blank lines so no\r\n\r\ncan form;tgbot_client.go:316const maxPerMessage = 50;tgbot_client.go:322/:327the single-\r\nseparators;tgbot_inbound.go:42+en-US.json:1987show the\r\n\r\nconvention the pager relies on. No test ininternal/web/service/tgbot/*_test.goreferencesSendMsgToTgbotor the paging, so nothing pins the current behaviour.Not determined: Your Environment section reads "Same as above" with nothing above it, so I do not have your 3x-ui version — it does not change the verdict, since the defect is present at the tip. Whether Telegram's 4096 cap is applied before or after HTML entity parsing would only shift the exact overflow threshold, not whether it is crossed; sending one 50-link message would settle it.
Suggested fix: The smallest change that removes the root cause is at the call site — accumulate by byte length in
sendClientIndividualLinksand flush the builder before it crosses the pager'slimit, instead of counting to 50. A generic hard-split fallback inSendMsgToTgbotwould additionally cover the oversized-segment case attgbot_send.go:100-102, but it has to preserve<code>tag boundaries underParseMode: "HTML".@MHSanaei
This reply was generated automatically; a maintainer may follow up.
@github-actions[bot] commented on GitHub (Aug 20, 2026):
Confirmed on the current default branch (
58669f61), and the fix belongs in the pager, not at the call site:SendMsgToTgbotsplits only on\r\n\r\nand never enforces its own limit, so any message without that boundary — or any single boundary-delimited block over the limit — is handed to Telegram whole.Root cause
internal/web/service/tgbot/tgbot_send.go:91-112:strings.Spliton a string with no\r\n\r\nreturns a one-element slice; the single loop iteration takes thelen(allMessages) == 0branch and appends the oversized string unchanged.limitis never enforced on a segment — it only decides whether two segments may be merged.Most senders happen to satisfy the separator by accident, which is why this looks like it works elsewhere:
tgbot.messages.refreshedOnends in\r\n\r\n(internal/web/translation/en-US.json:2003), andgetInboundUsagesadds its owninfo.WriteString("\r\n")on top of a template that already ends in one (internal/web/service/tgbot/tgbot_inbound.go:42+en-US.json:1987).sendClientIndividualLinksis the one path that produces none. Blank lines are stripped atinternal/web/service/tgbot/tgbot_client.go:302-309, and the batch is then assembled with single-\r\nseparators (tgbot_client.go:315-330):So
\r\n\r\ncannot occur, and the whole batch goes out as onesendMessage— the 400 in your log.On sizing: the Bot API counts the 4096 cap after entity parsing, so the
<code>markup itself does not count, but the links do. With the header plus one newline per entry, 50 links overflow once the average link exceeds ~81 characters — below a minimalvless://link carrying just a UUID, host and port. The reporter's figures follow: 800-char links cross at 6 entries, 1900-char links at 3. The panel's ownlimit := 2000should have paged all of these long before Telegram ever saw them.Fix
Replace
tgbot_send.go:91-112with a single call, and add the two helpers alongside it. Splitting on\r\nrather than at an arbitrary offset matters:ParseMode: "HTML"is set attgbot_send.go:117, so a cut landing inside<code>…</code>would trade the 400 forcan't parse entities.Notes on the shape:
splitOnLinesonly starts a new page when the previous one could not take the next line — so a\r\nseparator is never silently promoted to\r\n\r\n. Content is preserved exactly.len(msg) <= limitis unchanged: one page, verbatim.+len("\r\n\r\n")term also fixes the small existing miscount at the old line 100, which merged two segments without counting the separator it then inserted.splitOnLinesruns over every block, this also covers the second, latent case: a\r\n\r\n-delimited segment that is on its own longer thanlimit.tgbot_client.go:316.maxPerMessage = 50becomes harmless — each batch is now paged into ~2000-byte messages. Dropping the count-based chunking entirely would work too, but it is not needed for the fix.Residual, deliberately not handled: a single line longer than the limit still goes out whole. That is a link over ~4096 characters, which Telegram cannot accept in one message under any splitting scheme, and half a link split across two messages is not usable anyway.
Test
internal/web/service/tgbot/tgbot_send_test.go— the helpers are pure, so this drives the real paging code:To watch it go red rather than merely fail to build, temporarily replace
splitOnLines's body withreturn []string{block}— that is exactly the old behaviour, and both tests fail on the length assertion.Adjacent, out of scope
The retry loop at
tgbot_send.go:124-151only retries onconnection/timeout/closedsubstrings, andtelego.NewBotis built without a rate-limiter option (internal/web/service/tgbot/tgbot.go:426-433). A 50-link batch now becomes ~9 messages at 100 ms spacing, so a429: Too Many Requestswould be logged and dropped the same way the 400 is today. That is a separate pre-existing gap — worth a look, but not part of this fix.I only investigate and reply here; I cannot open a PR or push a branch, so the blocks above are meant to be applied by hand.
🤖 Generated with Claude Code