2025-07-28 20:35:48 +04:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
2025-07-24 23:19:59 +04:00
|
|
|
using Microsoft.Extensions.Options;
|
2025-07-28 20:35:48 +04:00
|
|
|
using Microsoft.Extensions.Primitives;
|
|
|
|
|
using System.Text.Json;
|
2025-07-24 23:19:59 +04:00
|
|
|
using Telegram.Bot;
|
2025-07-28 20:35:48 +04:00
|
|
|
using Telegram.Bot.Types;
|
2025-07-24 23:19:59 +04:00
|
|
|
using Telegrator.Hosting.Web.Components;
|
|
|
|
|
using Telegrator.MadiatorCore;
|
|
|
|
|
|
|
|
|
|
namespace Telegrator.Hosting.Web.Polling
|
|
|
|
|
{
|
2025-07-28 20:35:48 +04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Service for receiving updates for Hosted telegram bots via Webhooks
|
|
|
|
|
/// </summary>
|
2025-07-24 23:19:59 +04:00
|
|
|
public class HostedUpdateWebhooker : IHostedService
|
|
|
|
|
{
|
2025-07-28 20:35:48 +04:00
|
|
|
private const string SecretTokenHeader = "X-Telegram-Bot-Api-Secret-Token";
|
|
|
|
|
|
2025-07-24 23:19:59 +04:00
|
|
|
private readonly ITelegramBotWebHost _botHost;
|
|
|
|
|
private readonly ITelegramBotClient _botClient;
|
|
|
|
|
private readonly IUpdateRouter _updateRouter;
|
|
|
|
|
private readonly TelegramBotWebOptions _options;
|
|
|
|
|
|
2025-07-28 20:35:48 +04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Initiallizes new instance of <see cref="HostedUpdateWebhooker"/>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="botHost"></param>
|
|
|
|
|
/// <param name="botClient"></param>
|
|
|
|
|
/// <param name="updateRouter"></param>
|
|
|
|
|
/// <param name="options"></param>
|
|
|
|
|
/// <exception cref="ArgumentNullException"></exception>
|
2025-07-24 23:19:59 +04:00
|
|
|
public HostedUpdateWebhooker(ITelegramBotWebHost botHost, ITelegramBotClient botClient, IUpdateRouter updateRouter, IOptions<TelegramBotWebOptions> options)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(options.Value.WebhookUri))
|
|
|
|
|
throw new ArgumentNullException(nameof(options), "Option \"WebhookUrl\" must be set to subscribe for update recieving");
|
|
|
|
|
|
|
|
|
|
_botHost = botHost;
|
|
|
|
|
_botClient = botClient;
|
|
|
|
|
_updateRouter = updateRouter;
|
|
|
|
|
_options = options.Value;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-28 20:35:48 +04:00
|
|
|
/// <inheritdoc/>
|
2025-07-24 23:19:59 +04:00
|
|
|
public Task StartAsync(CancellationToken cancellationToken)
|
|
|
|
|
{
|
2025-07-28 20:35:48 +04:00
|
|
|
string pattern = new UriBuilder(_options.WebhookUri).Path;
|
|
|
|
|
_botHost.MapPost(pattern, (Delegate)ReceiveUpdate);
|
|
|
|
|
|
2025-07-24 23:19:59 +04:00
|
|
|
_botClient.SetWebhook(
|
|
|
|
|
url: _options.WebhookUri,
|
|
|
|
|
maxConnections: _options.MaxConnections,
|
|
|
|
|
allowedUpdates: _botHost.UpdateRouter.HandlersProvider.AllowedTypes,
|
|
|
|
|
dropPendingUpdates: _options.DropPendingUpdates,
|
2025-07-28 20:35:48 +04:00
|
|
|
cancellationToken: cancellationToken)
|
|
|
|
|
.Wait(cancellationToken);
|
2025-07-24 23:19:59 +04:00
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-28 20:35:48 +04:00
|
|
|
/// <inheritdoc/>
|
2025-07-24 23:19:59 +04:00
|
|
|
public Task StopAsync(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
_botClient.DeleteWebhook(_options.DropPendingUpdates, cancellationToken);
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2025-07-28 20:35:48 +04:00
|
|
|
|
|
|
|
|
private async Task<IResult> ReceiveUpdate(HttpContext ctx)
|
|
|
|
|
{
|
|
|
|
|
if (_options.SecretToken != null)
|
|
|
|
|
{
|
|
|
|
|
if (!ctx.Request.Headers.TryGetValue(SecretTokenHeader, out StringValues strings))
|
|
|
|
|
return Results.BadRequest();
|
|
|
|
|
|
|
|
|
|
string? secret = strings.SingleOrDefault();
|
|
|
|
|
if (secret == null)
|
|
|
|
|
return Results.BadRequest();
|
|
|
|
|
|
|
|
|
|
if (_options.SecretToken != secret)
|
|
|
|
|
return Results.StatusCode(401);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Update? update = await JsonSerializer.DeserializeAsync<Update>(ctx.Request.Body, JsonBotAPI.Options, ctx.RequestAborted);
|
|
|
|
|
if (update is not { Id: > 0 })
|
|
|
|
|
return Results.BadRequest();
|
|
|
|
|
|
|
|
|
|
await _updateRouter.HandleUpdateAsync(_botClient, update, ctx.RequestAborted);
|
|
|
|
|
return Results.Ok();
|
|
|
|
|
}
|
2025-07-24 23:19:59 +04:00
|
|
|
}
|
|
|
|
|
}
|