From 3d2d21f6c03c27024f05572c671fa7a7bb5ef573 Mon Sep 17 00:00:00 2001 From: Rikitav Date: Sun, 15 Mar 2026 17:20:53 +0400 Subject: [PATCH] * Added building delegate to UseTelegrator method * Fixed Webhooker trying to resolve IEndpointRouteBuildr from DI --- docs/Telegrator.Hosting.Web.xml | 25 ++++++++++++-- docs/Telegrator.Hosting.xml | 20 ++++++++--- .../Telegartor.RedisStateStorage.csproj | 2 +- .../Hosting.Web/TelegramBotWebHost.cs | 16 ++++----- .../Hosting.Web/TelegramBotWebHostBuilder.cs | 10 +----- .../Mediation/HostedUpdateWebhooker.cs | 17 ++++++---- .../Telegrator.Hosting.Web.csproj | 2 +- src/Telegrator.Hosting.Web/TypesExtensions.cs | 33 ++++++++++++++++--- .../Hosting/ITelegramBotHostBuilder.cs | 12 +++++++ .../Hosting/TelegramBotHostBuilder.cs | 12 ++----- .../Telegrator.Hosting.csproj | 2 +- src/Telegrator.Hosting/TypesExtensions.cs | 32 +++++++++++++----- src/Telegrator/Telegrator.csproj | 2 +- 13 files changed, 127 insertions(+), 58 deletions(-) create mode 100644 src/Telegrator.Hosting/Hosting/ITelegramBotHostBuilder.cs diff --git a/docs/Telegrator.Hosting.Web.xml b/docs/Telegrator.Hosting.Web.xml index 83cc9a4..36a48b9 100644 --- a/docs/Telegrator.Hosting.Web.xml +++ b/docs/Telegrator.Hosting.Web.xml @@ -28,6 +28,9 @@ This application's logger + + + Initializes a new instance of the class. @@ -181,7 +184,7 @@ Service for receiving updates for Hosted telegram bots via Webhooks - + Initiallizes new instance of @@ -197,6 +200,12 @@ + + + Maps bot webhook to application builder + + + Contains extensions for @@ -211,12 +220,22 @@ - + Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers. - + + + Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers. + + + + + Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers. + + + Replaces the initialization logic from TelegramBotWebHost constructor. Initializes the bot and logs handlers on application startup. diff --git a/docs/Telegrator.Hosting.xml b/docs/Telegrator.Hosting.xml index 10761a7..d9a9352 100644 --- a/docs/Telegrator.Hosting.xml +++ b/docs/Telegrator.Hosting.xml @@ -255,15 +255,25 @@ The key used to store the in the builder properties. - - + + - + - Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers. + Replaces TelegramBotHostBuilder. Configures DI, options, and handlers. - + + + Replaces TelegramBotHostBuilder. Configures DI, options, and handlers. + + + + + Replaces TelegramBotHostBuilder. Configures DI, options, and handlers. + + + Gets the from the builder properties. diff --git a/src/Telegartor.RedisStateStorage/Telegartor.RedisStateStorage.csproj b/src/Telegartor.RedisStateStorage/Telegartor.RedisStateStorage.csproj index 94d709c..67db6f8 100644 --- a/src/Telegartor.RedisStateStorage/Telegartor.RedisStateStorage.csproj +++ b/src/Telegartor.RedisStateStorage/Telegartor.RedisStateStorage.csproj @@ -15,7 +15,7 @@ True Telegrator.RedisStateStorage - 1.16.5 + 1.16.6 Rikitav Tim4ik Rikitav Tim4ik https://github.com/Rikitav/Telegrator diff --git a/src/Telegrator.Hosting.Web/Hosting.Web/TelegramBotWebHost.cs b/src/Telegrator.Hosting.Web/Hosting.Web/TelegramBotWebHost.cs index f5abd80..13eab68 100644 --- a/src/Telegrator.Hosting.Web/Hosting.Web/TelegramBotWebHost.cs +++ b/src/Telegrator.Hosting.Web/Hosting.Web/TelegramBotWebHost.cs @@ -39,11 +39,13 @@ public class TelegramBotWebHost : IHost, IApplicationBuilder, IEndpointRouteBuil /// public ILogger Logger => _logger; + /// + public IDictionary Properties => ((IApplicationBuilder)_innerApp).Properties; + // Private interface fields IServiceProvider IEndpointRouteBuilder.ServiceProvider => Services; IServiceProvider IApplicationBuilder.ApplicationServices { get => Services; set => throw new NotImplementedException(); } IFeatureCollection IApplicationBuilder.ServerFeatures => ((IApplicationBuilder)_innerApp).ServerFeatures; - IDictionary IApplicationBuilder.Properties => ((IApplicationBuilder)_innerApp).Properties; /// /// Initializes a new instance of the class. @@ -68,9 +70,7 @@ public class TelegramBotWebHost : IHost, IApplicationBuilder, IEndpointRouteBuil ArgumentNullException.ThrowIfNull(settings, nameof(settings)); WebApplicationBuilder innerApp = WebApplication.CreateBuilder(settings); TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp, settings); - - builder.Services.AddTelegramBotHostDefaults(); - builder.Services.AddTelegramWebhook(); + builder.AddTelegratorWeb(); return builder; } @@ -83,9 +83,7 @@ public class TelegramBotWebHost : IHost, IApplicationBuilder, IEndpointRouteBuil ArgumentNullException.ThrowIfNull(settings, nameof(settings)); WebApplicationBuilder innerApp = WebApplication.CreateSlimBuilder(settings); TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp, settings); - - builder.Services.AddTelegramBotHostDefaults(); - builder.Services.AddTelegramWebhook(); + builder.AddTelegratorWeb(); return builder; } @@ -97,7 +95,9 @@ public class TelegramBotWebHost : IHost, IApplicationBuilder, IEndpointRouteBuil { ArgumentNullException.ThrowIfNull(settings, nameof(settings)); WebApplicationBuilder innerApp = WebApplication.CreateEmptyBuilder(settings); - return new TelegramBotWebHostBuilder(innerApp, settings); + TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp, settings); + builder.AddTelegratorWeb(); + return builder; } /// diff --git a/src/Telegrator.Hosting.Web/Hosting.Web/TelegramBotWebHostBuilder.cs b/src/Telegrator.Hosting.Web/Hosting.Web/TelegramBotWebHostBuilder.cs index c561bcd..752cfd7 100644 --- a/src/Telegrator.Hosting.Web/Hosting.Web/TelegramBotWebHostBuilder.cs +++ b/src/Telegrator.Hosting.Web/Hosting.Web/TelegramBotWebHostBuilder.cs @@ -12,7 +12,7 @@ namespace Telegrator.Hosting.Web; /// /// Represents a web hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more. /// -public class TelegramBotWebHostBuilder : IHostApplicationBuilder, ICollectingProvider +public class TelegramBotWebHostBuilder : ITelegramBotHostBuilder { private readonly WebApplicationBuilder _innerBuilder; private readonly WebApplicationOptions _settings; @@ -48,8 +48,6 @@ public class TelegramBotWebHostBuilder : IHostApplicationBuilder, ICollectingPro { _innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder)); _settings = settings ?? throw new ArgumentNullException(nameof(settings)); - - this.AddTelegratorWeb(); } /// @@ -62,8 +60,6 @@ public class TelegramBotWebHostBuilder : IHostApplicationBuilder, ICollectingPro { _innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder)); _settings = settings ?? throw new ArgumentNullException(nameof(settings)); - - this.AddTelegratorWeb(options, null); } /// @@ -76,8 +72,6 @@ public class TelegramBotWebHostBuilder : IHostApplicationBuilder, ICollectingPro { _innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder)); _settings = settings ?? throw new ArgumentNullException(nameof(settings)); - - this.AddTelegratorWeb(null, handlers); } /// @@ -91,8 +85,6 @@ public class TelegramBotWebHostBuilder : IHostApplicationBuilder, ICollectingPro { _innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder)); _settings = settings ?? throw new ArgumentNullException(nameof(settings)); - - this.AddTelegratorWeb(options, handlers); } /// diff --git a/src/Telegrator.Hosting.Web/Mediation/HostedUpdateWebhooker.cs b/src/Telegrator.Hosting.Web/Mediation/HostedUpdateWebhooker.cs index 369295f..dca6cbf 100644 --- a/src/Telegrator.Hosting.Web/Mediation/HostedUpdateWebhooker.cs +++ b/src/Telegrator.Hosting.Web/Mediation/HostedUpdateWebhooker.cs @@ -19,7 +19,6 @@ 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; @@ -32,12 +31,11 @@ public class HostedUpdateWebhooker : IHostedService /// /// /// - public HostedUpdateWebhooker(IEndpointRouteBuilder botHost, ITelegramBotClient botClient, IUpdateRouter updateRouter, IOptions options) + public HostedUpdateWebhooker(ITelegramBotClient botClient, IUpdateRouter updateRouter, IOptions 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; @@ -52,9 +50,6 @@ public class HostedUpdateWebhooker : IHostedService 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, @@ -71,6 +66,16 @@ public class HostedUpdateWebhooker : IHostedService return Task.CompletedTask; } + /// + /// Maps bot webhook to application builder + /// + /// + public void MapWebhook(IEndpointRouteBuilder routeBuilder) + { + string pattern = new UriBuilder(_options.WebhookUri).Path; + routeBuilder.MapPost(pattern, (Delegate)ReceiveUpdate); + } + private async Task ReceiveUpdate(HttpContext ctx) { if (_options.SecretToken != null) diff --git a/src/Telegrator.Hosting.Web/Telegrator.Hosting.Web.csproj b/src/Telegrator.Hosting.Web/Telegrator.Hosting.Web.csproj index 6084a91..dd09b44 100644 --- a/src/Telegrator.Hosting.Web/Telegrator.Hosting.Web.csproj +++ b/src/Telegrator.Hosting.Web/Telegrator.Hosting.Web.csproj @@ -15,7 +15,7 @@ True Telegrator.Hosting.Web - 1.16.5 + 1.16.6 Rikitav Tim4ik Rikitav Tim4ik https://github.com/Rikitav/Telegrator diff --git a/src/Telegrator.Hosting.Web/TypesExtensions.cs b/src/Telegrator.Hosting.Web/TypesExtensions.cs index f55bff0..8317189 100644 --- a/src/Telegrator.Hosting.Web/TypesExtensions.cs +++ b/src/Telegrator.Hosting.Web/TypesExtensions.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; @@ -42,7 +43,27 @@ namespace Telegrator /// /// Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers. /// - public static IHostApplicationBuilder AddTelegratorWeb(this IHostApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null) + public static ITelegramBotHostBuilder AddTelegratorWeb(this ITelegramBotHostBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null, Action? action = null) + { + builder.AddTelegratorWebInternal(options, handlers); + action?.Invoke(builder); + return builder; + } + + /// + /// Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers. + /// + public static IHostApplicationBuilder AddTelegratorWeb(this WebApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null, Action? action = null) + { + builder.AddTelegratorWebInternal(options, handlers); + action?.Invoke(new TelegramBotWebHostBuilder(builder)); + return builder; + } + + /// + /// Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers. + /// + internal static IHostApplicationBuilder AddTelegratorWebInternal(this IHostApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null) { IServiceCollection services = builder.Services; IConfigurationManager configuration = builder.Configuration; @@ -103,11 +124,12 @@ namespace Telegrator /// Replaces the initialization logic from TelegramBotWebHost constructor. /// Initializes the bot and logs handlers on application startup. /// - public static WebApplication UseTelegratorWeb(this WebApplication app) + public static T UseTelegratorWeb(this T app) where T : IEndpointRouteBuilder, IHost { - ITelegramBotInfo info = app.Services.GetRequiredService(); - IHandlersCollection handlers = app.Services.GetRequiredService(); - ILoggerFactory loggerFactory = app.Services.GetRequiredService(); + HostedUpdateWebhooker webhooker = app.ServiceProvider.GetRequiredService(); + ITelegramBotInfo info = app.ServiceProvider.GetRequiredService(); + IHandlersCollection handlers = app.ServiceProvider.GetRequiredService(); + ILoggerFactory loggerFactory = app.ServiceProvider.GetRequiredService(); ILogger logger = loggerFactory.CreateLogger("Telegrator.Hosting.Web.TelegratorHost"); if (logger.IsEnabled(LogLevel.Information)) @@ -117,6 +139,7 @@ namespace Telegrator logger.LogHandlers(handlers); } + webhooker.MapWebhook(app); return app; } diff --git a/src/Telegrator.Hosting/Hosting/ITelegramBotHostBuilder.cs b/src/Telegrator.Hosting/Hosting/ITelegramBotHostBuilder.cs new file mode 100644 index 0000000..1d56912 --- /dev/null +++ b/src/Telegrator.Hosting/Hosting/ITelegramBotHostBuilder.cs @@ -0,0 +1,12 @@ +using Microsoft.Extensions.Hosting; +using System; +using System.Collections.Generic; +using System.Text; +using Telegrator.Core; + +namespace Telegrator.Hosting; + +public interface ITelegramBotHostBuilder : IHostApplicationBuilder, ICollectingProvider +{ + +} diff --git a/src/Telegrator.Hosting/Hosting/TelegramBotHostBuilder.cs b/src/Telegrator.Hosting/Hosting/TelegramBotHostBuilder.cs index c357e7b..485e0fc 100644 --- a/src/Telegrator.Hosting/Hosting/TelegramBotHostBuilder.cs +++ b/src/Telegrator.Hosting/Hosting/TelegramBotHostBuilder.cs @@ -11,7 +11,7 @@ namespace Telegrator.Hosting; /// /// Represents a hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more. /// -public class TelegramBotHostBuilder : IHostApplicationBuilder, ICollectingProvider +public class TelegramBotHostBuilder : ITelegramBotHostBuilder { private readonly HostApplicationBuilder _innerBuilder; private readonly HostApplicationBuilderSettings _settings; @@ -47,8 +47,6 @@ public class TelegramBotHostBuilder : IHostApplicationBuilder, ICollectingProvid { _innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder)); _settings = settings ?? new HostApplicationBuilderSettings(); - - this.AddTelegrator(); } /// @@ -61,8 +59,6 @@ public class TelegramBotHostBuilder : IHostApplicationBuilder, ICollectingProvid { _innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder)); _settings = settings ?? new HostApplicationBuilderSettings(); - - this.AddTelegrator(options, null); } /// @@ -75,8 +71,6 @@ public class TelegramBotHostBuilder : IHostApplicationBuilder, ICollectingProvid { _innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder)); _settings = settings ?? new HostApplicationBuilderSettings(); - - this.AddTelegrator(null, handlers); } /// @@ -90,8 +84,6 @@ public class TelegramBotHostBuilder : IHostApplicationBuilder, ICollectingProvid { _innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder)); _settings = settings ?? new HostApplicationBuilderSettings(); - - this.AddTelegrator(options, handlers); } /// @@ -108,6 +100,6 @@ public class TelegramBotHostBuilder : IHostApplicationBuilder, ICollectingProvid /// public void ConfigureContainer(IServiceProviderFactory factory, Action? configure = null) where TContainerBuilder : notnull { - this.ConfigureContainer(factory, configure); + _innerBuilder.ConfigureContainer(factory, configure); } } diff --git a/src/Telegrator.Hosting/Telegrator.Hosting.csproj b/src/Telegrator.Hosting/Telegrator.Hosting.csproj index 2e01884..8f6f61e 100644 --- a/src/Telegrator.Hosting/Telegrator.Hosting.csproj +++ b/src/Telegrator.Hosting/Telegrator.Hosting.csproj @@ -15,7 +15,7 @@ True Telegrator.Hosting - 1.16.5 + 1.16.6 Rikitav Tim4ik Rikitav Tim4ik https://github.com/Rikitav/Telegrator diff --git a/src/Telegrator.Hosting/TypesExtensions.cs b/src/Telegrator.Hosting/TypesExtensions.cs index 2612e0b..1b10664 100644 --- a/src/Telegrator.Hosting/TypesExtensions.cs +++ b/src/Telegrator.Hosting/TypesExtensions.cs @@ -30,7 +30,7 @@ public static class HostBuilderExtensions /// public const string HandlersCollectionPropertyKey = nameof(IHandlersCollection); - extension (IHostApplicationBuilder builder) + extension (HostApplicationBuilder builder) { /// /// Gets the from the builder properties. @@ -39,18 +39,35 @@ public static class HostBuilderExtensions { get { - if (builder is TelegramBotHostBuilder botHostBuilder) - return botHostBuilder.Handlers; - - return (IHandlersCollection)builder.Properties[HandlersCollectionPropertyKey]; + return (IHandlersCollection)((IHostApplicationBuilder)builder).Properties[HandlersCollectionPropertyKey]; } } } /// - /// Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers. + /// Replaces TelegramBotHostBuilder. Configures DI, options, and handlers. /// - public static IHostApplicationBuilder AddTelegrator(this IHostApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null) + public static ITelegramBotHostBuilder AddTelegrator(this ITelegramBotHostBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null, Action? action = null) + { + builder.AddTelegratorInternal(options, handlers); + action?.Invoke(builder); + return builder; + } + + /// + /// Replaces TelegramBotHostBuilder. Configures DI, options, and handlers. + /// + public static IHostApplicationBuilder AddTelegrator(this HostApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null, Action? action = null) + { + builder.AddTelegratorInternal(options, handlers); + action?.Invoke(new TelegramBotHostBuilder(builder)); + return builder; + } + + /// + /// Replaces TelegramBotHostBuilder. Configures DI, options, and handlers. + /// + public static IHostApplicationBuilder AddTelegratorInternal(this IHostApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null) { IServiceCollection services = builder.Services; IConfigurationManager configuration = builder.Configuration; @@ -133,7 +150,6 @@ public static class ServicesCollectionExtensions /// public static IServiceCollection AddTelegramBotHostDefaults(this IServiceCollection services) { - services.AddLogging(builder => builder.AddConsole().AddDebug()); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); diff --git a/src/Telegrator/Telegrator.csproj b/src/Telegrator/Telegrator.csproj index 037ca42..ec54821 100644 --- a/src/Telegrator/Telegrator.csproj +++ b/src/Telegrator/Telegrator.csproj @@ -14,7 +14,7 @@ True Telegrator : Telegram.Bot mediator framework - 1.16.5 + 1.16.6 Rikitav Tim4ik Rikitav Tim4ik https://github.com/Rikitav/Telegrator