{"record":{"id":"3ea843ef0e55fda6","repo":"MHSanaei/3x-ui","slug":"bot-not-initialized","errorCode":null,"errorMessage":"bot not initialized","messagePattern":"bot not initialized","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/web/service/tgbot/tgbot_send.go","lineNumber":267,"sourceCode":"\t}\n\tparams := telego.DeleteMessageParams{\n\t\tChatID:    tu.ID(chatId),\n\t\tMessageID: messageID,\n\t}\n\tif err := bot.DeleteMessage(context.Background(), &params); err != nil {\n\t\tlogger.Warning(\"Failed to delete message:\", err)\n\t} else {\n\t\tlogger.Info(\"Message deleted successfully\")\n\t}\n}\n\n// TestConnection verifies the bot token is valid and the API is reachable.\nfunc (t *Tgbot) TestConnection() error {\n\ttgBotMutex.Lock()\n\tb := bot\n\ttgBotMutex.Unlock()\n\tif b == nil {\n\t\treturn fmt.Errorf(\"bot not initialized\")\n\t}\n\tme, err := b.GetMe(context.Background())\n\tif err != nil {\n\t\treturn fmt.Errorf(\"API unreachable: %w\", err)\n\t}\n\t_ = me\n\treturn nil\n}\n","sourceCodeStart":249,"sourceCodeEnd":276,"githubUrl":"https://github.com/MHSanaei/3x-ui/blob/ad32144c42455696ea9f14e12168beac3e25f5d2/internal/web/service/tgbot/tgbot_send.go#L249-L276","documentation":"Returned by Tgbot.TestConnection when the package-level bot handle is nil. The Telegram bot in this panel is a lazily-initialized global (guarded by tgBotMutex); if Start() was never called or failed (bad token, disabled in settings), the handle stays nil and every test call short-circuits here before any network traffic happens.","triggerScenarios":"Calling the Telegram test endpoint (SetTestTgFunc wiring in web.go checks IsRunning() first, but direct TestConnection calls bypass that) while the bot setting is disabled, the token is empty/invalid so Start() failed, or after Stop() has run.","commonSituations":"Admin clicks 'test Telegram' before filling in botToken/chatId, or toggles the bot off then hits a stale test button; also during panel startup before the tgbot job has started the bot.","solutions":["Verify the Telegram bot is enabled in panel settings and botToken + chatId are set, then Save (this triggers a bot reload).","Call Start() (or the reload endpoint wired via SetReloadTgbotFunc) before testing, or simply use the panel's test endpoint which checks IsRunning() first and gives the clearer 'telegram bot is not running' message.","Check panel logs for a prior Start() failure — usually a 401 from Telegram meaning the token is wrong.","Treat this state as permanent until reconfiguration: do not retry, reconfigure."],"exampleFix":"// before\nif err := s.tgbotService.TestConnection(); err != nil { ... }\n\n// after — gate on running state first so the user gets an actionable message\nif !s.tgbotService.IsRunning() {\n    return fmt.Errorf(\"telegram bot is not running (check token and chat ID)\")\n}\nif err := s.tgbotService.TestConnection(); err != nil { ... }","handlingStrategy":"validation","validationCode":"// Before testing, check the bot is actually running\nif !tgbotSvc.IsRunning() {\n    // reconfigure (token/chatId/enable) instead of calling TestConnection\n    return errors.New(\"configure and start the telegram bot first\")\n}","typeGuard":"null","tryCatchPattern":"if err := tgbotSvc.TestConnection(); err != nil {\n    if strings.Contains(err.Error(), \"bot not initialized\") {\n        // permanent state: reload bot config, do not retry\n    }\n}","preventionTips":["Always gate test calls behind IsRunning().","Save Telegram settings (which reloads the bot) before testing."],"tags":["telegram","bot","initialization"],"backgroundTag":null,"analyzedSha":"ad32144c42455696ea9f14e12168beac3e25f5d2","analyzedAt":"2026-08-15T11:13:23.905Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}