babalae/better-genshin-impact · error · NotifierException

Telegram {type} message failed: {response.StatusCode}, {resp

Error message

Telegram {type} message failed: {response.StatusCode}, {responseContent}

What it means

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.

Source

Thrown at BetterGenshinImpact/Service/Notifier/TelegramNotifier.cs:183

            text = message,
            disable_web_page_preview = true
        }, JsonOptions);

        var content = new StringContent(json, Encoding.UTF8, "application/json");
        await SendRequestAsync(endpoint, content, "text");
    }

    private async Task SendRequestAsync(string endpoint, HttpContent content, string type)
    {
        var request = new HttpRequestMessage(HttpMethod.Post, endpoint) { Content = content };
        request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        request.Headers.UserAgent.Add(new ProductInfoHeaderValue("BetterGenshinImpact", "1.0"));

        var response = await _httpClient.SendAsync(request);
        var responseContent = await response.Content.ReadAsStringAsync();

        if (!response.IsSuccessStatusCode)
            throw new NotifierException($"Telegram {type} message failed: {response.StatusCode}, {responseContent}");

        var (isSuccess, errorCode, errorDescription) = ValidateApiResponse(responseContent);
        if (!isSuccess)
        {
            var msg = errorCode switch
            {
                400 => "Please send a message to the bot first and check that the chat ID is correct.",
                401 => "Telegram bot token is incorrect.",
                404 => $"Telegram API not found (404). Please verify your bot token is correct. URL: {endpoint}",
                _ => $"Telegram API error: {errorDescription} (Code: {errorCode})"
            };
            throw new NotifierException(msg);
        }
    }

    private static string FormatApiBaseUrl(string apiBaseUrl)
    {
        if (string.IsNullOrEmpty(apiBaseUrl)) return DefaultApiUrl;

View on GitHub (pinned to a7cb36712d)

Solutions

  1. 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).
  2. For sendPhoto failures, downscale or recompress the screenshot before upload.
  3. Verify the bot token and (if set) TelegramApiBaseUrl.
  4. For 429, parse retry_after from the body and wait before retrying.

Example fix

// before (already includes body) — improve by parsing retry_after for 429
if (!response.IsSuccessStatusCode)
    throw new NotifierException($"Telegram {type} message failed: {response.StatusCode}, {responseContent}");

// after
if (response.StatusCode == System.Net.HttpStatusCode.TooManyRequests)
{
    int retryAfter = ParseRetryAfter(response);
    throw new NotifierException($"Telegram rate limited. Retry after {retryAfter}s. Body: {responseContent}");
}
if (!response.IsSuccessStatusCode)
    throw new NotifierException($"Telegram {type} message failed: {response.StatusCode}, {responseContent}");
Defensive patterns

Strategy: try-catch

Try / catch

try { await telegramNotifier.SendAsync(data); }
catch (NotifierException ex) when (ex.Message.Contains("message failed"))
{
    if (ex.Message.Contains("413")) { /* downscale screenshot and retry */ }
    else if (ex.Message.Contains("429")) { /* parse retry_after, wait */ }
    else throw;
}

Prevention

When it happens

Trigger: 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).

Common situations: 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).

Related errors


AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13). Data as JSON: /api/errors/d2fb3e63eb77af44. Report an issue: GitHub.