4 Commits

Author SHA1 Message Date
Rikitav 233a67f2b0 * Added RemapWebhook method to WebHost
* Updatetd dependencues
* Fixed some warnings
2026-04-03 19:23:53 +04:00
Rikitav ba772f3f7c * Fixed wrong UseTelegrator method using in web host
* Fixed HostedUpdateWebhooker service discovery
2026-03-15 19:23:56 +04:00
Rikitav ee78703cd1 Update README.md 2026-03-15 17:56:05 +04:00
Rikitav 3d2d21f6c0 * Added building delegate to UseTelegrator method
* Fixed Webhooker trying to resolve IEndpointRouteBuildr from DI
2026-03-15 17:20:53 +04:00
15 changed files with 289 additions and 213 deletions
+4
View File
@@ -8,6 +8,10 @@ Telegrator is a modern C# framework for building Telegram bots, inspired by AOP
--- ---
The documentation may not be completely transparent, informative or even actual to latest version, so if you have any questions or problems, please write them in the [Telegram.Bot](https://t.me/joinchat/B35YY0QbLfd034CFnvCtCA) group. I am a member of this group and will notice you! If you have any suggestions or want to participate in building documentation, make push requests and open issues on this repository!
---
## ✨ Key Features ## ✨ Key Features
- **Aspect-oriented approach**: Handlers and filters are "aspects" of the bot, easily composable and extendable. - **Aspect-oriented approach**: Handlers and filters are "aspects" of the bot, easily composable and extendable.
+52 -28
View File
@@ -28,6 +28,9 @@
This application's logger This application's logger
</summary> </summary>
</member> </member>
<member name="P:Telegrator.Hosting.Web.TelegramBotWebHost.Properties">
<inheritdoc/>
</member>
<member name="M:Telegrator.Hosting.Web.TelegramBotWebHost.#ctor(Microsoft.AspNetCore.Builder.WebApplicationBuilder)"> <member name="M:Telegrator.Hosting.Web.TelegramBotWebHost.#ctor(Microsoft.AspNetCore.Builder.WebApplicationBuilder)">
<summary> <summary>
Initializes a new instance of the <see cref="T:Microsoft.AspNetCore.Builder.WebApplicationBuilder"/> class. Initializes a new instance of the <see cref="T:Microsoft.AspNetCore.Builder.WebApplicationBuilder"/> class.
@@ -81,9 +84,7 @@
</summary> </summary>
</member> </member>
<member name="T:Telegrator.Hosting.Web.TelegramBotWebHostBuilder"> <member name="T:Telegrator.Hosting.Web.TelegramBotWebHostBuilder">
<summary> <inheritdoc/>
Represents a web hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more.
</summary>
</member> </member>
<member name="P:Telegrator.Hosting.Web.TelegramBotWebHostBuilder.Handlers"> <member name="P:Telegrator.Hosting.Web.TelegramBotWebHostBuilder.Handlers">
<inheritdoc/> <inheritdoc/>
@@ -106,37 +107,18 @@
<member name="P:Telegrator.Hosting.Web.TelegramBotWebHostBuilder.Metrics"> <member name="P:Telegrator.Hosting.Web.TelegramBotWebHostBuilder.Metrics">
<inheritdoc/> <inheritdoc/>
</member> </member>
<member name="M:Telegrator.Hosting.Web.TelegramBotWebHostBuilder.#ctor(Microsoft.AspNetCore.Builder.WebApplicationBuilder,Microsoft.AspNetCore.Builder.WebApplicationOptions)"> <member name="M:Telegrator.Hosting.Web.TelegramBotWebHostBuilder.#ctor(Microsoft.AspNetCore.Builder.WebApplicationBuilder)">
<summary> <summary>
Initializes a new instance of the <see cref="T:Telegrator.Hosting.Web.TelegramBotWebHostBuilder"/> class. Initializes a new instance of the <see cref="T:Telegrator.Hosting.Web.TelegramBotWebHostBuilder"/> class.
</summary> </summary>
<param name="webApplicationBuilder"></param> <param name="webApplicationBuilder"></param>
<param name="settings"></param>
</member> </member>
<member name="M:Telegrator.Hosting.Web.TelegramBotWebHostBuilder.#ctor(Microsoft.AspNetCore.Builder.WebApplicationBuilder,Telegrator.TelegratorOptions,Microsoft.AspNetCore.Builder.WebApplicationOptions)"> <member name="M:Telegrator.Hosting.Web.TelegramBotWebHostBuilder.#ctor(Microsoft.AspNetCore.Builder.WebApplicationBuilder,Telegrator.Core.IHandlersCollection)">
<summary>
Initializes a new instance of the <see cref="T:Telegrator.Hosting.Web.TelegramBotWebHostBuilder"/> class.
</summary>
<param name="webApplicationBuilder"></param>
<param name="options"></param>
<param name="settings"></param>
</member>
<member name="M:Telegrator.Hosting.Web.TelegramBotWebHostBuilder.#ctor(Microsoft.AspNetCore.Builder.WebApplicationBuilder,Telegrator.Core.IHandlersCollection,Microsoft.AspNetCore.Builder.WebApplicationOptions)">
<summary> <summary>
Initializes a new instance of the <see cref="T:Telegrator.Hosting.Web.TelegramBotWebHostBuilder"/> class. Initializes a new instance of the <see cref="T:Telegrator.Hosting.Web.TelegramBotWebHostBuilder"/> class.
</summary> </summary>
<param name="webApplicationBuilder"></param> <param name="webApplicationBuilder"></param>
<param name="handlers"></param> <param name="handlers"></param>
<param name="settings"></param>
</member>
<member name="M:Telegrator.Hosting.Web.TelegramBotWebHostBuilder.#ctor(Microsoft.AspNetCore.Builder.WebApplicationBuilder,Telegrator.Core.IHandlersCollection,Telegrator.TelegratorOptions,Microsoft.AspNetCore.Builder.WebApplicationOptions)">
<summary>
Initializes a new instance of the <see cref="T:Telegrator.Hosting.Web.TelegramBotWebHostBuilder"/> class.
</summary>
<param name="webApplicationBuilder"></param>
<param name="handlers"></param>
<param name="options"></param>
<param name="settings"></param>
</member> </member>
<member name="M:Telegrator.Hosting.Web.TelegramBotWebHostBuilder.Build"> <member name="M:Telegrator.Hosting.Web.TelegramBotWebHostBuilder.Build">
<summary> <summary>
@@ -181,11 +163,10 @@
Service for receiving updates for Hosted telegram bots via Webhooks Service for receiving updates for Hosted telegram bots via Webhooks
</summary> </summary>
</member> </member>
<member name="M:Telegrator.Mediation.HostedUpdateWebhooker.#ctor(Microsoft.AspNetCore.Routing.IEndpointRouteBuilder,Telegram.Bot.ITelegramBotClient,Telegrator.Core.IUpdateRouter,Microsoft.Extensions.Options.IOptions{Telegrator.Hosting.Web.WebhookerOptions})"> <member name="M:Telegrator.Mediation.HostedUpdateWebhooker.#ctor(Telegram.Bot.ITelegramBotClient,Telegrator.Core.IUpdateRouter,Microsoft.Extensions.Options.IOptions{Telegrator.Hosting.Web.WebhookerOptions})">
<summary> <summary>
Initiallizes new instance of <see cref="T:Telegrator.Mediation.HostedUpdateWebhooker"/> Initiallizes new instance of <see cref="T:Telegrator.Mediation.HostedUpdateWebhooker"/>
</summary> </summary>
<param name="botHost"></param>
<param name="botClient"></param> <param name="botClient"></param>
<param name="updateRouter"></param> <param name="updateRouter"></param>
<param name="options"></param> <param name="options"></param>
@@ -197,6 +178,22 @@
<member name="M:Telegrator.Mediation.HostedUpdateWebhooker.StopAsync(System.Threading.CancellationToken)"> <member name="M:Telegrator.Mediation.HostedUpdateWebhooker.StopAsync(System.Threading.CancellationToken)">
<inheritdoc/> <inheritdoc/>
</member> </member>
<member name="M:Telegrator.Mediation.HostedUpdateWebhooker.RemapWebhook(Microsoft.AspNetCore.Routing.IEndpointRouteBuilder,System.String,System.Threading.CancellationToken)">
<summary>
Allows to remap receiving webhook endpoint and map new route to webhost.
</summary>
<param name="routeBuilder"></param>
<param name="webhookUri"></param>
<param name="cancellationToken"></param>
<returns></returns>
<exception cref="T:System.ArgumentException"></exception>
</member>
<member name="M:Telegrator.Mediation.HostedUpdateWebhooker.MapWebhook(Microsoft.AspNetCore.Routing.IEndpointRouteBuilder)">
<summary>
Maps bot webhook to application builder
</summary>
<param name="routeBuilder"></param>
</member>
<member name="T:Telegrator.ServicesCollectionExtensions"> <member name="T:Telegrator.ServicesCollectionExtensions">
<summary> <summary>
Contains extensions for <see cref="T:Microsoft.Extensions.DependencyInjection.IServiceCollection"/> Contains extensions for <see cref="T:Microsoft.Extensions.DependencyInjection.IServiceCollection"/>
@@ -211,17 +208,44 @@
<member name="M:Telegrator.ServicesCollectionExtensions.get_Handlers(Microsoft.AspNetCore.Builder.WebApplicationBuilder)"> <member name="M:Telegrator.ServicesCollectionExtensions.get_Handlers(Microsoft.AspNetCore.Builder.WebApplicationBuilder)">
<inheritdoc cref="P:Telegrator.ServicesCollectionExtensions.&lt;G&gt;$41F16C2D39AF52899E745C9C9F42FF83.Handlers"/> <inheritdoc cref="P:Telegrator.ServicesCollectionExtensions.&lt;G&gt;$41F16C2D39AF52899E745C9C9F42FF83.Handlers"/>
</member> </member>
<member name="M:Telegrator.ServicesCollectionExtensions.AddTelegratorWeb(Microsoft.Extensions.Hosting.IHostApplicationBuilder,Telegrator.TelegratorOptions,Telegrator.Core.IHandlersCollection)"> <member name="M:Telegrator.ServicesCollectionExtensions.AddTelegratorWeb(Telegrator.Hosting.ITelegramBotHostBuilder,Telegrator.TelegratorOptions,Telegrator.Core.IHandlersCollection,System.Action{Telegrator.Hosting.ITelegramBotHostBuilder})">
<summary> <summary>
Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers. Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers.
</summary> </summary>
</member> </member>
<member name="M:Telegrator.ServicesCollectionExtensions.UseTelegratorWeb(Microsoft.AspNetCore.Builder.WebApplication)"> <member name="M:Telegrator.ServicesCollectionExtensions.AddTelegratorWeb(Microsoft.AspNetCore.Builder.WebApplicationBuilder,Telegrator.TelegratorOptions,Telegrator.Core.IHandlersCollection,System.Action{Telegrator.Hosting.ITelegramBotHostBuilder})">
<summary>
Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers.
</summary>
</member>
<member name="M:Telegrator.ServicesCollectionExtensions.AddTelegratorWebInternal(Microsoft.Extensions.DependencyInjection.IServiceCollection,Microsoft.Extensions.Configuration.IConfiguration,System.Collections.Generic.IDictionary{System.Object,System.Object},Telegrator.Core.IHandlersCollection@,Telegrator.TelegratorOptions)">
<summary>
Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers.
</summary>
</member>
<member name="M:Telegrator.ServicesCollectionExtensions.TryFindWebhooker(System.IServiceProvider,Telegrator.Mediation.HostedUpdateWebhooker@)">
<summary>
Searchs for <see cref="T:Telegrator.Mediation.HostedUpdateWebhooker"/> hosted service inside hosts services
</summary>
<param name="services"></param>
<param name="webhooker"></param>
<returns></returns>
</member>
<member name="M:Telegrator.ServicesCollectionExtensions.UseTelegratorWeb``1(``0,System.Boolean)">
<summary> <summary>
Replaces the initialization logic from TelegramBotWebHost constructor. Replaces the initialization logic from TelegramBotWebHost constructor.
Initializes the bot and logs handlers on application startup. Initializes the bot and logs handlers on application startup.
</summary> </summary>
</member> </member>
<member name="M:Telegrator.ServicesCollectionExtensions.RemapWebhook``1(``0,System.String)">
<summary>
Allows to remap receiving webhook endpoint and map new route to webhost.
</summary>
<param name="app"></param>
<param name="webhookUri"></param>
<returns></returns>
<exception cref="T:System.ArgumentException"></exception>
</member>
<member name="M:Telegrator.ServicesCollectionExtensions.AddTelegramWebhook(Microsoft.Extensions.DependencyInjection.IServiceCollection)"> <member name="M:Telegrator.ServicesCollectionExtensions.AddTelegramWebhook(Microsoft.Extensions.DependencyInjection.IServiceCollection)">
<summary> <summary>
Registers <see cref="T:Telegram.Bot.ITelegramBotClient"/> service with <see cref="T:Telegrator.Mediation.HostedUpdateWebhooker"/> to receive updates using webhook Registers <see cref="T:Telegram.Bot.ITelegramBotClient"/> service with <see cref="T:Telegrator.Mediation.HostedUpdateWebhooker"/> to receive updates using webhook
+32 -28
View File
@@ -35,6 +35,11 @@
Provides access to configuration of this Hosted telegram bot Provides access to configuration of this Hosted telegram bot
</summary> </summary>
</member> </member>
<member name="T:Telegrator.Hosting.ITelegramBotHostBuilder">
<summary>
Represents a hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more.
</summary>
</member>
<member name="T:Telegrator.Hosting.TelegramBotHost"> <member name="T:Telegrator.Hosting.TelegramBotHost">
<summary> <summary>
Represents a hosted telegram bot Represents a hosted telegram bot
@@ -93,9 +98,7 @@
</summary> </summary>
</member> </member>
<member name="T:Telegrator.Hosting.TelegramBotHostBuilder"> <member name="T:Telegrator.Hosting.TelegramBotHostBuilder">
<summary> <inheritdoc/>
Represents a hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more.
</summary>
</member> </member>
<member name="P:Telegrator.Hosting.TelegramBotHostBuilder.Handlers"> <member name="P:Telegrator.Hosting.TelegramBotHostBuilder.Handlers">
<inheritdoc/> <inheritdoc/>
@@ -118,37 +121,18 @@
<member name="P:Telegrator.Hosting.TelegramBotHostBuilder.Metrics"> <member name="P:Telegrator.Hosting.TelegramBotHostBuilder.Metrics">
<inheritdoc/> <inheritdoc/>
</member> </member>
<member name="M:Telegrator.Hosting.TelegramBotHostBuilder.#ctor(Microsoft.Extensions.Hosting.HostApplicationBuilder,Microsoft.Extensions.Hosting.HostApplicationBuilderSettings)"> <member name="M:Telegrator.Hosting.TelegramBotHostBuilder.#ctor(Microsoft.Extensions.Hosting.HostApplicationBuilder)">
<summary> <summary>
Initializes a new instance of the <see cref="T:Telegrator.Hosting.TelegramBotHostBuilder"/> class. Initializes a new instance of the <see cref="T:Telegrator.Hosting.TelegramBotHostBuilder"/> class.
</summary> </summary>
<param name="hostApplicationBuilder"></param> <param name="hostApplicationBuilder"></param>
<param name="settings"></param>
</member> </member>
<member name="M:Telegrator.Hosting.TelegramBotHostBuilder.#ctor(Microsoft.Extensions.Hosting.HostApplicationBuilder,Telegrator.TelegratorOptions,Microsoft.Extensions.Hosting.HostApplicationBuilderSettings)"> <member name="M:Telegrator.Hosting.TelegramBotHostBuilder.#ctor(Microsoft.Extensions.Hosting.HostApplicationBuilder,Telegrator.Core.IHandlersCollection)">
<summary>
Initializes a new instance of the <see cref="T:Telegrator.Hosting.TelegramBotHostBuilder"/> class.
</summary>
<param name="hostApplicationBuilder"></param>
<param name="options"></param>
<param name="settings"></param>
</member>
<member name="M:Telegrator.Hosting.TelegramBotHostBuilder.#ctor(Microsoft.Extensions.Hosting.HostApplicationBuilder,Telegrator.Core.IHandlersCollection,Microsoft.Extensions.Hosting.HostApplicationBuilderSettings)">
<summary> <summary>
Initializes a new instance of the <see cref="T:Telegrator.Hosting.TelegramBotHostBuilder"/> class. Initializes a new instance of the <see cref="T:Telegrator.Hosting.TelegramBotHostBuilder"/> class.
</summary> </summary>
<param name="hostApplicationBuilder"></param> <param name="hostApplicationBuilder"></param>
<param name="handlers"></param> <param name="handlers"></param>
<param name="settings"></param>
</member>
<member name="M:Telegrator.Hosting.TelegramBotHostBuilder.#ctor(Microsoft.Extensions.Hosting.HostApplicationBuilder,Telegrator.Core.IHandlersCollection,Telegrator.TelegratorOptions,Microsoft.Extensions.Hosting.HostApplicationBuilderSettings)">
<summary>
Initializes a new instance of the <see cref="T:Telegrator.Hosting.TelegramBotHostBuilder"/> class.
</summary>
<param name="hostApplicationBuilder"></param>
<param name="handlers"></param>
<param name="options"></param>
<param name="settings"></param>
</member> </member>
<member name="M:Telegrator.Hosting.TelegramBotHostBuilder.Build"> <member name="M:Telegrator.Hosting.TelegramBotHostBuilder.Build">
<summary> <summary>
@@ -255,15 +239,35 @@
The key used to store the <see cref="T:Telegrator.Core.IHandlersCollection"/> in the builder properties. The key used to store the <see cref="T:Telegrator.Core.IHandlersCollection"/> in the builder properties.
</summary> </summary>
</member> </member>
<member name="M:Telegrator.HostBuilderExtensions.get_Handlers(Microsoft.Extensions.Hosting.IHostApplicationBuilder)"> <member name="M:Telegrator.HostBuilderExtensions.get_Handlers(Microsoft.Extensions.Hosting.HostApplicationBuilder)">
<inheritdoc cref="P:Telegrator.HostBuilderExtensions.&lt;G&gt;$605D8CCF64349EA050C790D67C500BD9.Handlers"/> <inheritdoc cref="P:Telegrator.HostBuilderExtensions.&lt;G&gt;$BF7227490CCA365283B3A9274C9033C7.Handlers"/>
</member>
<member name="M:Telegrator.HostBuilderExtensions.AddTelegrator(Telegrator.Hosting.ITelegramBotHostBuilder,Telegrator.TelegratorOptions,Telegrator.Core.IHandlersCollection,System.Action{Telegrator.Hosting.ITelegramBotHostBuilder})">
<summary>
Replaces TelegramBotHostBuilder. Configures DI, options, and handlers.
</summary>
</member>
<member name="M:Telegrator.HostBuilderExtensions.AddTelegrator(Microsoft.Extensions.Hosting.HostApplicationBuilder,Telegrator.TelegratorOptions,Telegrator.Core.IHandlersCollection,System.Action{Telegrator.Hosting.ITelegramBotHostBuilder})">
<summary>
Replaces TelegramBotHostBuilder. Configures DI, options, and handlers.
</summary>
</member> </member>
<member name="M:Telegrator.HostBuilderExtensions.AddTelegrator(Microsoft.Extensions.Hosting.IHostApplicationBuilder,Telegrator.TelegratorOptions,Telegrator.Core.IHandlersCollection)"> <member name="M:Telegrator.HostBuilderExtensions.AddTelegrator(Microsoft.Extensions.Hosting.IHostApplicationBuilder,Telegrator.TelegratorOptions,Telegrator.Core.IHandlersCollection)">
<summary> <summary>
Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers. Replaces TelegramBotHostBuilder. Configures DI, options, and handlers.
</summary> </summary>
</member> </member>
<member name="P:Telegrator.HostBuilderExtensions.&lt;G&gt;$605D8CCF64349EA050C790D67C500BD9.Handlers"> <member name="M:Telegrator.HostBuilderExtensions.AddTelegrator(Microsoft.Extensions.Hosting.IHostBuilder,Telegrator.TelegratorOptions,Telegrator.Core.IHandlersCollection)">
<summary>
Replaces TelegramBotHostBuilder. Configures DI, options, and handlers.
</summary>
</member>
<member name="M:Telegrator.HostBuilderExtensions.AddTelegratorInternal(Microsoft.Extensions.DependencyInjection.IServiceCollection,Microsoft.Extensions.Configuration.IConfiguration,System.Collections.Generic.IDictionary{System.Object,System.Object},Telegrator.Core.IHandlersCollection@,Telegrator.TelegratorOptions)">
<summary>
Replaces TelegramBotHostBuilder. Configures DI, options, and handlers.
</summary>
</member>
<member name="P:Telegrator.HostBuilderExtensions.&lt;G&gt;$BF7227490CCA365283B3A9274C9033C7.Handlers">
<summary> <summary>
Gets the <see cref="T:Telegrator.Core.IHandlersCollection"/> from the builder properties. Gets the <see cref="T:Telegrator.Core.IHandlersCollection"/> from the builder properties.
</summary> </summary>
@@ -15,7 +15,7 @@
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild> <EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<Title>Telegrator.RedisStateStorage</Title> <Title>Telegrator.RedisStateStorage</Title>
<Version>1.16.5</Version> <Version>1.16.6</Version>
<Authors>Rikitav Tim4ik</Authors> <Authors>Rikitav Tim4ik</Authors>
<Company>Rikitav Tim4ik</Company> <Company>Rikitav Tim4ik</Company>
<RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl> <RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl>
@@ -31,7 +31,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="StackExchange.Redis" Version="2.11.8" /> <PackageReference Include="StackExchange.Redis" Version="2.12.14" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@@ -39,11 +39,13 @@ public class TelegramBotWebHost : IHost, IApplicationBuilder, IEndpointRouteBuil
/// </summary> /// </summary>
public ILogger<TelegramBotWebHost> Logger => _logger; public ILogger<TelegramBotWebHost> Logger => _logger;
/// <inheritdoc/>
public IDictionary<string, object?> Properties => ((IApplicationBuilder)_innerApp).Properties;
// Private interface fields // Private interface fields
IServiceProvider IEndpointRouteBuilder.ServiceProvider => Services; IServiceProvider IEndpointRouteBuilder.ServiceProvider => Services;
IServiceProvider IApplicationBuilder.ApplicationServices { get => Services; set => throw new NotImplementedException(); } IServiceProvider IApplicationBuilder.ApplicationServices { get => Services; set => throw new NotImplementedException(); }
IFeatureCollection IApplicationBuilder.ServerFeatures => ((IApplicationBuilder)_innerApp).ServerFeatures; IFeatureCollection IApplicationBuilder.ServerFeatures => ((IApplicationBuilder)_innerApp).ServerFeatures;
IDictionary<string, object?> IApplicationBuilder.Properties => ((IApplicationBuilder)_innerApp).Properties;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="WebApplicationBuilder"/> class. /// Initializes a new instance of the <see cref="WebApplicationBuilder"/> class.
@@ -63,14 +65,12 @@ public class TelegramBotWebHost : IHost, IApplicationBuilder, IEndpointRouteBuil
/// Creates new <see cref="TelegramBotHostBuilder"/> with default services and webhook update receiving scheme /// Creates new <see cref="TelegramBotHostBuilder"/> with default services and webhook update receiving scheme
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public static TelegramBotWebHostBuilder CreateBuilder(WebApplicationOptions settings) public static TelegramBotWebHostBuilder CreateBuilder(WebApplicationOptions? settings)
{ {
ArgumentNullException.ThrowIfNull(settings, nameof(settings)); ArgumentNullException.ThrowIfNull(settings, nameof(settings));
WebApplicationBuilder innerApp = WebApplication.CreateBuilder(settings); WebApplicationBuilder innerApp = WebApplication.CreateBuilder(settings);
TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp, settings); TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp);
builder.AddTelegratorWeb();
builder.Services.AddTelegramBotHostDefaults();
builder.Services.AddTelegramWebhook();
return builder; return builder;
} }
@@ -78,14 +78,12 @@ public class TelegramBotWebHost : IHost, IApplicationBuilder, IEndpointRouteBuil
/// Creates new SLIM <see cref="TelegramBotHostBuilder"/> with default services and webhook update receiving scheme /// Creates new SLIM <see cref="TelegramBotHostBuilder"/> with default services and webhook update receiving scheme
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public static TelegramBotWebHostBuilder CreateSlimBuilder(WebApplicationOptions settings) public static TelegramBotWebHostBuilder CreateSlimBuilder(WebApplicationOptions? settings)
{ {
ArgumentNullException.ThrowIfNull(settings, nameof(settings)); ArgumentNullException.ThrowIfNull(settings, nameof(settings));
WebApplicationBuilder innerApp = WebApplication.CreateSlimBuilder(settings); WebApplicationBuilder innerApp = WebApplication.CreateSlimBuilder(settings);
TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp, settings); TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp);
builder.AddTelegratorWeb();
builder.Services.AddTelegramBotHostDefaults();
builder.Services.AddTelegramWebhook();
return builder; return builder;
} }
@@ -93,11 +91,13 @@ public class TelegramBotWebHost : IHost, IApplicationBuilder, IEndpointRouteBuil
/// Creates new EMPTY <see cref="TelegramBotHostBuilder"/> WITHOUT any services or update receiving schemes /// Creates new EMPTY <see cref="TelegramBotHostBuilder"/> WITHOUT any services or update receiving schemes
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public static TelegramBotWebHostBuilder CreateEmptyBuilder(WebApplicationOptions settings) public static TelegramBotWebHostBuilder CreateEmptyBuilder(WebApplicationOptions? settings)
{ {
ArgumentNullException.ThrowIfNull(settings, nameof(settings)); ArgumentNullException.ThrowIfNull(settings, nameof(settings));
WebApplicationBuilder innerApp = WebApplication.CreateEmptyBuilder(settings); WebApplicationBuilder innerApp = WebApplication.CreateEmptyBuilder(settings);
return new TelegramBotWebHostBuilder(innerApp, settings); TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp);
builder.AddTelegratorWeb();
return builder;
} }
/// <inheritdoc/> /// <inheritdoc/>
@@ -9,13 +9,10 @@ using Telegrator.Core;
#pragma warning disable IDE0001 #pragma warning disable IDE0001
namespace Telegrator.Hosting.Web; namespace Telegrator.Hosting.Web;
/// <summary> /// <inheritdoc/>
/// Represents a web hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more. public class TelegramBotWebHostBuilder : ITelegramBotHostBuilder
/// </summary>
public class TelegramBotWebHostBuilder : IHostApplicationBuilder, ICollectingProvider
{ {
private readonly WebApplicationBuilder _innerBuilder; private readonly WebApplicationBuilder _innerBuilder;
private readonly WebApplicationOptions _settings;
internal IHandlersCollection _handlers = null!; internal IHandlersCollection _handlers = null!;
/// <inheritdoc/> /// <inheritdoc/>
@@ -43,27 +40,9 @@ public class TelegramBotWebHostBuilder : IHostApplicationBuilder, ICollectingPro
/// Initializes a new instance of the <see cref="TelegramBotWebHostBuilder"/> class. /// Initializes a new instance of the <see cref="TelegramBotWebHostBuilder"/> class.
/// </summary> /// </summary>
/// <param name="webApplicationBuilder"></param> /// <param name="webApplicationBuilder"></param>
/// <param name="settings"></param> public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder)
public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder, WebApplicationOptions? settings = null)
{ {
_innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder)); _innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder));
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
this.AddTelegratorWeb();
}
/// <summary>
/// Initializes a new instance of the <see cref="TelegramBotWebHostBuilder"/> class.
/// </summary>
/// <param name="webApplicationBuilder"></param>
/// <param name="options"></param>
/// <param name="settings"></param>
public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder, TelegratorOptions? options, WebApplicationOptions? settings)
{
_innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder));
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
this.AddTelegratorWeb(options, null);
} }
/// <summary> /// <summary>
@@ -71,28 +50,10 @@ public class TelegramBotWebHostBuilder : IHostApplicationBuilder, ICollectingPro
/// </summary> /// </summary>
/// <param name="webApplicationBuilder"></param> /// <param name="webApplicationBuilder"></param>
/// <param name="handlers"></param> /// <param name="handlers"></param>
/// <param name="settings"></param> public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder, IHandlersCollection handlers)
public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder, IHandlersCollection handlers, WebApplicationOptions settings)
{ {
_innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder)); _innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder));
_settings = settings ?? throw new ArgumentNullException(nameof(settings)); _handlers = handlers ?? throw new ArgumentNullException(nameof(handlers));
this.AddTelegratorWeb(null, handlers);
}
/// <summary>
/// Initializes a new instance of the <see cref="TelegramBotWebHostBuilder"/> class.
/// </summary>
/// <param name="webApplicationBuilder"></param>
/// <param name="handlers"></param>
/// <param name="options"></param>
/// <param name="settings"></param>
public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder, IHandlersCollection handlers, TelegratorOptions? options, WebApplicationOptions settings)
{
_innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder));
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
this.AddTelegratorWeb(options, handlers);
} }
/// <summary> /// <summary>
@@ -102,7 +63,7 @@ public class TelegramBotWebHostBuilder : IHostApplicationBuilder, ICollectingPro
public TelegramBotWebHost Build() public TelegramBotWebHost Build()
{ {
TelegramBotWebHost host = new TelegramBotWebHost(_innerBuilder); TelegramBotWebHost host = new TelegramBotWebHost(_innerBuilder);
host.UseTelegrator(); host.UseTelegratorWeb();
return host; return host;
} }
@@ -19,7 +19,6 @@ public class HostedUpdateWebhooker : IHostedService
{ {
private const string SecretTokenHeader = "X-Telegram-Bot-Api-Secret-Token"; private const string SecretTokenHeader = "X-Telegram-Bot-Api-Secret-Token";
private readonly IEndpointRouteBuilder _botHost;
private readonly ITelegramBotClient _botClient; private readonly ITelegramBotClient _botClient;
private readonly IUpdateRouter _updateRouter; private readonly IUpdateRouter _updateRouter;
private readonly WebhookerOptions _options; private readonly WebhookerOptions _options;
@@ -27,17 +26,15 @@ public class HostedUpdateWebhooker : IHostedService
/// <summary> /// <summary>
/// Initiallizes new instance of <see cref="HostedUpdateWebhooker"/> /// Initiallizes new instance of <see cref="HostedUpdateWebhooker"/>
/// </summary> /// </summary>
/// <param name="botHost"></param>
/// <param name="botClient"></param> /// <param name="botClient"></param>
/// <param name="updateRouter"></param> /// <param name="updateRouter"></param>
/// <param name="options"></param> /// <param name="options"></param>
/// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentNullException"></exception>
public HostedUpdateWebhooker(IEndpointRouteBuilder botHost, ITelegramBotClient botClient, IUpdateRouter updateRouter, IOptions<WebhookerOptions> options) public HostedUpdateWebhooker(ITelegramBotClient botClient, IUpdateRouter updateRouter, IOptions<WebhookerOptions> options)
{ {
if (string.IsNullOrEmpty(options.Value.WebhookUri)) if (string.IsNullOrEmpty(options.Value.WebhookUri))
throw new ArgumentNullException(nameof(options), "Option \"WebhookUrl\" must be set to subscribe for update recieving"); throw new ArgumentNullException(nameof(options), "Option \"WebhookUrl\" must be set to subscribe for update recieving");
_botHost = botHost;
_botClient = botClient; _botClient = botClient;
_updateRouter = updateRouter; _updateRouter = updateRouter;
_options = options.Value; _options = options.Value;
@@ -50,11 +47,48 @@ public class HostedUpdateWebhooker : IHostedService
return Task.CompletedTask; return Task.CompletedTask;
} }
private async void StartInternal(CancellationToken cancellationToken) /// <inheritdoc/>
public Task StopAsync(CancellationToken cancellationToken)
{
_botClient.DeleteWebhook(_options.DropPendingUpdates, cancellationToken);
return Task.CompletedTask;
}
/// <summary>
/// Allows to remap receiving webhook endpoint and map new route to webhost.
/// </summary>
/// <param name="routeBuilder"></param>
/// <param name="webhookUri"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public async Task RemapWebhook(IEndpointRouteBuilder routeBuilder, string webhookUri, CancellationToken cancellationToken = default)
{
if (!Uri.TryCreate(webhookUri, UriKind.Absolute, out Uri? result))
throw new ArgumentException("invalid URL");
_options.WebhookUri = result.ToString();
await SetWebhook(cancellationToken);
MapWebhook(routeBuilder);
}
/// <summary>
/// Maps bot webhook to application builder
/// </summary>
/// <param name="routeBuilder"></param>
internal void MapWebhook(IEndpointRouteBuilder routeBuilder)
{ {
string pattern = new UriBuilder(_options.WebhookUri).Path; string pattern = new UriBuilder(_options.WebhookUri).Path;
_botHost.MapPost(pattern, (Delegate)ReceiveUpdate); routeBuilder.MapPost(pattern, (Delegate)ReceiveUpdate);
}
private async void StartInternal(CancellationToken cancellationToken)
{
await SetWebhook(cancellationToken);
}
private async Task SetWebhook(CancellationToken cancellationToken)
{
await _botClient.SetWebhook( await _botClient.SetWebhook(
url: _options.WebhookUri, url: _options.WebhookUri,
maxConnections: _options.MaxConnections, maxConnections: _options.MaxConnections,
@@ -64,13 +98,6 @@ public class HostedUpdateWebhooker : IHostedService
cancellationToken: cancellationToken); cancellationToken: cancellationToken);
} }
/// <inheritdoc/>
public Task StopAsync(CancellationToken cancellationToken)
{
_botClient.DeleteWebhook(_options.DropPendingUpdates, cancellationToken);
return Task.CompletedTask;
}
private async Task<IResult> ReceiveUpdate(HttpContext ctx) private async Task<IResult> ReceiveUpdate(HttpContext ctx)
{ {
if (_options.SecretToken != null) if (_options.SecretToken != null)
@@ -15,7 +15,7 @@
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild> <EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<Title>Telegrator.Hosting.Web</Title> <Title>Telegrator.Hosting.Web</Title>
<Version>1.16.5</Version> <Version>1.16.8</Version>
<Authors>Rikitav Tim4ik</Authors> <Authors>Rikitav Tim4ik</Authors>
<Company>Rikitav Tim4ik</Company> <Company>Rikitav Tim4ik</Company>
<RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl> <RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl>
+64 -12
View File
@@ -1,10 +1,12 @@
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using System.Diagnostics.CodeAnalysis;
using Telegram.Bot; using Telegram.Bot;
using Telegrator.Core; using Telegrator.Core;
using Telegrator.Hosting; using Telegrator.Hosting;
@@ -42,11 +44,31 @@ namespace Telegrator
/// <summary> /// <summary>
/// Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers. /// Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers.
/// </summary> /// </summary>
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<ITelegramBotHostBuilder>? action = null)
{ {
IServiceCollection services = builder.Services; AddTelegratorWebInternal(builder.Services, builder.Configuration, builder.Properties, ref handlers, options);
IConfigurationManager configuration = builder.Configuration; if (builder is TelegramBotWebHostBuilder telegramBotHostBuilder)
telegramBotHostBuilder._handlers = handlers;
action?.Invoke(builder);
return builder;
}
/// <summary>
/// Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers.
/// </summary>
public static IHostApplicationBuilder AddTelegratorWeb(this WebApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null, Action<ITelegramBotHostBuilder>? action = null)
{
AddTelegratorWebInternal(builder.Services, builder.Configuration, ((IHostApplicationBuilder)builder).Properties, ref handlers, options);
action?.Invoke(new TelegramBotWebHostBuilder(builder));
return builder;
}
/// <summary>
/// Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers.
/// </summary>
internal static void AddTelegratorWebInternal(IServiceCollection services, IConfiguration configuration, IDictionary<object, object> properties, [NotNull] ref IHandlersCollection? handlers, TelegratorOptions? options = null)
{
if (options == null) if (options == null)
{ {
options = configuration.GetSection(nameof(TelegratorOptions)).Get<TelegratorOptions>(); options = configuration.GetSection(nameof(TelegratorOptions)).Get<TelegratorOptions>();
@@ -71,10 +93,7 @@ namespace Telegrator
handlers ??= new HostHandlersCollection(services, options); handlers ??= new HostHandlersCollection(services, options);
services.AddSingleton(handlers); services.AddSingleton(handlers);
properties.Add(HandlersCollectionPropertyKey, handlers);
builder.Properties.Add(HandlersCollectionPropertyKey, handlers);
if (builder is TelegramBotWebHostBuilder botHostBuilder)
botHostBuilder._handlers = handlers;
if (!services.Any(srvc => srvc.ImplementationType == typeof(IOptions<WebhookerOptions>))) if (!services.Any(srvc => srvc.ImplementationType == typeof(IOptions<WebhookerOptions>)))
{ {
@@ -96,18 +115,32 @@ namespace Telegrator
services.AddTelegramBotHostDefaults(); services.AddTelegramBotHostDefaults();
services.AddTelegramWebhook(); services.AddTelegramWebhook();
return builder; }
/// <summary>
/// Searchs for <see cref="HostedUpdateWebhooker"/> hosted service inside hosts services
/// </summary>
/// <param name="services"></param>
/// <param name="webhooker"></param>
/// <returns></returns>
public static bool TryFindWebhooker(this IServiceProvider services, [NotNullWhen(true)] out HostedUpdateWebhooker? webhooker)
{
webhooker = services.GetServices<IHostedService>().FirstOrDefault(s => s is HostedUpdateWebhooker) as HostedUpdateWebhooker;
return webhooker != null;
} }
/// <summary> /// <summary>
/// Replaces the initialization logic from TelegramBotWebHost constructor. /// Replaces the initialization logic from TelegramBotWebHost constructor.
/// Initializes the bot and logs handlers on application startup. /// Initializes the bot and logs handlers on application startup.
/// </summary> /// </summary>
public static WebApplication UseTelegratorWeb(this WebApplication app) public static T UseTelegratorWeb<T>(this T app, bool dontMap = false) where T : IEndpointRouteBuilder, IHost
{ {
ITelegramBotInfo info = app.Services.GetRequiredService<ITelegramBotInfo>(); if (!app.ServiceProvider.TryFindWebhooker(out HostedUpdateWebhooker? webhooker))
IHandlersCollection handlers = app.Services.GetRequiredService<IHandlersCollection>(); throw new InvalidOperationException("No service for type 'Telegrator.Mediation.HostedUpdateWebhooker' has been registered.");
ILoggerFactory loggerFactory = app.Services.GetRequiredService<ILoggerFactory>();
ITelegramBotInfo info = app.ServiceProvider.GetRequiredService<ITelegramBotInfo>();
IHandlersCollection handlers = app.ServiceProvider.GetRequiredService<IHandlersCollection>();
ILoggerFactory loggerFactory = app.ServiceProvider.GetRequiredService<ILoggerFactory>();
ILogger logger = loggerFactory.CreateLogger("Telegrator.Hosting.Web.TelegratorHost"); ILogger logger = loggerFactory.CreateLogger("Telegrator.Hosting.Web.TelegratorHost");
if (logger.IsEnabled(LogLevel.Information)) if (logger.IsEnabled(LogLevel.Information))
@@ -117,6 +150,25 @@ namespace Telegrator
logger.LogHandlers(handlers); logger.LogHandlers(handlers);
} }
if (!dontMap)
webhooker.MapWebhook(app);
return app;
}
/// <summary>
/// Allows to remap receiving webhook endpoint and map new route to webhost.
/// </summary>
/// <param name="app"></param>
/// <param name="webhookUri"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static T RemapWebhook<T>(this T app, string webhookUri) where T : IEndpointRouteBuilder, IHost
{
if (!app.ServiceProvider.TryFindWebhooker(out HostedUpdateWebhooker? webhooker))
throw new InvalidOperationException("No service for type 'Telegrator.Mediation.HostedUpdateWebhooker' has been registered.");
webhooker.RemapWebhook(app, webhookUri, default).GetAwaiter().GetResult();
return app; return app;
} }
@@ -0,0 +1,12 @@
using Microsoft.Extensions.Hosting;
using Telegrator.Core;
namespace Telegrator.Hosting;
/// <summary>
/// Represents a hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more.
/// </summary>
public interface ITelegramBotHostBuilder : IHostApplicationBuilder, ICollectingProvider
{
}
@@ -51,7 +51,7 @@ public class TelegramBotHost : IHost, ITelegratorBot
public static TelegramBotHostBuilder CreateBuilder() public static TelegramBotHostBuilder CreateBuilder()
{ {
HostApplicationBuilder innerBuilder = new HostApplicationBuilder(settings: null); HostApplicationBuilder innerBuilder = new HostApplicationBuilder(settings: null);
TelegramBotHostBuilder builder = new TelegramBotHostBuilder(innerBuilder, null); TelegramBotHostBuilder builder = new TelegramBotHostBuilder(innerBuilder);
return builder; return builder;
} }
@@ -62,7 +62,7 @@ public class TelegramBotHost : IHost, ITelegratorBot
public static TelegramBotHostBuilder CreateBuilder(HostApplicationBuilderSettings? settings) public static TelegramBotHostBuilder CreateBuilder(HostApplicationBuilderSettings? settings)
{ {
HostApplicationBuilder innerBuilder = new HostApplicationBuilder(settings); HostApplicationBuilder innerBuilder = new HostApplicationBuilder(settings);
TelegramBotHostBuilder builder = new TelegramBotHostBuilder(innerBuilder, settings); TelegramBotHostBuilder builder = new TelegramBotHostBuilder(innerBuilder);
return builder; return builder;
} }
@@ -73,7 +73,7 @@ public class TelegramBotHost : IHost, ITelegratorBot
public static TelegramBotHostBuilder CreateEmptyBuilder() public static TelegramBotHostBuilder CreateEmptyBuilder()
{ {
HostApplicationBuilder innerBuilder = Host.CreateEmptyApplicationBuilder(null); HostApplicationBuilder innerBuilder = Host.CreateEmptyApplicationBuilder(null);
return new TelegramBotHostBuilder(innerBuilder, null); return new TelegramBotHostBuilder(innerBuilder);
} }
/// <summary> /// <summary>
@@ -82,8 +82,8 @@ public class TelegramBotHost : IHost, ITelegratorBot
/// <returns></returns> /// <returns></returns>
public static TelegramBotHostBuilder CreateEmptyBuilder(HostApplicationBuilderSettings? settings) public static TelegramBotHostBuilder CreateEmptyBuilder(HostApplicationBuilderSettings? settings)
{ {
HostApplicationBuilder innerBuilder = Host.CreateEmptyApplicationBuilder(null); HostApplicationBuilder innerBuilder = Host.CreateEmptyApplicationBuilder(settings);
return new TelegramBotHostBuilder(innerBuilder, settings); return new TelegramBotHostBuilder(innerBuilder);
} }
/// <inheritdoc/> /// <inheritdoc/>
@@ -8,13 +8,10 @@ using Telegrator.Core;
#pragma warning disable IDE0001 #pragma warning disable IDE0001
namespace Telegrator.Hosting; namespace Telegrator.Hosting;
/// <summary> /// <inheritdoc/>
/// Represents a hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more. public class TelegramBotHostBuilder : ITelegramBotHostBuilder
/// </summary>
public class TelegramBotHostBuilder : IHostApplicationBuilder, ICollectingProvider
{ {
private readonly HostApplicationBuilder _innerBuilder; private readonly HostApplicationBuilder _innerBuilder;
private readonly HostApplicationBuilderSettings _settings;
internal IHandlersCollection _handlers = null!; internal IHandlersCollection _handlers = null!;
/// <inheritdoc/> /// <inheritdoc/>
@@ -42,27 +39,9 @@ public class TelegramBotHostBuilder : IHostApplicationBuilder, ICollectingProvid
/// Initializes a new instance of the <see cref="TelegramBotHostBuilder"/> class. /// Initializes a new instance of the <see cref="TelegramBotHostBuilder"/> class.
/// </summary> /// </summary>
/// <param name="hostApplicationBuilder"></param> /// <param name="hostApplicationBuilder"></param>
/// <param name="settings"></param> public TelegramBotHostBuilder(HostApplicationBuilder hostApplicationBuilder)
public TelegramBotHostBuilder(HostApplicationBuilder hostApplicationBuilder, HostApplicationBuilderSettings? settings = null)
{ {
_innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder)); _innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder));
_settings = settings ?? new HostApplicationBuilderSettings();
this.AddTelegrator();
}
/// <summary>
/// Initializes a new instance of the <see cref="TelegramBotHostBuilder"/> class.
/// </summary>
/// <param name="hostApplicationBuilder"></param>
/// <param name="options"></param>
/// <param name="settings"></param>
public TelegramBotHostBuilder(HostApplicationBuilder hostApplicationBuilder, TelegratorOptions? options, HostApplicationBuilderSettings? settings)
{
_innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder));
_settings = settings ?? new HostApplicationBuilderSettings();
this.AddTelegrator(options, null);
} }
/// <summary> /// <summary>
@@ -70,28 +49,10 @@ public class TelegramBotHostBuilder : IHostApplicationBuilder, ICollectingProvid
/// </summary> /// </summary>
/// <param name="hostApplicationBuilder"></param> /// <param name="hostApplicationBuilder"></param>
/// <param name="handlers"></param> /// <param name="handlers"></param>
/// <param name="settings"></param> public TelegramBotHostBuilder(HostApplicationBuilder hostApplicationBuilder, IHandlersCollection handlers)
public TelegramBotHostBuilder(HostApplicationBuilder hostApplicationBuilder, IHandlersCollection handlers, HostApplicationBuilderSettings? settings)
{ {
_innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder)); _innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder));
_settings = settings ?? new HostApplicationBuilderSettings(); _handlers = handlers ?? throw new ArgumentNullException(nameof(handlers));
this.AddTelegrator(null, handlers);
}
/// <summary>
/// Initializes a new instance of the <see cref="TelegramBotHostBuilder"/> class.
/// </summary>
/// <param name="hostApplicationBuilder"></param>
/// <param name="handlers"></param>
/// <param name="options"></param>
/// <param name="settings"></param>
public TelegramBotHostBuilder(HostApplicationBuilder hostApplicationBuilder, IHandlersCollection handlers, TelegratorOptions? options, HostApplicationBuilderSettings? settings)
{
_innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder));
_settings = settings ?? new HostApplicationBuilderSettings();
this.AddTelegrator(options, handlers);
} }
/// <summary> /// <summary>
@@ -108,6 +69,6 @@ public class TelegramBotHostBuilder : IHostApplicationBuilder, ICollectingProvid
/// <inheritdoc/> /// <inheritdoc/>
public void ConfigureContainer<TContainerBuilder>(IServiceProviderFactory<TContainerBuilder> factory, Action<TContainerBuilder>? configure = null) where TContainerBuilder : notnull public void ConfigureContainer<TContainerBuilder>(IServiceProviderFactory<TContainerBuilder> factory, Action<TContainerBuilder>? configure = null) where TContainerBuilder : notnull
{ {
this.ConfigureContainer(factory, configure); _innerBuilder.ConfigureContainer(factory, configure);
} }
} }
@@ -15,7 +15,7 @@
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild> <EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<Title>Telegrator.Hosting</Title> <Title>Telegrator.Hosting</Title>
<Version>1.16.5</Version> <Version>1.16.8</Version>
<Authors>Rikitav Tim4ik</Authors> <Authors>Rikitav Tim4ik</Authors>
<Company>Rikitav Tim4ik</Company> <Company>Rikitav Tim4ik</Company>
<RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl> <RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl>
@@ -31,8 +31,8 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.3" /> <PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.5" />
<PackageReference Include="Microsoft.Extensions.Http" Version="10.0.3" /> <PackageReference Include="Microsoft.Extensions.Http" Version="10.0.5" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
+46 -15
View File
@@ -4,6 +4,7 @@ using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using System.Diagnostics.CodeAnalysis;
using System.Text; using System.Text;
using Telegram.Bot; using Telegram.Bot;
using Telegram.Bot.Polling; using Telegram.Bot.Polling;
@@ -30,7 +31,7 @@ public static class HostBuilderExtensions
/// </summary> /// </summary>
public const string HandlersCollectionPropertyKey = nameof(IHandlersCollection); public const string HandlersCollectionPropertyKey = nameof(IHandlersCollection);
extension (IHostApplicationBuilder builder) extension (HostApplicationBuilder builder)
{ {
/// <summary> /// <summary>
/// Gets the <see cref="IHandlersCollection"/> from the builder properties. /// Gets the <see cref="IHandlersCollection"/> from the builder properties.
@@ -39,22 +40,57 @@ public static class HostBuilderExtensions
{ {
get get
{ {
if (builder is TelegramBotHostBuilder botHostBuilder) return (IHandlersCollection)((IHostApplicationBuilder)builder).Properties[HandlersCollectionPropertyKey];
return botHostBuilder.Handlers;
return (IHandlersCollection)builder.Properties[HandlersCollectionPropertyKey];
} }
} }
} }
/// <summary> /// <summary>
/// Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers. /// Replaces TelegramBotHostBuilder. Configures DI, options, and handlers.
/// </summary>
public static ITelegramBotHostBuilder AddTelegrator(this ITelegramBotHostBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null, Action<ITelegramBotHostBuilder>? action = null)
{
AddTelegratorInternal(builder.Services, builder.Configuration, builder.Properties, ref handlers, options);
if (builder is TelegramBotHostBuilder telegramBotHostBuilder)
telegramBotHostBuilder._handlers = handlers;
action?.Invoke(builder);
return builder;
}
/// <summary>
/// Replaces TelegramBotHostBuilder. Configures DI, options, and handlers.
/// </summary>
public static IHostApplicationBuilder AddTelegrator(this HostApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null, Action<ITelegramBotHostBuilder>? action = null)
{
AddTelegratorInternal(builder.Services, builder.Configuration, ((IHostApplicationBuilder)builder).Properties, ref handlers, options);
action?.Invoke(new TelegramBotHostBuilder(builder, handlers));
return builder;
}
/// <summary>
/// Replaces TelegramBotHostBuilder. Configures DI, options, and handlers.
/// </summary> /// </summary>
public static IHostApplicationBuilder AddTelegrator(this IHostApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null) public static IHostApplicationBuilder AddTelegrator(this IHostApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null)
{ {
IServiceCollection services = builder.Services; AddTelegratorInternal(builder.Services, builder.Configuration, builder.Properties, ref handlers, options);
IConfigurationManager configuration = builder.Configuration; return builder;
}
/// <summary>
/// Replaces TelegramBotHostBuilder. Configures DI, options, and handlers.
/// </summary>
public static IHostBuilder AddTelegrator(this IHostBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null)
{
builder.ConfigureServices((ctx, sp) => AddTelegratorInternal(sp, ctx.Configuration, builder.Properties, ref handlers, options));
return builder;
}
/// <summary>
/// Replaces TelegramBotHostBuilder. Configures DI, options, and handlers.
/// </summary>
internal static void AddTelegratorInternal(IServiceCollection services, IConfiguration configuration, IDictionary<object, object> properties, [NotNull] ref IHandlersCollection? handlers, TelegratorOptions? options = null)
{
if (options == null) if (options == null)
{ {
options = configuration.GetSection(nameof(TelegratorOptions)).Get<TelegratorOptions>(); options = configuration.GetSection(nameof(TelegratorOptions)).Get<TelegratorOptions>();
@@ -79,10 +115,7 @@ public static class HostBuilderExtensions
handlers ??= new HostHandlersCollection(services, options); handlers ??= new HostHandlersCollection(services, options);
services.AddSingleton(handlers); services.AddSingleton(handlers);
properties.Add(HandlersCollectionPropertyKey, handlers);
builder.Properties.Add(HandlersCollectionPropertyKey, handlers);
if (builder is TelegramBotHostBuilder botHostBuilder)
botHostBuilder._handlers = handlers;
if (!services.Any(srvc => srvc.ImplementationType == typeof(IOptions<ReceiverOptions>))) if (!services.Any(srvc => srvc.ImplementationType == typeof(IOptions<ReceiverOptions>)))
{ {
@@ -102,9 +135,8 @@ public static class HostBuilderExtensions
})); }));
} }
services.AddTelegramReceiver();
services.AddTelegramBotHostDefaults(); services.AddTelegramBotHostDefaults();
return builder; services.AddTelegramReceiver();
} }
} }
@@ -133,7 +165,6 @@ public static class ServicesCollectionExtensions
/// <returns></returns> /// <returns></returns>
public static IServiceCollection AddTelegramBotHostDefaults(this IServiceCollection services) public static IServiceCollection AddTelegramBotHostDefaults(this IServiceCollection services)
{ {
services.AddLogging(builder => builder.AddConsole().AddDebug());
services.AddSingleton<IAwaitingProvider, HostAwaitingProvider>(); services.AddSingleton<IAwaitingProvider, HostAwaitingProvider>();
services.AddSingleton<IHandlersProvider, HostHandlersProvider>(); services.AddSingleton<IHandlersProvider, HostHandlersProvider>();
services.AddSingleton<IUpdateRouter, HostUpdateRouter>(); services.AddSingleton<IUpdateRouter, HostUpdateRouter>();
+3 -3
View File
@@ -14,7 +14,7 @@
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild> <EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<Title>Telegrator : Telegram.Bot mediator framework</Title> <Title>Telegrator : Telegram.Bot mediator framework</Title>
<Version>1.16.5</Version> <Version>1.16.8</Version>
<Authors>Rikitav Tim4ik</Authors> <Authors>Rikitav Tim4ik</Authors>
<Company>Rikitav Tim4ik</Company> <Company>Rikitav Tim4ik</Company>
<RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl> <RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl>
@@ -32,8 +32,8 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="10.0.3" /> <PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="10.0.5" />
<PackageReference Include="System.Threading.Channels" Version="10.0.3" /> <PackageReference Include="System.Threading.Channels" Version="10.0.5" />
<PackageReference Include="Telegram.Bot" Version="22.9.5.3" /> <PackageReference Include="Telegram.Bot" Version="22.9.5.3" />
</ItemGroup> </ItemGroup>