44 lines
1.3 KiB
C#
44 lines
1.3 KiB
C#
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(),
|
||
};
|
||
}
|
||
}
|
||
}
|