2025-07-24 23:19:59 +04:00
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
using Telegram.Bot;
|
|
|
|
|
using Telegram.Bot.Polling;
|
|
|
|
|
using Telegrator.Hosting.Components;
|
|
|
|
|
using Telegrator.MadiatorCore;
|
|
|
|
|
using Telegrator.Polling;
|
|
|
|
|
|
|
|
|
|
namespace Telegrator.Hosting.Polling
|
|
|
|
|
{
|
2025-07-26 00:01:46 +04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Service for receiving updates for Hosted telegram bots
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="botHost"></param>
|
|
|
|
|
/// <param name="botClient"></param>
|
|
|
|
|
/// <param name="updateRouter"></param>
|
|
|
|
|
/// <param name="options"></param>
|
|
|
|
|
/// <param name="logger"></param>
|
2025-07-24 23:19:59 +04:00
|
|
|
public class HostedUpdateReceiver(ITelegramBotHost botHost, ITelegramBotClient botClient, IUpdateRouter updateRouter, IOptions<ReceiverOptions> options, ILogger<HostedUpdateReceiver> logger) : BackgroundService
|
|
|
|
|
{
|
2025-07-26 00:01:46 +04:00
|
|
|
private readonly ReceiverOptions _receiverOptions = options.Value;
|
|
|
|
|
private readonly IUpdateRouter _updateRouter = updateRouter;
|
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
|
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
|
|
|
{
|
|
|
|
|
logger.LogInformation("Starting receiving updates via long-polling");
|
2025-07-26 00:01:46 +04:00
|
|
|
_receiverOptions.AllowedUpdates = botHost.UpdateRouter.HandlersProvider.AllowedTypes.ToArray();
|
2026-03-06 18:51:10 +04:00
|
|
|
DefaultUpdateReceiver updateReceiver = new DefaultUpdateReceiver(botClient, _receiverOptions);
|
2025-07-26 00:01:46 +04:00
|
|
|
await updateReceiver.ReceiveAsync(_updateRouter, stoppingToken).ConfigureAwait(false);
|
2025-07-24 23:19:59 +04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|