[GH-ISSUE #5805] [Bug]: Telegram bot usage lookup fails when tgId JSON is compacted #4035

Closed
opened 2026-10-31 20:36:41 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @menzhu on GitHub (Jul 5, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/5805

Describe the bug

When a client has a Telegram user ID, the Telegram bot usage query can fail after the inbound settings JSON is compacted during node sync/import.

The client row and traffic row exist, but the bot returns no result because the lookup only matches "tgId": 123 with a space after the colon. If the JSON contains "tgId":123, the inbound is not found.

How to reproduce the problem

  1. Configure a client with a Telegram user ID.

  2. Sync/import or otherwise save the inbound so inbounds.settings contains compact JSON like:

    {"clients":[{"email":"client-a","tgId":123456789}]}
    
  3. Make sure the client and traffic rows exist, for example clients.tg_id = 123456789 and client_traffics.email = 'client-a'.

  4. Send a usage query to the Telegram bot from that Telegram user.

  5. The bot replies that no configuration was found. Logs show:

    WARNING - No ClientTraffic records found for emails: []
    

Expected behavior

The Telegram bot should find the client by Telegram user ID regardless of JSON whitespace/serialization style.

Ideally the query should use the normalized clients.tg_id column or SQLite JSON functions, instead of matching a formatted JSON string.

Actual behavior

The bot fails to find the inbound when inbounds.settings contains compact JSON:

"tgId":123456789

The current code appears to require a space after the colon:

// internal/web/service/inbound_traffic.go
db.Model(model.Inbound{}).Where("settings LIKE ?", fmt.Sprintf(`%%"tgId": %d%%`, tgId)).Find(&inbounds)

That pattern matches "tgId": 123456789 but not "tgId":123456789. Reformatting the JSON to include the space makes the bot work immediately.

Relevant logs


Screenshots

Sanitized verification:

-- compact JSON exists after sync/import
select id, remark from inbounds where settings like '%"tgId":123456789%';
-- returns the remote/synced inbound

-- current bot lookup pattern
select id, remark from inbounds where settings like '%"tgId": 123456789%';
-- returns no rows until the JSON is reformatted

Workaround tested: reformat settings so "tgId":123456789 becomes "tgId": 123456789; after that the Telegram bot can return the usage data.

3x-ui version

3.4.2 (Docker image ghcr.io/mhsanaei/3x-ui:latest)

Xray-core version

26.6.27

How did you install 3x-ui?

install.sh script

Operating system

Debian 12 (bookworm), Docker

Which parts of the panel are affected?

No response

Browser (only if it is a UI bug)

Not browser-specific; reproduced via Telegram bot and SQLite data

Is the panel behind a reverse proxy or CDN?

None

Before submitting

  • I searched existing issues and this bug has not been reported.
  • I am running the latest released version of 3x-ui (or have verified the bug still exists on it).
  • This bug report is written in English.
  • I have redacted any sensitive data (tokens, real domains, client UUIDs).
Originally created by @menzhu on GitHub (Jul 5, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/5805 ### Describe the bug When a client has a Telegram user ID, the Telegram bot usage query can fail after the inbound settings JSON is compacted during node sync/import. The client row and traffic row exist, but the bot returns no result because the lookup only matches `"tgId": 123` with a space after the colon. If the JSON contains `"tgId":123`, the inbound is not found. ### How to reproduce the problem 1. Configure a client with a Telegram user ID. 2. Sync/import or otherwise save the inbound so `inbounds.settings` contains compact JSON like: ```json {"clients":[{"email":"client-a","tgId":123456789}]} ``` 3. Make sure the client and traffic rows exist, for example `clients.tg_id = 123456789` and `client_traffics.email = 'client-a'`. 4. Send a usage query to the Telegram bot from that Telegram user. 5. The bot replies that no configuration was found. Logs show: ```text WARNING - No ClientTraffic records found for emails: [] ``` ### Expected behavior The Telegram bot should find the client by Telegram user ID regardless of JSON whitespace/serialization style. Ideally the query should use the normalized `clients.tg_id` column or SQLite JSON functions, instead of matching a formatted JSON string. ### Actual behavior The bot fails to find the inbound when `inbounds.settings` contains compact JSON: ```json "tgId":123456789 ``` The current code appears to require a space after the colon: ```go // internal/web/service/inbound_traffic.go db.Model(model.Inbound{}).Where("settings LIKE ?", fmt.Sprintf(`%%"tgId": %d%%`, tgId)).Find(&inbounds) ``` That pattern matches `"tgId": 123456789` but not `"tgId":123456789`. Reformatting the JSON to include the space makes the bot work immediately. ### Relevant logs ```shell ``` ### Screenshots Sanitized verification: ```sql -- compact JSON exists after sync/import select id, remark from inbounds where settings like '%"tgId":123456789%'; -- returns the remote/synced inbound -- current bot lookup pattern select id, remark from inbounds where settings like '%"tgId": 123456789%'; -- returns no rows until the JSON is reformatted ``` Workaround tested: reformat `settings` so `"tgId":123456789` becomes `"tgId": 123456789`; after that the Telegram bot can return the usage data. ### 3x-ui version 3.4.2 (Docker image ghcr.io/mhsanaei/3x-ui:latest) ### Xray-core version 26.6.27 ### How did you install 3x-ui? install.sh script ### Operating system Debian 12 (bookworm), Docker ### Which parts of the panel are affected? _No response_ ### Browser (only if it is a UI bug) Not browser-specific; reproduced via Telegram bot and SQLite data ### Is the panel behind a reverse proxy or CDN? None ### Before submitting - [x] I searched [existing issues](../issues?q=is%3Aissue) and this bug has not been reported. - [x] I am running the latest released version of 3x-ui (or have verified the bug still exists on it). - [x] This bug report is written in English. - [x] I have redacted any sensitive data (tokens, real domains, client UUIDs).
bsakaguchi 2026-10-31 20:36:41 +07:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@github-actions[bot] commented on GitHub (Jul 5, 2026):

Confirmed in the source. GetClientTrafficTgBot in internal/web/service/inbound_traffic.go (line 843) narrows candidate inbounds with:

db.Model(model.Inbound{}).Where("settings LIKE ?", fmt.Sprintf(`%%"tgId": %d%%`, tgId)).Find(&inbounds)

The %%"tgId": %d%% pattern requires a literal space after the colon, so it does not match compact JSON such as "tgId":123456789. Since this is only a pre-filter — for each matched inbound the code does call GetClients(inbound) (internal/web/service/inbound.go:424, plain json.Unmarshal) and then exact-compares client.TgID == tgId — an inbound that fails the LIKE step is never even fetched, so the later exact match never gets a chance to run. This is called from three places in internal/web/service/tgbot/ (tgbot_router.go, tgbot_client.go, tgbot_report.go), so any of those bot flows (usage query, client menu, scheduled report) can hit it whenever inbounds.settings has been serialized without the space, e.g. after node sync/import as described.

Your suggestion to use the normalized column/JSON functions instead of a formatted-string match is consistent with how the codebase already avoids this class of bug elsewhere:

  • model.ClientRecord (internal/database/model/model.go:620, table clients) already stores a normalized tg_id column (gorm:"column:tg_id"), populated from the same Client.TgID field.
  • GetAllEmails (internal/web/service/inbound.go:445) already queries inbounds.settings via driver-portable JSON helpers (database.JSONClientsFromInbound() / JSONFieldText, internal/database/dialect.go) — JSON_EXTRACT/JSON_EACH on SQLite, jsonb_array_elements/->> on Postgres — instead of a LIKE-on-formatted-JSON pattern.

So there is existing precedent for fixing this either by querying clients.tg_id directly (if that table is reliably kept in sync for this lookup path) or by switching the inbound pre-filter to the same JSON-function helpers used by GetAllEmails, rather than widening the LIKE pattern to tolerate both spacing styles.

I've labeled this a confirmed bug; no further information is needed to reproduce or diagnose it.

This reply was generated automatically. A maintainer may follow up.

<!-- gh-comment-id:4884978611 --> @github-actions[bot] commented on GitHub (Jul 5, 2026): Confirmed in the source. `GetClientTrafficTgBot` in `internal/web/service/inbound_traffic.go` (line 843) narrows candidate inbounds with: ```go db.Model(model.Inbound{}).Where("settings LIKE ?", fmt.Sprintf(`%%"tgId": %d%%`, tgId)).Find(&inbounds) ``` The `%%"tgId": %d%%` pattern requires a literal space after the colon, so it does not match compact JSON such as `"tgId":123456789`. Since this is only a pre-filter — for each matched inbound the code does call `GetClients(inbound)` (`internal/web/service/inbound.go:424`, plain `json.Unmarshal`) and then exact-compares `client.TgID == tgId` — an inbound that fails the LIKE step is never even fetched, so the later exact match never gets a chance to run. This is called from three places in `internal/web/service/tgbot/` (`tgbot_router.go`, `tgbot_client.go`, `tgbot_report.go`), so any of those bot flows (usage query, client menu, scheduled report) can hit it whenever `inbounds.settings` has been serialized without the space, e.g. after node sync/import as described. Your suggestion to use the normalized column/JSON functions instead of a formatted-string match is consistent with how the codebase already avoids this class of bug elsewhere: - `model.ClientRecord` (`internal/database/model/model.go:620`, table `clients`) already stores a normalized `tg_id` column (`gorm:"column:tg_id"`), populated from the same `Client.TgID` field. - `GetAllEmails` (`internal/web/service/inbound.go:445`) already queries `inbounds.settings` via driver-portable JSON helpers (`database.JSONClientsFromInbound()` / `JSONFieldText`, `internal/database/dialect.go`) — `JSON_EXTRACT`/`JSON_EACH` on SQLite, `jsonb_array_elements`/`->>` on Postgres — instead of a LIKE-on-formatted-JSON pattern. So there is existing precedent for fixing this either by querying `clients.tg_id` directly (if that table is reliably kept in sync for this lookup path) or by switching the inbound pre-filter to the same JSON-function helpers used by `GetAllEmails`, rather than widening the LIKE pattern to tolerate both spacing styles. I've labeled this a confirmed bug; no further information is needed to reproduce or diagnose it. *This reply was generated automatically. A maintainer may follow up.*
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#4035
No description provided.