{"record":{"id":"d2fb3e63eb77af44","repo":"babalae/better-genshin-impact","slug":"telegram-type-message-failed-response-statusco","errorCode":null,"errorMessage":"Telegram {type} message failed: {response.StatusCode}, {responseContent}","messagePattern":"Telegram (.+?) message failed: (.+?), (.+?)","errorType":"exception","errorClass":"NotifierException","httpStatus":null,"severity":"error","filePath":"BetterGenshinImpact/Service/Notifier/TelegramNotifier.cs","lineNumber":183,"sourceCode":"            text = message,\n            disable_web_page_preview = true\n        }, JsonOptions);\n\n        var content = new StringContent(json, Encoding.UTF8, \"application/json\");\n        await SendRequestAsync(endpoint, content, \"text\");\n    }\n\n    private async Task SendRequestAsync(string endpoint, HttpContent content, string type)\n    {\n        var request = new HttpRequestMessage(HttpMethod.Post, endpoint) { Content = content };\n        request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(\"application/json\"));\n        request.Headers.UserAgent.Add(new ProductInfoHeaderValue(\"BetterGenshinImpact\", \"1.0\"));\n\n        var response = await _httpClient.SendAsync(request);\n        var responseContent = await response.Content.ReadAsStringAsync();\n\n        if (!response.IsSuccessStatusCode)\n            throw new NotifierException($\"Telegram {type} message failed: {response.StatusCode}, {responseContent}\");\n\n        var (isSuccess, errorCode, errorDescription) = ValidateApiResponse(responseContent);\n        if (!isSuccess)\n        {\n            var msg = errorCode switch\n            {\n                400 => \"Please send a message to the bot first and check that the chat ID is correct.\",\n                401 => \"Telegram bot token is incorrect.\",\n                404 => $\"Telegram API not found (404). Please verify your bot token is correct. URL: {endpoint}\",\n                _ => $\"Telegram API error: {errorDescription} (Code: {errorCode})\"\n            };\n            throw new NotifierException(msg);\n        }\n    }\n\n    private static string FormatApiBaseUrl(string apiBaseUrl)\n    {\n        if (string.IsNullOrEmpty(apiBaseUrl)) return DefaultApiUrl;","sourceCodeStart":165,"sourceCodeEnd":201,"githubUrl":"https://github.com/babalae/better-genshin-impact/blob/a7cb36712dcb409be610257d877fcea3597e9d6b/BetterGenshinImpact/Service/Notifier/TelegramNotifier.cs#L165-L201","documentation":"Thrown by SendRequestAsync when the Telegram Bot API returns a non-2xx HTTP status. Includes the {type} (text or image), the HttpStatusCode, and the raw responseContent so the developer can see Telegram's error body. This is the HTTP-level failure; the application-level ok:false path is handled separately at line 185-196.","triggerScenarios":"response.IsSuccessStatusCode is false after POST to {baseUrl}{token}/sendMessage or /sendPhoto. Common: 401 Unauthorized (bad token format in URL), 404 (wrong API base URL), 413 (image too large), 429 (rate limit).","commonSituations":"Token contains characters that break the URL; custom TelegramApiBaseUrl wrong; screenshot PNG exceeds Telegram's 10MB photo limit; hitting the Telegram API rate limit (429 with retry_after).","solutions":["Read responseContent in the message — it already contains Telegram's description; act on it (401 -> fix token, 413 -> compress image, 429 -> back off by retry_after).","For sendPhoto failures, downscale or recompress the screenshot before upload.","Verify the bot token and (if set) TelegramApiBaseUrl.","For 429, parse retry_after from the body and wait before retrying."],"exampleFix":"// before (already includes body) — improve by parsing retry_after for 429\nif (!response.IsSuccessStatusCode)\n    throw new NotifierException($\"Telegram {type} message failed: {response.StatusCode}, {responseContent}\");\n\n// after\nif (response.StatusCode == System.Net.HttpStatusCode.TooManyRequests)\n{\n    int retryAfter = ParseRetryAfter(response);\n    throw new NotifierException($\"Telegram rate limited. Retry after {retryAfter}s. Body: {responseContent}\");\n}\nif (!response.IsSuccessStatusCode)\n    throw new NotifierException($\"Telegram {type} message failed: {response.StatusCode}, {responseContent}\");","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { await telegramNotifier.SendAsync(data); }\ncatch (NotifierException ex) when (ex.Message.Contains(\"message failed\"))\n{\n    if (ex.Message.Contains(\"413\")) { /* downscale screenshot and retry */ }\n    else if (ex.Message.Contains(\"429\")) { /* parse retry_after, wait */ }\n    else throw;\n}","preventionTips":["Read the responseContent embedded in the message to pick the right fix.","Compress screenshots before sendPhoto to avoid 413.","Respect retry_after on 429 to avoid worsening rate limits."],"tags":["telegram","http","api-failure","rate-limit","notifier"],"backgroundTag":null,"analyzedSha":"a7cb36712dcb409be610257d877fcea3597e9d6b","analyzedAt":"2026-08-13T16:44:57.548Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}