Files
Telegrator/src/Telegrator.Hosting/Polling/HostUpdateRouter.cs
T

61 lines
2.3 KiB
C#
Raw Normal View History

2025-07-24 23:19:59 +04:00
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Telegram.Bot;
using Telegram.Bot.Polling;
using Telegram.Bot.Types;
using Telegrator.Configuration;
using Telegrator.MadiatorCore;
using Telegrator.Polling;
namespace Telegrator.Hosting.Polling
{
/// <inheritdoc/>
2025-07-24 23:19:59 +04:00
public class HostUpdateRouter : UpdateRouter
{
/// <summary>
/// <see cref="ILogger"/> of this router
/// </summary>
2025-07-24 23:19:59 +04:00
protected readonly ILogger<HostUpdateRouter> Logger;
/// <inheritdoc/>
public HostUpdateRouter(
IHandlersProvider handlersProvider,
IAwaitingProvider awaitingProvider,
IOptions<TelegratorOptions> options,
IUpdateHandlersPool handlersPool,
2025-07-27 14:19:40 +04:00
ITelegramBotInfo botInfo,
ILogger<HostUpdateRouter> logger) : base(handlersProvider, awaitingProvider, options.Value, handlersPool, botInfo)
2025-07-24 23:19:59 +04:00
{
Logger = logger;
ExceptionHandler = new DefaultRouterExceptionHandler(HandleException);
2025-07-24 23:19:59 +04:00
}
/// <inheritdoc/>
2025-07-24 23:19:59 +04:00
public override Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
{
2025-08-10 02:48:45 +04:00
//Logger.LogInformation("Received update of type \"{type}\"", update.Type);
2025-07-24 23:19:59 +04:00
return base.HandleUpdateAsync(botClient, update, cancellationToken);
}
/// <summary>
/// Default exception handler of this router
/// </summary>
/// <param name="botClient"></param>
/// <param name="exception"></param>
/// <param name="source"></param>
/// <param name="cancellationToken"></param>
public void HandleException(ITelegramBotClient botClient, Exception exception, HandleErrorSource source, CancellationToken cancellationToken)
2025-07-24 23:19:59 +04:00
{
if (exception is HandlerFaultedException handlerFaultedException)
2025-07-24 23:19:59 +04:00
{
Logger.LogError("\"{handler}\" handler's execution was faulted :\n{exception}",
handlerFaultedException.HandlerInfo.ToString(),
handlerFaultedException.InnerException?.ToString() ?? "No inner exception");
return;
2025-07-24 23:19:59 +04:00
}
Logger.LogError("Exception was thrown during update routing faulted :\n{exception}", exception.ToString());
2025-07-24 23:19:59 +04:00
}
}
}