[GH-ISSUE #5188] [Feature]: Telegram bot notifications for outbound status changes (up/down) via Observatory #3640

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

Originally created by @Ssentiago on GitHub (Jun 10, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/5188

There is no proactive alerting when an outbound goes down. The only way to detect a failed outbound is to manually check the panel UI or wait for client complaints. For multi-node setups (e.g. cascaded exit nodes across different countries) this creates a blind spot — an outbound can be dead for hours without anyone noticing.

Describe the solution you'd like

Telegram bot notifications when an outbound changes its status, leveraging the existing Observatory/BurstObservatory data that Xray-core already collects and the panel already queries for the UI.

Minimal behavior:

  • Notify when an outbound transitions alive → dead
  • Notify when it recovers dead → alive
  • Include outbound tag and timestamp in the message

Optional / configurable:

  • Per-outbound notification toggle
  • Cooldown to avoid notification storms on flapping outbounds
  • Option to suppress recovery notifications

Alternatives you've considered

A custom script polling /panel/api/server/xrayObservatory and sending Telegram messages directly. Works, but requires maintaining a separate process and running it as a cronjob or service. The panel already has the bot, the Observatory data, and the notification infrastructure — native integration is cleaner.

Concrete use case

A panel with multiple outbound nodes — when one goes dead, the admin should get a Telegram message immediately rather than discovering it from client complaints.

Mockups, screenshots, or examples

No response

Which parts of the panel would this affect?

Telegram bot

Are you willing to help?

  • I'd like to implement this feature myself and open a PR.
  • I can help test once a PR is open.

Before submitting

  • I searched existing issues and this feature has not been requested.
  • This feature request is written in English.
Originally created by @Ssentiago on GitHub (Jun 10, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/5188 ### Is your feature request related to a problem? There is no proactive alerting when an outbound goes down. The only way to detect a failed outbound is to manually check the panel UI or wait for client complaints. For multi-node setups (e.g. cascaded exit nodes across different countries) this creates a blind spot — an outbound can be dead for hours without anyone noticing. ### Describe the solution you'd like Telegram bot notifications when an outbound changes its status, leveraging the existing Observatory/BurstObservatory data that Xray-core already collects and the panel already queries for the UI. Minimal behavior: - Notify when an outbound transitions alive → dead - Notify when it recovers dead → alive - Include outbound tag and timestamp in the message Optional / configurable: - Per-outbound notification toggle - Cooldown to avoid notification storms on flapping outbounds - Option to suppress recovery notifications ### Alternatives you've considered A custom script polling /panel/api/server/xrayObservatory and sending Telegram messages directly. Works, but requires maintaining a separate process and running it as a cronjob or service. The panel already has the bot, the Observatory data, and the notification infrastructure — native integration is cleaner. ### Concrete use case A panel with multiple outbound nodes — when one goes dead, the admin should get a Telegram message immediately rather than discovering it from client complaints. ### Mockups, screenshots, or examples _No response_ ### Which parts of the panel would this affect? Telegram bot ### Are you willing to help? - [x] I'd like to implement this feature myself and open a PR. - [x] I can help test once a PR is open. ### Before submitting - [x] I searched [existing issues](../issues?q=is%3Aissue) and this feature has not been requested. - [x] This feature request is written in English.
Author
Owner

@github-actions[bot] commented on GitHub (Jun 10, 2026):

Thanks for the detailed request and for volunteering to implement it — the use case is clear and the panel already has every building block in place.

What's already there

Component File Notes
Observatory snapshot internal/web/service/xray_metrics.go:70 ObservatorySnapshot() returns []ObsTagSnapshot{Tag, Alive, Delay, LastSeenTime, …}
State update loop xray_metrics.go:185 applyObservatory() Called every 2 s from server.go:startTask() — old map is in s.obsByTag, new map is in next before they are swapped at line 213
Telegram broadcast internal/web/service/tgbot/tgbot_send.go:160 SendMsgToTgbotAdmins(msg)
Boolean-toggle pattern internal/web/entity/entity.go:49 + setting.go:448 TgBotLoginNotify / GetTgBotLoginNotify() — exact same shape needed here
Cron job pattern internal/web/job/check_cpu_usage.go settingService + tgbotService dependencies, registered in web.go:startTask()
i18n message keys internal/web/translation/en-US.json:1571 cpuThreshold, loginSuccess, etc. — 13 locale files to update

Suggested implementation path

applyObservatory() already has both the old state (s.obsByTag) and the new state (next) in scope before the swap. Add an optional callback:

// XrayMetricsService — new field
aliveChangeCallback func(tag string, wasAlive, isAlive bool)

func (s *XrayMetricsService) SetAliveChangeCallback(fn func(tag, wasAlive, isAlive bool)) {
    s.mu.Lock()
    s.aliveChangeCallback = fn
    s.mu.Unlock()
}

Inside applyObservatory(), after building next and before s.obsByTag = next:

if s.aliveChangeCallback \!= nil {
    for tag, snap := range next {
        old, existed := s.obsByTag[tag]
        wasAlive := existed && old.Alive
        if wasAlive \!= snap.Alive {
            s.aliveChangeCallback(tag, wasAlive, snap.Alive)
        }
    }
}

This reacts within the existing 2 s sample cycle — no extra polling loop.

2. Add the setting

In internal/web/entity/entity.go (after TgBotLoginNotify):

TgBotObservatoryNotify bool `json:"tgBotObservatoryNotify" form:"tgBotObservatoryNotify"`

In internal/web/service/setting.go (after GetTgBotLoginNotify):

func (s *SettingService) GetTgBotObservatoryNotify() (bool, error) {
    return s.getBool("tgBotObservatoryNotify")
}

3. Wire the callback in server.go:startTask()

a.xrayMetricsService.SetAliveChangeCallback(func(tag string, wasAlive, isAlive bool) {
    enabled, err := a.settingService.GetTgBotObservatoryNotify() // or use a.tgbotService equivalent
    if err \!= nil || \!enabled {
        return
    }
    // build and send message via tgbotService.SendMsgToTgbotAdmins(...)
})

4. i18n keys

Add two keys to all 13 files under internal/web/translation/ — one for dead, one for alive. Follow the cpuThreshold pattern at line 1571 of en-US.json:

"outboundDead":  "🔴 Outbound {{ .Tag }} is DOWN ({{ .Time }})",
"outboundAlive": "🟢 Outbound {{ .Tag }} is back UP ({{ .Time }})"

5. Frontend toggle

Add the tgBotObservatoryNotify boolean to the Telegram bot settings section in the React frontend (follows the existing tgBotLoginNotify checkbox).

One prerequisite worth documenting

Observatory data is only available when the metrics block is configured in the Xray config template (checked in xray_metrics.go:98-115). If XrayMetricsService.State().Enabled is false, applyObservatory() is never called with live data, so no transitions will fire. The PR description or UI tooltip should note this dependency.

Cooldown / flap suppression (optional but useful)

A simple map[string]time.Time in the callback closure (or a new field on XrayMetricsService) storing the last notification time per tag is enough to implement the cooldown you mentioned — no additional dependencies needed.

Good luck with the PR!

<!-- gh-comment-id:4674846955 --> @github-actions[bot] commented on GitHub (Jun 10, 2026): Thanks for the detailed request and for volunteering to implement it — the use case is clear and the panel already has every building block in place. ## What's already there | Component | File | Notes | |---|---|---| | Observatory snapshot | `internal/web/service/xray_metrics.go:70` | `ObservatorySnapshot()` returns `[]ObsTagSnapshot{Tag, Alive, Delay, LastSeenTime, …}` | | State update loop | `xray_metrics.go:185` `applyObservatory()` | Called every 2 s from `server.go:startTask()` — old map is in `s.obsByTag`, new map is in `next` before they are swapped at line 213 | | Telegram broadcast | `internal/web/service/tgbot/tgbot_send.go:160` | `SendMsgToTgbotAdmins(msg)` | | Boolean-toggle pattern | `internal/web/entity/entity.go:49` + `setting.go:448` | `TgBotLoginNotify` / `GetTgBotLoginNotify()` — exact same shape needed here | | Cron job pattern | `internal/web/job/check_cpu_usage.go` | `settingService` + `tgbotService` dependencies, registered in `web.go:startTask()` | | i18n message keys | `internal/web/translation/en-US.json:1571` | `cpuThreshold`, `loginSuccess`, etc. — 13 locale files to update | ## Suggested implementation path ### 1. Callback hook in `applyObservatory()` (recommended) `applyObservatory()` already has both the old state (`s.obsByTag`) and the new state (`next`) in scope before the swap. Add an optional callback: ```go // XrayMetricsService — new field aliveChangeCallback func(tag string, wasAlive, isAlive bool) func (s *XrayMetricsService) SetAliveChangeCallback(fn func(tag, wasAlive, isAlive bool)) { s.mu.Lock() s.aliveChangeCallback = fn s.mu.Unlock() } ``` Inside `applyObservatory()`, after building `next` and before `s.obsByTag = next`: ```go if s.aliveChangeCallback \!= nil { for tag, snap := range next { old, existed := s.obsByTag[tag] wasAlive := existed && old.Alive if wasAlive \!= snap.Alive { s.aliveChangeCallback(tag, wasAlive, snap.Alive) } } } ``` This reacts within the existing 2 s sample cycle — no extra polling loop. ### 2. Add the setting In `internal/web/entity/entity.go` (after `TgBotLoginNotify`): ```go TgBotObservatoryNotify bool `json:"tgBotObservatoryNotify" form:"tgBotObservatoryNotify"` ``` In `internal/web/service/setting.go` (after `GetTgBotLoginNotify`): ```go func (s *SettingService) GetTgBotObservatoryNotify() (bool, error) { return s.getBool("tgBotObservatoryNotify") } ``` ### 3. Wire the callback in `server.go:startTask()` ```go a.xrayMetricsService.SetAliveChangeCallback(func(tag string, wasAlive, isAlive bool) { enabled, err := a.settingService.GetTgBotObservatoryNotify() // or use a.tgbotService equivalent if err \!= nil || \!enabled { return } // build and send message via tgbotService.SendMsgToTgbotAdmins(...) }) ``` ### 4. i18n keys Add two keys to all 13 files under `internal/web/translation/` — one for dead, one for alive. Follow the `cpuThreshold` pattern at line 1571 of `en-US.json`: ```json "outboundDead": "🔴 Outbound {{ .Tag }} is DOWN ({{ .Time }})", "outboundAlive": "🟢 Outbound {{ .Tag }} is back UP ({{ .Time }})" ``` ### 5. Frontend toggle Add the `tgBotObservatoryNotify` boolean to the Telegram bot settings section in the React frontend (follows the existing `tgBotLoginNotify` checkbox). ## One prerequisite worth documenting Observatory data is only available when the `metrics` block is configured in the Xray config template (checked in `xray_metrics.go:98-115`). If `XrayMetricsService.State().Enabled` is false, `applyObservatory()` is never called with live data, so no transitions will fire. The PR description or UI tooltip should note this dependency. ## Cooldown / flap suppression (optional but useful) A simple `map[string]time.Time` in the callback closure (or a new field on `XrayMetricsService`) storing the last notification time per tag is enough to implement the cooldown you mentioned — no additional dependencies needed. Good luck with the PR\!
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#3640
No description provided.