[PR #6518] [MERGED] feat(tgbot): access levels and /start account binding #6325

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6518
Author: @pcxzs
Created: 9/14/2026
Status: ✅ Merged
Merged: 9/27/2026
Merged by: @MHSanaei

Base: main ← Head: feat/tgbot-access-binding


📝 Commits (6)

  • 83447c2 feat(tgbot): gate the bot behind three user levels
  • 41200f1 feat(tgbot): bind Telegram accounts through /start deep links
  • 750e154 fix(tgbot): harden invite claims after review
  • c6a08b2 Merge remote-tracking branch 'origin/main' into pr/6518
  • 224a715 fix(tgbot): answer group chats again and make the claim race test bite
  • 34b0a7a docs(tgbot): document access levels and invite links

📊 Changes

25 files changed (+801 additions, -11 deletions)

View changed files

📝 docs/content/docs/en/operations/telegram-bot.mdx (+11 -2)
📝 docs/content/docs/fa/operations/telegram-bot.mdx (+11 -2)
📝 docs/content/docs/ru/operations/telegram-bot.mdx (+12 -2)
📝 docs/content/docs/zh/operations/telegram-bot.mdx (+9 -2)
📝 frontend/src/pages/clients/ClientFormModal.tsx (+4 -1)
📝 internal/web/service/client_lookup.go (+11 -0)
📝 internal/web/service/tgbot/tgbot_client.go (+1 -0)
➕ internal/web/service/tgbot/tgbot_invite.go (+237 -0)
➕ internal/web/service/tgbot/tgbot_invite_test.go (+221 -0)
➕ internal/web/service/tgbot/tgbot_level.go (+72 -0)
➕ internal/web/service/tgbot/tgbot_level_test.go (+100 -0)
📝 internal/web/service/tgbot/tgbot_router.go (+21 -2)
📝 internal/web/translation/ar-EG.json (+7 -0)
📝 internal/web/translation/en-US.json (+7 -0)
📝 internal/web/translation/es-ES.json (+7 -0)
📝 internal/web/translation/fa-IR.json (+7 -0)
📝 internal/web/translation/id-ID.json (+7 -0)
📝 internal/web/translation/ja-JP.json (+7 -0)
📝 internal/web/translation/pt-BR.json (+7 -0)
📝 internal/web/translation/ru-RU.json (+7 -0)

...and 5 more files

📄 Description

Summary

Gives the Telegram bot access levels and a way for a customer to link their own Telegram account to their client, via a /start deep link built from the client's subId. This is the first part of #6428, split as suggested there: access levels and account binding first, standing on their own against current main.

Why

Any Telegram account that finds the bot is treated the same as a customer an admin has bound. Binding is also manual: the customer sends /id, and the admin copies the ChatID into the client. Both have to change before the bot can face customers, and everything else in #6428 builds on them.

What changed

Access levels. Each update resolves to stranger, client or admin (tgbot_level.go):

  • Commands are allowlisted per level, so a command added later stays admin-only until it is listed.
  • A stranger may run /start and /id, nothing else. /start answers with the existing askToAddUserId text (their ChatID), so manual binding still works. A stranger's callback taps are answered and dropped.
  • A client keeps exactly the commands a non-admin could meaningfully use before: /start /help /status /id /usage.
  • The level comes from the indexed tg_id column of the clients table, so no update, a stranger's included, triggers a scan of the inbound JSON. Ownership of a specific client is still #6489's clientOwnedByTgUser, unchanged.

Private chats only. The bot ignores updates from groups and channels. Authorization keys on the sender, but wizard state keys on the chat, and those are the same identity only in a private chat. A button tapped in a group is still answered so it stops spinning, and each ignored chat is logged once so an operator can see why the bot is quiet there.

Binding. The admin client card gets an Invite Link button next to Set Telegram User. It produces https://t.me/<bot>?start=<payload>, and the first account to open it is bound through the existing SetClientTelegramUserID:

  • Payload: the subId, base64url-encoded, because Telegram accepts only A-Za-z0-9_- (up to 64 characters) and a subId may contain #, & or non-ASCII. A subId too long to fit is refused with a log line rather than truncated.
  • Concurrent claims: serialized, so of several accounts opening the same link at once exactly one is bound and told so.
  • Several clients: a subscription spanning several clients binds all of them, and is refused if any part belongs to another account. If one bind fails part-way, the ones already made are undone.
  • Reuse and probing: re-opening your own link is idempotent. Unknown and already-claimed tokens get the same reply.
  • Guessing: non-admin claim attempts are limited to 5 per Telegram account per hour. The first refused attempt notifies the admins with the account's name and ID. Plain /start without a payload is not counted.
  • Client form: the Subscription ID field gets a tooltip explaining that it doubles as the bot invite code, so a short or guessable one can be claimed by someone else.
  • Unbinding: stays where it is today, on Set Telegram User.

Whether the subId should double as the invite token at all is an open question for review; see the comment below.

Scope

  • Outside tgbot/:
    • One read-only lookup, ClientService.GetRecordsBySubID in client_lookup.go.
    • One tooltip prop on the Subscription ID field in ClientFormModal.tsx.
  • Not touched: routes, settings, migrations, Xray config.
  • Locales: 7 new keys, in all 13 files.
  • Size: 21 files, ~830 lines (378 code, 353 tests, 91 locale, 4 frontend).

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?

New tests:

  • tgbot_level_test.go:
    • The allowlist, in both directions.
    • Ignored chat types.
    • levelOf against a seeded binding.
    • gateCommand: a stranger gets no reply, a client gets "unknown command".
    • gateCallback: a stranger's forged tap is answered and dropped.
  • tgbot_invite_test.go:
    • Token resolution: unclaimed, trimmed, idempotent owner, taken, shared subscription, partly owned, partly held by another, unknown, empty, missing sender.
    • An end-to-end claim that promotes a stranger to client.
    • Concurrent claims: 6 accounts, exactly one told it bound and holding the client. This fails without the claim lock.
    • A payload round trip for subIds containing #, &, ? and non-ASCII, plus the 64-character limit.
    • The attempt limit: the 6th attempt is refused, admins are notified once per window, other accounts are unaffected, and the window resets. It fails if the limit is removed.
    • HTML escaping of the account name in the admin notice.

These reuse the existing staleButtonServer / newLinksCallbackTgbot fixtures.

Checks run:

  • Go: gofmt, go vet ./..., golangci-lint, go test -race -shuffle=on ./internal/web/service/..., go test -shuffle=on ./....
  • Frontend: the i18n dead-keys test, npm run typecheck, npm run lint (oxlint) and oxfmt --check.

Breaking changes

None for bound customers or admins in private chats. Two visible changes:

  • Unbound accounts now get only /start (showing their ChatID) and /id. Previously they could open the customer menu, whose every button answered "configuration not found".
  • Commands and taps sent in a group chat are ignored, with a one-time log line per chat. Notifications the bot sends to a group chat ID are unaffected.

Checklist

  • I tested the change locally and confirmed the described behavior.
  • I added or updated tests for the new behavior (when applicable).
  • go build ./... and the test suite pass locally.
  • For frontend changes: npm run lint, npm run typecheck, and npm run build pass.
  • I updated the Wiki / README / API docs if user-facing behavior changed.
  • 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/6518 **Author:** [@pcxzs](https://github.com/pcxzs) **Created:** 9/14/2026 **Status:** ✅ Merged **Merged:** 9/27/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `feat/tgbot-access-binding` --- ### 📝 Commits (6) - [`83447c2`](https://github.com/MHSanaei/3x-ui/commit/83447c23e48b9e76ab3c2ef349a57f96dd58f6d4) feat(tgbot): gate the bot behind three user levels - [`41200f1`](https://github.com/MHSanaei/3x-ui/commit/41200f18dc8c9d69f23073e6b900c56e17bf65b4) feat(tgbot): bind Telegram accounts through /start deep links - [`750e154`](https://github.com/MHSanaei/3x-ui/commit/750e15407cd93bdc19c602ddf6f644013ac374b4) fix(tgbot): harden invite claims after review - [`c6a08b2`](https://github.com/MHSanaei/3x-ui/commit/c6a08b22334853f055758df8919da52ae1139ffe) Merge remote-tracking branch 'origin/main' into pr/6518 - [`224a715`](https://github.com/MHSanaei/3x-ui/commit/224a7157f31f10b9dc5cf4d470fa6e8f44653f38) fix(tgbot): answer group chats again and make the claim race test bite - [`34b0a7a`](https://github.com/MHSanaei/3x-ui/commit/34b0a7aa86165e1b0f6b7b8caaf7eed39fdd4684) docs(tgbot): document access levels and invite links ### 📊 Changes **25 files changed** (+801 additions, -11 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/en/operations/telegram-bot.mdx` (+11 -2) 📝 `docs/content/docs/fa/operations/telegram-bot.mdx` (+11 -2) 📝 `docs/content/docs/ru/operations/telegram-bot.mdx` (+12 -2) 📝 `docs/content/docs/zh/operations/telegram-bot.mdx` (+9 -2) 📝 `frontend/src/pages/clients/ClientFormModal.tsx` (+4 -1) 📝 `internal/web/service/client_lookup.go` (+11 -0) 📝 `internal/web/service/tgbot/tgbot_client.go` (+1 -0) ➕ `internal/web/service/tgbot/tgbot_invite.go` (+237 -0) ➕ `internal/web/service/tgbot/tgbot_invite_test.go` (+221 -0) ➕ `internal/web/service/tgbot/tgbot_level.go` (+72 -0) ➕ `internal/web/service/tgbot/tgbot_level_test.go` (+100 -0) 📝 `internal/web/service/tgbot/tgbot_router.go` (+21 -2) 📝 `internal/web/translation/ar-EG.json` (+7 -0) 📝 `internal/web/translation/en-US.json` (+7 -0) 📝 `internal/web/translation/es-ES.json` (+7 -0) 📝 `internal/web/translation/fa-IR.json` (+7 -0) 📝 `internal/web/translation/id-ID.json` (+7 -0) 📝 `internal/web/translation/ja-JP.json` (+7 -0) 📝 `internal/web/translation/pt-BR.json` (+7 -0) 📝 `internal/web/translation/ru-RU.json` (+7 -0) _...and 5 more files_ </details> ### 📄 Description ## Summary Gives the Telegram bot access levels and a way for a customer to link their own Telegram account to their client, via a `/start` deep link built from the client's subId. This is the first part of #6428, split as suggested there: access levels and account binding first, standing on their own against current `main`. ## Why Any Telegram account that finds the bot is treated the same as a customer an admin has bound. Binding is also manual: the customer sends `/id`, and the admin copies the ChatID into the client. Both have to change before the bot can face customers, and everything else in #6428 builds on them. ## What changed **Access levels.** Each update resolves to stranger, client or admin (`tgbot_level.go`): - Commands are **allowlisted** per level, so a command added later stays admin-only until it is listed. - A stranger may run `/start` and `/id`, nothing else. `/start` answers with the existing `askToAddUserId` text (their ChatID), so manual binding still works. A stranger's callback taps are answered and dropped. - A client keeps exactly the commands a non-admin could meaningfully use before: `/start /help /status /id /usage`. - The level comes from the indexed `tg_id` column of the clients table, so no update, a stranger's included, triggers a scan of the inbound JSON. Ownership of a specific client is still #6489's `clientOwnedByTgUser`, unchanged. **Private chats only.** The bot ignores updates from groups and channels. Authorization keys on the sender, but wizard state keys on the chat, and those are the same identity only in a private chat. A button tapped in a group is still answered so it stops spinning, and each ignored chat is logged once so an operator can see why the bot is quiet there. **Binding.** The admin client card gets an **Invite Link** button next to *Set Telegram User*. It produces `https://t.me/<bot>?start=<payload>`, and the first account to open it is bound through the existing `SetClientTelegramUserID`: - **Payload:** the subId, base64url-encoded, because Telegram accepts only `A-Za-z0-9_-` (up to 64 characters) and a subId may contain `#`, `&` or non-ASCII. A subId too long to fit is refused with a log line rather than truncated. - **Concurrent claims:** serialized, so of several accounts opening the same link at once exactly one is bound and told so. - **Several clients:** a subscription spanning several clients binds all of them, and is refused if any part belongs to another account. If one bind fails part-way, the ones already made are undone. - **Reuse and probing:** re-opening your own link is idempotent. Unknown and already-claimed tokens get the same reply. - **Guessing:** non-admin claim attempts are limited to 5 per Telegram account per hour. The first refused attempt notifies the admins with the account's name and ID. Plain `/start` without a payload is not counted. - **Client form:** the *Subscription ID* field gets a tooltip explaining that it doubles as the bot invite code, so a short or guessable one can be claimed by someone else. - **Unbinding:** stays where it is today, on *Set Telegram User*. Whether the subId should double as the invite token at all is an open question for review; see the comment below. ## Scope - **Outside `tgbot/`:** - One read-only lookup, `ClientService.GetRecordsBySubID` in `client_lookup.go`. - One `tooltip` prop on the Subscription ID field in `ClientFormModal.tsx`. - **Not touched:** routes, settings, migrations, Xray config. - **Locales:** 7 new keys, in all 13 files. - **Size:** 21 files, ~830 lines (378 code, 353 tests, 91 locale, 4 frontend). ## Type of change - [ ] Bug fix - [x] New feature - [ ] Refactoring (no behavior change) - [ ] Documentation - [ ] Tests only - [ ] Build / CI / tooling - [ ] Other ## Areas affected - [x] 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? New tests: - `tgbot_level_test.go`: - The allowlist, in both directions. - Ignored chat types. - `levelOf` against a seeded binding. - `gateCommand`: a stranger gets no reply, a client gets "unknown command". - `gateCallback`: a stranger's forged tap is answered and dropped. - `tgbot_invite_test.go`: - Token resolution: unclaimed, trimmed, idempotent owner, taken, shared subscription, partly owned, partly held by another, unknown, empty, missing sender. - An end-to-end claim that promotes a stranger to client. - Concurrent claims: 6 accounts, exactly one told it bound and holding the client. This fails without the claim lock. - A payload round trip for subIds containing `#`, `&`, `?` and non-ASCII, plus the 64-character limit. - The attempt limit: the 6th attempt is refused, admins are notified once per window, other accounts are unaffected, and the window resets. It fails if the limit is removed. - HTML escaping of the account name in the admin notice. These reuse the existing `staleButtonServer` / `newLinksCallbackTgbot` fixtures. Checks run: - Go: `gofmt`, `go vet ./...`, `golangci-lint`, `go test -race -shuffle=on ./internal/web/service/...`, `go test -shuffle=on ./...`. - Frontend: the i18n dead-keys test, `npm run typecheck`, `npm run lint` (oxlint) and `oxfmt --check`. ## Breaking changes None for bound customers or admins in private chats. Two visible changes: - **Unbound accounts** now get only `/start` (showing their ChatID) and `/id`. Previously they could open the customer menu, whose every button answered "configuration not found". - **Commands and taps sent in a group chat** are ignored, with a one-time log line per chat. Notifications the bot *sends* to a group chat ID are unaffected. ## Checklist - [ ] I tested the change locally and confirmed the described behavior. - [x] I added or updated tests for the new behavior (when applicable). - [x] `go build ./...` and the test suite pass locally. - [ ] For frontend changes: `npm run lint`, `npm run typecheck`, and `npm run build` pass. - [ ] I updated the Wiki / README / API docs if user-facing behavior changed. - [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:57 +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#6325
No description provided.