From 2692797dbc00bf07b165d9b40adb487a01d93a0a Mon Sep 17 00:00:00 2001 From: Ruberoid Date: Tue, 22 Jul 2025 10:52:43 +0300 Subject: [PATCH] Added common 403 exception handling. At least two reason of not sending message to user. 1. Bot blocked, 2. User deleted. --- .../MessageDispatcherHandler.cs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/Nocr.TelegramClient.AppServices/Bots/MessageDispatcher/MessageDispatcherHandler.cs b/src/Nocr.TelegramClient.AppServices/Bots/MessageDispatcher/MessageDispatcherHandler.cs index 7953f16..b09f32e 100644 --- a/src/Nocr.TelegramClient.AppServices/Bots/MessageDispatcher/MessageDispatcherHandler.cs +++ b/src/Nocr.TelegramClient.AppServices/Bots/MessageDispatcher/MessageDispatcherHandler.cs @@ -36,31 +36,28 @@ public sealed class MessageDispatcherHandler : IMessageDispatcherHandler { await _bot.SendMessageAsync(tm, cancellationToken); } - catch (Exception ex) when (ex is ApiRequestException && - ex.Message.Equals("Forbidden: bot was blocked by the user", - StringComparison.OrdinalIgnoreCase)) + catch (ApiRequestException ex) when (ex.ErrorCode == 403) { - _logger.LogWarning("Bot was blocked by user {ChatId}, marking user as blocked.", tm.ChatId); - + _logger.LogWarning("Bot can't interract with user {ChatId}.", tm.ChatId); + _logger.LogDebug(ex.ToString()); try { var user = await _usersService.GetByIdentity(tm.ChatId.Identifier ?? 0, cancellationToken); if (user != null) { - // Атомарно (лол. Клод считает, что две инструкции ниже это "атомарно") блокируем пользователя и деактивируем все его подписки await _usersService.BlockBot(user.Id, cancellationToken); await _textMatcherService.DeactivateAllUserSubscriptions(user.Id, cancellationToken); - - _logger.LogInformation("Successfully blocked user {UserId} (ChatId: {ChatId}) and deactivated all subscriptions.", user.Id, tm.ChatId); + + _logger.LogInformation("Successfully deactivated user {UserId} subscriptions. ", tm.ChatId); } else { - _logger.LogWarning("Could not find user with ChatId {ChatId} to mark as blocked.", tm.ChatId); + _logger.LogWarning("Could not find user with TelegramId {ChatId} to mark as blocked.", tm.ChatId); } } catch (Exception blockEx) { - _logger.LogError(blockEx, "Failed to process user blocking for ChatId {ChatId}.", tm.ChatId); + _logger.LogError(blockEx, "Failed to process user blocking for TelegramId {ChatId}.", tm.ChatId); } } break;