From 55d0b02e396e8006e38ceaf7f25ef8600a651e2c Mon Sep 17 00:00:00 2001 From: database64128 Date: Tue, 5 Nov 2024 20:23:56 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=81=20Add=20startup=20retry?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Make sure the service won't abort if the Telegram servers are down. --- CubicBot.Telegram/BotService.cs | 57 +++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/CubicBot.Telegram/BotService.cs b/CubicBot.Telegram/BotService.cs index d3efcf6..775a223 100644 --- a/CubicBot.Telegram/BotService.cs +++ b/CubicBot.Telegram/BotService.cs @@ -1,7 +1,9 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Telegram.Bot; +using Telegram.Bot.Exceptions; using Telegram.Bot.Polling; +using Telegram.Bot.Types; namespace CubicBot.Telegram; @@ -42,30 +44,51 @@ private async Task RunBotAsync(CancellationToken cancellationToken = default) var saveDataTask = SaveDataHourlyAsync(data, cancellationToken); - try + TelegramBotClientOptions options = new(botToken) { - using var httpClient = new HttpClient(); - var bot = new TelegramBotClient(botToken, httpClient); - var me = await bot.GetMe(cancellationToken); + RetryCount = 7, + }; + using HttpClient httpClient = new(); + TelegramBotClient bot = new(options, httpClient); + User me; - if (string.IsNullOrEmpty(me.Username)) - throw new Exception("Bot username is null or empty."); + while (true) + { + try + { + me = await bot.GetMe(cancellationToken); + break; + } + catch (RequestException ex) + { + logger.LogWarning(ex, "Failed to get bot info, retrying in 30 seconds"); + await Task.Delay(30000, cancellationToken); + } + } - var updateHandler = new UpdateHandler(me.Username, config, data, logger); - await updateHandler.RegisterCommandsAsync(bot, cancellationToken); + if (string.IsNullOrEmpty(me.Username)) + throw new Exception("Bot username is null or empty."); - logger.LogInformation("Started Telegram bot: @{BotUsername} ({BotId})", me.Username, me.Id); + var updateHandler = new UpdateHandler(me.Username, config, data, logger); - var updateReceiver = new QueuedUpdateReceiver(bot, null, updateHandler.HandleErrorAsync); - await updateHandler.HandleUpdateStreamAsync(bot, updateReceiver, cancellationToken); - } - catch (OperationCanceledException) + while (true) { + try + { + await updateHandler.RegisterCommandsAsync(bot, cancellationToken); + break; + } + catch (RequestException ex) + { + logger.LogWarning(ex, "Failed to register commands, retrying in 30 seconds"); + await Task.Delay(30000, cancellationToken); + } } - catch (Exception ex) - { - throw new Exception("Failed to start Telegram bot", ex); - } + + logger.LogInformation("Started Telegram bot: @{BotUsername} ({BotId})", me.Username, me.Id); + + var updateReceiver = new QueuedUpdateReceiver(bot, null, updateHandler.HandleErrorAsync); + await updateHandler.HandleUpdateStreamAsync(bot, updateReceiver, cancellationToken); await saveDataTask; }