From 4e330a2455bd8994bb39aa1182785d80cb45e51e Mon Sep 17 00:00:00 2001 From: Sergey Nazarov Date: Sat, 3 Aug 2024 15:13:03 +0300 Subject: [PATCH] Add exception handling to MessageDispatcherHandler --- .../MessageDispatcher/MessageDispatcherHandler.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Nocr.TelegramClient.AppServices/Bots/MessageDispatcher/MessageDispatcherHandler.cs b/src/Nocr.TelegramClient.AppServices/Bots/MessageDispatcher/MessageDispatcherHandler.cs index 64e847e..1cdcf5c 100644 --- a/src/Nocr.TelegramClient.AppServices/Bots/MessageDispatcher/MessageDispatcherHandler.cs +++ b/src/Nocr.TelegramClient.AppServices/Bots/MessageDispatcher/MessageDispatcherHandler.cs @@ -1,6 +1,7 @@ using Insight.TelegramBot; using Insight.TelegramBot.Models; using Microsoft.Extensions.Logging; +using Telegram.Bot.Exceptions; using Telegram.Bot.Types; namespace Nocr.TelegramClient.AppServices.Bots.MessageDispatcher; @@ -25,7 +26,16 @@ public sealed class MessageDispatcherHandler : IMessageDispatcherHandler switch (message) { case TextMessage tm: - await _bot.SendMessageAsync(tm, cancellationToken); + try + { + await _bot.SendMessageAsync(tm, cancellationToken); + } + catch (Exception ex) when (ex is ApiRequestException && + ex.Message.Equals("Forbidden: bot was blocked by the user", + StringComparison.OrdinalIgnoreCase)) + { + _logger.LogWarning(ex, "Failed to deliver message due bot was blocked by the user [{ChatId}]", tm.ChatId); + } break; case AnimationMessage am: await _bot.SendAnimationAsync(am, cancellationToken);