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;
|
2026-03-06 23:19:24 +04:00
|
|
|
using Telegrator.Core;
|
|
|
|
|
using Telegrator.Mediation;
|
2025-07-24 23:19:59 +04:00
|
|
|
|
2026-03-09 13:23:21 +04:00
|
|
|
namespace Telegrator.Polling;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Service for receiving updates for Hosted telegram bots
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="botClient"></param>
|
|
|
|
|
/// <param name="updateRouter"></param>
|
|
|
|
|
/// <param name="options"></param>
|
|
|
|
|
/// <param name="logger"></param>
|
|
|
|
|
public class HostedUpdateReceiver(ITelegramBotClient botClient, IUpdateRouter updateRouter, IOptions<ReceiverOptions> options, ILogger<HostedUpdateReceiver> logger) : BackgroundService
|
2025-07-24 23:19:59 +04:00
|
|
|
{
|
2026-03-09 13:23:21 +04:00
|
|
|
private readonly ReceiverOptions _receiverOptions = options.Value;
|
|
|
|
|
private readonly IUpdateRouter _updateRouter = updateRouter;
|
2025-07-24 23:19:59 +04:00
|
|
|
|
2026-03-09 13:23:21 +04:00
|
|
|
/// <inheritdoc/>
|
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
|
|
|
{
|
|
|
|
|
logger.LogInformation("Starting receiving updates via long-polling");
|
|
|
|
|
_receiverOptions.AllowedUpdates = _updateRouter.HandlersProvider.AllowedTypes.ToArray();
|
|
|
|
|
DefaultUpdateReceiver updateReceiver = new DefaultUpdateReceiver(botClient, _receiverOptions);
|
|
|
|
|
await updateReceiver.ReceiveAsync(_updateRouter, stoppingToken).ConfigureAwait(false);
|
2025-07-24 23:19:59 +04:00
|
|
|
}
|
|
|
|
|
}
|