Files
ChatBot/ChatBot/Services/Telegram/TelegramErrorHandler.cs
Leonid Pershin fd68fb4cba fix
2025-10-15 21:28:33 +03:00

44 lines
1.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Microsoft.Extensions.Logging;
using Telegram.Bot;
using Telegram.Bot.Exceptions;
namespace ChatBot.Services.Telegram
{
/// <summary>
/// Обработчик ошибок Telegram бота
/// </summary>
public class TelegramErrorHandler : ITelegramErrorHandler
{
private readonly ILogger<TelegramErrorHandler> _logger;
public TelegramErrorHandler(ILogger<TelegramErrorHandler> logger)
{
_logger = logger;
}
/// <summary>
/// Обрабатывает ошибки polling'а Telegram бота
/// </summary>
public Task HandlePollingErrorAsync(
ITelegramBotClient botClient,
Exception exception,
CancellationToken cancellationToken
)
{
var errorMessage = GetErrorMessage(exception);
_logger.LogError(exception, "Telegram bot polling error: {ErrorMessage}", errorMessage);
return Task.CompletedTask;
}
private static string GetErrorMessage(Exception exception)
{
return exception switch
{
ApiRequestException apiRequestException =>
$"Telegram API Error:\n[{apiRequestException.ErrorCode}]\n{apiRequestException.Message}",
_ => exception.ToString(),
};
}
}
}