* Changed public API overview generator behaviour, now working only in DEBUG builds
* Fixed wrong LeveldDebug method calls after moving logic from providers to router * Added independent "IndentFlags" property to inner debugger class * Fixed debug logging in few places * Removed "ICollectingOptions" and merged it with new options abstract "ITelegratorOptions" * Added WebHook version of hosting class
This commit is contained in:
@@ -1,49 +1,93 @@
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using System.Text.Json;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegrator.Hosting.Web.Components;
|
||||
using Telegrator.MadiatorCore;
|
||||
|
||||
namespace Telegrator.Hosting.Web.Polling
|
||||
{
|
||||
/// <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 ITelegramBotWebHost _botHost;
|
||||
private readonly ITelegramBotClient _botClient;
|
||||
private readonly IUpdateRouter _updateRouter;
|
||||
private readonly TelegramBotWebOptions _options;
|
||||
|
||||
/// <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(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");
|
||||
|
||||
if (string.IsNullOrEmpty(options.Value.WebhookPattern))
|
||||
throw new ArgumentNullException(nameof(options), "Option \"WebhookPattern\" must be set to subscribe for update recieving");
|
||||
|
||||
_botHost = botHost;
|
||||
_botClient = botClient;
|
||||
_updateRouter = updateRouter;
|
||||
_options = options.Value;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
string pattern = new UriBuilder(_options.WebhookUri).Path;
|
||||
_botHost.MapPost(pattern, (Delegate)ReceiveUpdate);
|
||||
|
||||
_botClient.SetWebhook(
|
||||
url: _options.WebhookUri,
|
||||
maxConnections: _options.MaxConnections,
|
||||
allowedUpdates: _botHost.UpdateRouter.HandlersProvider.AllowedTypes,
|
||||
dropPendingUpdates: _options.DropPendingUpdates,
|
||||
cancellationToken: cancellationToken);
|
||||
cancellationToken: cancellationToken)
|
||||
.Wait(cancellationToken);
|
||||
|
||||
//botHost.MapGet(_options.WebhookPattern, async (Update update) => await _updateRouter.HandleUpdateAsync(_botClient, update, cancellationToken));
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <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 })
|
||||
return Results.BadRequest();
|
||||
|
||||
await _updateRouter.HandleUpdateAsync(_botClient, update, ctx.RequestAborted);
|
||||
return Results.Ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user