* Syntaxual refactoring

This commit is contained in:
2026-03-09 13:23:21 +04:00
parent 899243c62b
commit 79d5df8291
130 changed files with 9679 additions and 9831 deletions
@@ -10,88 +10,87 @@ using Telegram.Bot.Types;
using Telegrator.Core;
using Telegrator.Hosting.Web;
namespace Telegrator.Mediation
namespace Telegrator.Mediation;
/// <summary>
/// Service for receiving updates for Hosted telegram bots via Webhooks
/// </summary>
public class HostedUpdateWebhooker : IHostedService
{
private const string SecretTokenHeader = "X-Telegram-Bot-Api-Secret-Token";
private readonly IEndpointRouteBuilder _botHost;
private readonly ITelegramBotClient _botClient;
private readonly IUpdateRouter _updateRouter;
private readonly WebhookerOptions _options;
/// <summary>
/// Service for receiving updates for Hosted telegram bots via Webhooks
/// Initiallizes new instance of <see cref="HostedUpdateWebhooker"/>
/// </summary>
public class HostedUpdateWebhooker : IHostedService
/// <param name="botHost"></param>
/// <param name="botClient"></param>
/// <param name="updateRouter"></param>
/// <param name="options"></param>
/// <exception cref="ArgumentNullException"></exception>
public HostedUpdateWebhooker(IEndpointRouteBuilder botHost, ITelegramBotClient botClient, IUpdateRouter updateRouter, IOptions<WebhookerOptions> options)
{
private const string SecretTokenHeader = "X-Telegram-Bot-Api-Secret-Token";
if (string.IsNullOrEmpty(options.Value.WebhookUri))
throw new ArgumentNullException(nameof(options), "Option \"WebhookUrl\" must be set to subscribe for update recieving");
private readonly IEndpointRouteBuilder _botHost;
private readonly ITelegramBotClient _botClient;
private readonly IUpdateRouter _updateRouter;
private readonly WebhookerOptions _options;
_botHost = botHost;
_botClient = botClient;
_updateRouter = updateRouter;
_options = options.Value;
}
/// <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>
public HostedUpdateWebhooker(IEndpointRouteBuilder botHost, ITelegramBotClient botClient, IUpdateRouter updateRouter, IOptions<WebhookerOptions> options)
/// <inheritdoc/>
public Task StartAsync(CancellationToken cancellationToken)
{
StartInternal(cancellationToken);
return Task.CompletedTask;
}
private async void StartInternal(CancellationToken cancellationToken)
{
string pattern = new UriBuilder(_options.WebhookUri).Path;
_botHost.MapPost(pattern, (Delegate)ReceiveUpdate);
await _botClient.SetWebhook(
url: _options.WebhookUri,
maxConnections: _options.MaxConnections,
allowedUpdates: _updateRouter.HandlersProvider.AllowedTypes,
dropPendingUpdates: _options.DropPendingUpdates,
secretToken: _options.SecretToken,
cancellationToken: cancellationToken);
}
/// <inheritdoc/>
public Task StopAsync(CancellationToken cancellationToken)
{
_botClient.DeleteWebhook(_options.DropPendingUpdates, cancellationToken);
return Task.CompletedTask;
}
private async Task<IResult> ReceiveUpdate(HttpContext ctx)
{
if (_options.SecretToken != null)
{
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;
}
/// <inheritdoc/>
public Task StartAsync(CancellationToken cancellationToken)
{
StartInternal(cancellationToken);
return Task.CompletedTask;
}
private async void StartInternal(CancellationToken cancellationToken)
{
string pattern = new UriBuilder(_options.WebhookUri).Path;
_botHost.MapPost(pattern, (Delegate)ReceiveUpdate);
await _botClient.SetWebhook(
url: _options.WebhookUri,
maxConnections: _options.MaxConnections,
allowedUpdates: _updateRouter.HandlersProvider.AllowedTypes,
dropPendingUpdates: _options.DropPendingUpdates,
secretToken: _options.SecretToken,
cancellationToken: cancellationToken);
}
/// <inheritdoc/>
public Task StopAsync(CancellationToken cancellationToken)
{
_botClient.DeleteWebhook(_options.DropPendingUpdates, cancellationToken);
return Task.CompletedTask;
}
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 })
if (!ctx.Request.Headers.TryGetValue(SecretTokenHeader, out StringValues strings))
return Results.BadRequest();
await _updateRouter.HandleUpdateAsync(_botClient, update, ctx.RequestAborted);
return Results.Ok();
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();
}
}