* Added Result class to communicate with router from handler

* Removed "ExecuteOnlyFirstFoundHanlder" in sake of testing new Result pattern based routing system
* Removed obsolete option property "DescendDescriptorIndex"
* Changed router logic
* Changed handlers pool logic
This commit is contained in:
2025-08-02 02:32:38 +04:00
parent b8e4398b50
commit 16d11990ec
26 changed files with 347 additions and 115 deletions
@@ -1,6 +1,5 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;
@@ -22,7 +21,7 @@ namespace Telegrator.Hosting.Web.Polling
private readonly ITelegramBotWebHost _botHost;
private readonly ITelegramBotClient _botClient;
private readonly IUpdateRouter _updateRouter;
private readonly TelegramBotWebOptions _options;
private readonly TelegratorWebOptions _options;
/// <summary>
/// Initiallizes new instance of <see cref="HostedUpdateWebhooker"/>
@@ -32,7 +31,7 @@ namespace Telegrator.Hosting.Web.Polling
/// <param name="updateRouter"></param>
/// <param name="options"></param>
/// <exception cref="ArgumentNullException"></exception>
public HostedUpdateWebhooker(ITelegramBotWebHost botHost, ITelegramBotClient botClient, IUpdateRouter updateRouter, IOptions<TelegramBotWebOptions> options)
public HostedUpdateWebhooker(ITelegramBotWebHost botHost, ITelegramBotClient botClient, IUpdateRouter updateRouter, IOptions<TelegratorWebOptions> options)
{
if (string.IsNullOrEmpty(options.Value.WebhookUri))
throw new ArgumentNullException(nameof(options), "Option \"WebhookUrl\" must be set to subscribe for update recieving");
+14 -4
View File
@@ -5,8 +5,10 @@ using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Text;
using Telegram.Bot.Types.Enums;
using Telegrator.Configuration;
using Telegrator.Hosting.Components;
using Telegrator.Hosting.Providers;
using Telegrator.Hosting.Web.Components;
@@ -22,7 +24,7 @@ namespace Telegrator.Hosting.Web
{
private readonly WebApplication _innerApp;
private readonly IUpdateRouter _updateRouter;
private readonly ILogger<TelegramBotHost> _logger;
private readonly ILogger<TelegramBotWebHost> _logger;
private bool _disposed;
@@ -43,7 +45,7 @@ namespace Telegrator.Hosting.Web
/// <summary>
/// This application's logger
/// </summary>
public ILogger<TelegramBotHost> Logger => _logger;
public ILogger<TelegramBotWebHost> Logger => _logger;
// Private interface fields
IServiceProvider IEndpointRouteBuilder.ServiceProvider => Services;
@@ -53,12 +55,20 @@ namespace Telegrator.Hosting.Web
internal TelegramBotWebHost(WebApplicationBuilder webApplicationBuilder, HostHandlersCollection handlers)
{
// Registering this host in services for easy access
RegisterHostServices(webApplicationBuilder, handlers);
// Building proxy application
_innerApp = webApplicationBuilder.Build();
_updateRouter = Services.GetRequiredService<IUpdateRouter>();
_logger = Services.GetRequiredService<ILogger<TelegramBotHost>>();
// Initializing bot info, as it requires to make a request via tg bot
Services.GetRequiredService<ITelegramBotInfo>();
// Reruesting services for this host
_updateRouter = Services.GetRequiredService<IUpdateRouter>();
_logger = Services.GetRequiredService<ILogger<TelegramBotWebHost>>();
// Logging registering handlers in DEBUG purposes
LogHandlers(handlers);
}
@@ -43,10 +43,6 @@ namespace Telegrator.Hosting.Web
_innerBuilder = webApplicationBuilder;
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
_handlers = new HostHandlersCollection(Services, _settings);
Services.AddSingleton<IOptions<TelegramBotWebOptions>>(Options.Create(settings));
Services.Configure<TelegratorOptions>(Configuration.GetSection(nameof(TelegratorOptions)));
Services.Configure<TelegramBotClientOptions>(Configuration.GetSection(nameof(TelegramBotClientOptions)), new TelegramBotClientOptionsProxy());
}
/// <summary>
@@ -67,6 +63,14 @@ namespace Telegrator.Hosting.Web
}
}
if (!_settings.DisableAutoConfigure)
{
Services.Configure<TelegratorWebOptions>(Configuration.GetSection(nameof(TelegratorWebOptions)));
Services.Configure<TelegratorOptions>(Configuration.GetSection(nameof(TelegratorOptions)));
Services.Configure<TelegramBotClientOptions>(Configuration.GetSection(nameof(TelegramBotClientOptions)), new TelegramBotClientOptionsProxy());
}
Services.AddSingleton<IOptions<TelegratorOptions>>(Options.Create(_settings));
return new TelegramBotWebHost(_innerBuilder, _handlers);
}
}
@@ -1,37 +1,18 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Options;
using System.Diagnostics.CodeAnalysis;
namespace Telegrator.Hosting.Web
{
/// <summary>
/// Configuration options for Telegram bot behavior and execution settings.
/// Controls various aspects of bot operation including concurrency, routing, webhook receiving, and execution policies.
/// Options for configuring the behavior for TelegramBotWebHost.
/// </summary>
public class TelegramBotWebOptions : TelegratorOptions
{
/// <summary>
/// Gets or sets HTTPS URL to send updates to. Use an empty string to remove webhook integration
/// Disables automatic configuration for all of required <see cref="IOptions{TOptions}"/> instances
/// </summary>
[StringSyntax(StringSyntaxAttribute.Uri)]
public required string WebhookUri { get; set; }
/// <summary>
/// A secret token to be sent in a header “X-Telegram-Bot-Api-Secret-Token” in every webhook request, 1-256 characters.
/// Only characters A-Z, a-z, 0-9, _ and - are allowed.
/// The header is useful to ensure that the request comes from a webhook set by you.
/// </summary>
public string? SecretToken { get; set; }
/// <summary>
/// The maximum allowed number of simultaneous HTTPS connections to the webhook for update delivery, 1-100. Defaults to 40.
/// Use lower values to limit the load on your bot's server, and higher values to increase your bot's throughput.
/// </summary>
public int MaxConnections { get; set; } = 40;
/// <summary>
/// Pass true to drop all pending updates
/// </summary>
public bool DropPendingUpdates { get; set; }
public bool DisableAutoConfigure { get; set; }
/// <inheritdoc cref="WebApplicationOptions.Args"/>
public string[]? Args { get; init; }
@@ -0,0 +1,35 @@
using System.Diagnostics.CodeAnalysis;
namespace Telegrator.Hosting.Web
{
/// <summary>
/// Configuration options for Telegram bot behavior and execution settings.
/// Controls various aspects of bot operation including concurrency, routing, webhook receiving, and execution policies.
/// </summary>
public class TelegratorWebOptions
{
/// <summary>
/// Gets or sets HTTPS URL to send updates to. Use an empty string to remove webhook integration
/// </summary>
[StringSyntax(StringSyntaxAttribute.Uri)]
public required string WebhookUri { get; set; }
/// <summary>
/// A secret token to be sent in a header “X-Telegram-Bot-Api-Secret-Token” in every webhook request, 1-256 characters.
/// Only characters A-Z, a-z, 0-9, _ and - are allowed.
/// The header is useful to ensure that the request comes from a webhook set by you.
/// </summary>
public string? SecretToken { get; set; }
/// <summary>
/// The maximum allowed number of simultaneous HTTPS connections to the webhook for update delivery, 1-100. Defaults to 40.
/// Use lower values to limit the load on your bot's server, and higher values to increase your bot's throughput.
/// </summary>
public int MaxConnections { get; set; } = 40;
/// <summary>
/// Pass true to drop all pending updates
/// </summary>
public bool DropPendingUpdates { get; set; }
}
}