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
|
|
|
|
|
{
|
2025-07-26 00:01:46 +04:00
|
|
|
/// <inheritdoc/>
|
2025-07-24 23:19:59 +04:00
|
|
|
public class HostUpdateRouter : UpdateRouter
|
|
|
|
|
{
|
2025-07-26 00:01:46 +04:00
|
|
|
/// <summary>
|
|
|
|
|
/// <see cref="ILogger"/> of this router
|
|
|
|
|
/// </summary>
|
2025-07-24 23:19:59 +04:00
|
|
|
protected readonly ILogger<HostUpdateRouter> Logger;
|
|
|
|
|
|
2025-07-26 00:01:46 +04:00
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public HostUpdateRouter(
|
|
|
|
|
IHandlersProvider handlersProvider,
|
|
|
|
|
IAwaitingProvider awaitingProvider,
|
2025-07-28 20:35:48 +04:00
|
|
|
IOptions<TelegratorOptions> options,
|
2025-07-26 00:01:46 +04:00
|
|
|
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;
|
2025-07-26 21:03:42 +04:00
|
|
|
ExceptionHandler = new DefaultRouterExceptionHandler(HandleException);
|
2025-07-24 23:19:59 +04:00
|
|
|
}
|
|
|
|
|
|
2025-07-26 00:01:46 +04:00
|
|
|
/// <inheritdoc/>
|
2025-07-24 23:19:59 +04:00
|
|
|
public override Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2025-07-28 20:35:48 +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);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-26 00:01:46 +04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Default exception handler of this router
|
|
|
|
|
/// </summary>
|
2025-07-26 21:03:42 +04:00
|
|
|
/// <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
|
|
|
{
|
2025-07-26 21:03:42 +04:00
|
|
|
if (exception is HandlerFaultedException handlerFaultedException)
|
2025-07-24 23:19:59 +04:00
|
|
|
{
|
2025-07-26 21:03:42 +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
|
|
|
}
|
2025-07-26 21:03:42 +04:00
|
|
|
|
|
|
|
|
Logger.LogError("Exception was thrown during update routing faulted :\n{exception}", exception.ToString());
|
2025-07-24 23:19:59 +04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|