{"record":{"id":"e678ed185208c018","repo":"MHSanaei/3x-ui","slug":"telegram-bot-is-not-running-check-token-and-chat","errorCode":null,"errorMessage":"telegram bot is not running (check token and chat ID)","messagePattern":"telegram bot is not running \\(check token and chat ID\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/web/web.go","lineNumber":623,"sourceCode":"\t\t\ts.bus.Publish(eventbus.Event{\n\t\t\t\tType: eventbus.EventXrayCrash,\n\t\t\t\tData: err.Error(),\n\t\t\t})\n\t\t}\n\t}\n\n\t// Register email subscriber (always — it checks smtpEnable at runtime)\n\temailService := email.NewEmailService(s.settingService)\n\temailSub := email.NewSubscriber(s.settingService, emailService)\n\ts.bus.Subscribe(\"email-notifier\", emailSub.HandleEvent)\n\n\t// Wire email service to controller for test endpoint\n\tcontroller.SetEmailService(emailService)\n\n\t// Wire Telegram test function to controller\n\tcontroller.SetTestTgFunc(func() error {\n\t\tif !s.tgbotService.IsRunning() {\n\t\t\treturn fmt.Errorf(\"telegram bot is not running (check token and chat ID)\")\n\t\t}\n\t\tif err := s.tgbotService.TestConnection(); err != nil {\n\t\t\treturn fmt.Errorf(\"telegram API test failed: %w\", err)\n\t\t}\n\t\ts.tgbotService.SendMsgToTgbotAdmins(\"✅ Test message from 3x-ui\")\n\t\treturn nil\n\t})\n\n\tcontroller.SetReloadTgbotFunc(func() {\n\t\tenabled, err := s.settingService.GetTgbotEnabled()\n\t\tif err != nil || !enabled {\n\t\t\tif s.tgbotService.IsRunning() {\n\t\t\t\ts.tgbotService.Stop()\n\t\t\t}\n\t\t\tif s.bus != nil {\n\t\t\t\ts.bus.Unsubscribe(\"tg-notifier\")\n\t\t\t}\n\t\t\treturn","sourceCodeStart":605,"sourceCodeEnd":641,"githubUrl":"https://github.com/MHSanaei/3x-ui/blob/ad32144c42455696ea9f14e12168beac3e25f5d2/internal/web/web.go#L605-L641","documentation":"Returned by the SetTestTgFunc closure in web.go — the handler behind the panel's Telegram test endpoint. It fires before any API call when tgbotService.IsRunning() is false: the bot process/handle was never started, because the Telegram setting is disabled or Start() failed on an invalid token or missing chat ID.","triggerScenarios":"Clicking 'Test' on the Telegram settings page while tgbotEnable is off, botToken is empty/malformed, chatId is unset, or the bot previously crashed and was stopped.","commonSituations":"First-time Telegram setup where the admin pastes the token but forgets chatId or the enable toggle; toggling the bot off then testing; a previously-started bot stopped after a token error.","solutions":["Fill in botToken (from @BotFather) AND chatId (the admin chat the bot was started in), and enable the bot in settings, then Save — saving reloads the bot.","After saving, check panel logs for a successful bot start before pressing Test.","If it persists with correct settings, check the tgbot job/Start() logs — the underlying failure is usually visible there (network to Telegram, 401)."],"exampleFix":"// before: bot disabled + test pressed -> error\n// settings: tgbotEnable=false, botToken=\"\", chatId=\"\"\n\n// after: settings saved with\n// tgbotEnable=true, botToken=\"123456:ABC...\" (from @BotFather), chatId=\"<your chat id>\"\n// then press Test","handlingStrategy":"validation","validationCode":"// Before invoking the test, ensure the bot is configured and running\nenabled, _ := settingSvc.GetTgbotEnabled()\nif !enabled || !tgbotSvc.IsRunning() {\n    return errors.New(\"enable the telegram bot and set token/chat ID first\")\n}","typeGuard":"null","tryCatchPattern":"if err := testTg(); err != nil {\n    if strings.Contains(err.Error(), \"not running\") {\n        // guide user to settings; no retry\n    }\n}","preventionTips":["Treat token + chatId + enable as one unit when saving Telegram settings.","Confirm bot start in logs before testing."],"tags":["telegram","configuration","bot"],"backgroundTag":null,"analyzedSha":"ad32144c42455696ea9f14e12168beac3e25f5d2","analyzedAt":"2026-08-15T11:13:23.905Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}