Add exception handling to MessageDispatcherHandler

This commit is contained in:
Sergey Nazarov 2024-08-03 15:13:03 +03:00
parent 5aa5c2b183
commit 4e330a2455

View File

@ -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);