babalae/better-genshin-impact · warning · NotifierException

Telegram bot token is not set

Error message

Telegram bot token is not set

What it means

TelegramNotifier.SendAsync fails fast when TelegramBotToken is null/empty. The token (from BotFather, format <botid>:<hash>) is required to build the API URL {baseUrl}{token}/send*.

Source

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

                    // 抛出异常以通知调用者配置错误
                    throw new NotifierException($"Invalid Telegram proxy URL format: {proxyUrl}. Details: {ex.Message}");
                }

            _httpClient = new HttpClient(handler) { Timeout = TimeSpan.FromSeconds(30) };
            _ownsHttpClient = true;
        }

        TelegramApiBaseUrl = FormatApiBaseUrl(telegramApiBaseUrl);
    }

    public void Dispose()
    {
        if (_ownsHttpClient) _httpClient.Dispose();
    }

    public async Task SendAsync(BaseNotificationData content)
    {
        if (string.IsNullOrEmpty(TelegramBotToken)) throw new NotifierException("Telegram bot token is not set");
        if (string.IsNullOrEmpty(TelegramChatId)) throw new NotifierException("Telegram chat ID is not set");
        if (string.IsNullOrEmpty(content.Message)) throw new NotifierException("No message content to send");

        try
        {
            if (content.Screenshot != null)
            {
                await SendImageMessageAsync(content.Screenshot, content.Message.Length < 1024 ? content.Message : null);
                if (content.Message.Length < 1024) return;
            }

            await SendTextMessageAsync(content.Message);
        }
        catch (HttpRequestException ex)
        {
            throw new NotifierException("Network error sending Telegram notification: " + ex.Message);
        }
        catch (TaskCanceledException)

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Create a bot via @BotFather on Telegram and paste the token into settings.
  2. Block enabling the Telegram channel in the UI until a token is provided.
  3. Validate token shape (contains a colon) before construction.

Example fix

// before
var n = new TelegramNotifier(null, token, chatId);

// after
if (!Regex.IsMatch(token ?? "", @"^\d+:.+$"))
    throw new InvalidOperationException("Telegram bot token looks invalid (expected <numericBotId>:<hash>).");
var n = new TelegramNotifier(null, token, chatId);
Defensive patterns

Strategy: validation

Validate before calling

if (!Regex.IsMatch(token ?? "", @"^\d{6,}:[A-Za-z0-9_-]{30,}$"))
    throw new InvalidOperationException("Telegram bot token is missing or malformed (expected <botId>:<hash>).");

Type guard

static bool LooksLikeTelegramBotToken(string token)
    => !string.IsNullOrEmpty(token) && token.Contains(':') && token.Split(':')[0].All(char.IsDigit);

Try / catch

try { await telegramNotifier.SendAsync(data); }
catch (NotifierException ex) when (ex.Message.Contains("bot token is not set"))
{ /* prompt user to create a bot via BotFather and paste token, no retry */ }

Prevention

When it happens

Trigger: TelegramBotToken property empty at SendAsync time (default constructor arg was ""); line-114 IsNullOrEmpty guard fires.

Common situations: User enabled Telegram channel without pasting a bot token; token field cleared; new install with no token configured.

Related errors


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