using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Telegram.Bot; using Telegram.Bot.Polling; using Telegrator.Core; namespace Telegrator.Mediation; /// /// Service for receiving updates for Hosted telegram botsand queuing them to router /// /// /// /// /// public class HostedUpdateReceiver(ITelegramBotClient botClient, IUpdateRouter updateRouter, IOptions options, ILogger logger) : BackgroundService { private readonly ReceiverOptions _receiverOptions = options.Value; private readonly IUpdateRouter _updateRouter = updateRouter; /// protected override async Task ExecuteAsync(CancellationToken stoppingToken) { logger.LogInformation("Starting receiving updates via long-polling"); _receiverOptions.AllowedUpdates = _updateRouter.HandlersProvider.AllowedTypes.ToArray(); botClient.DeleteWebhook(options.Value.DropPendingUpdates, cancellationToken: stoppingToken) .ConfigureAwait(false).GetAwaiter().GetResult(); DefaultUpdateReceiver updateReceiver = new DefaultUpdateReceiver(botClient, _receiverOptions); await updateReceiver.ReceiveAsync(_updateRouter, stoppingToken).ConfigureAwait(false); } }