From 39f9abd9dec84dab73cf749c7d2d9bac9e9721d7 Mon Sep 17 00:00:00 2001 From: gutii Date: Fri, 1 May 2026 20:48:32 +0400 Subject: [PATCH] * Added WideBotOptions * Added ConfigureWideBot method * Refactored `AddWideTelegrator` method * Added `dbConnectionFactory` --- docs/Telegrator.Hosting.Web.xml | 62 +++++++++---------- docs/Telegrator.Hosting.WideBot.xml | 16 +++-- docs/Telegrator.Localized.xml | 8 +++ .../Telegartor.RedisStateStorage.csproj | 3 + .../Mediation/HostedUpdateWebhooker.cs | 1 - .../Telegrator.Hosting.Web.csproj | 5 +- src/Telegrator.Hosting.Web/TypesExtensions.cs | 1 - .../{Hosting.Web => }/WebhookerOptions.cs | 2 +- .../Mediation/HostedWideBotUpdateReceiver.cs | 2 +- .../Telegrator.Hosting.WideBot.csproj | 5 +- .../TypesExtensions.cs | 56 +++++++++++++---- .../WideBotOptions.cs | 16 +++++ .../Telegrator.Hosting.csproj | 5 +- src/Telegrator.Localized/README.md | 0 .../Telegrator.Localized.csproj | 30 +++++++++ src/Telegrator/Telegrator.csproj | 5 +- tests/Telegrator.Tests/Program.cs | 30 ++++++--- 17 files changed, 184 insertions(+), 63 deletions(-) create mode 100644 docs/Telegrator.Localized.xml rename src/Telegrator.Hosting.Web/{Hosting.Web => }/WebhookerOptions.cs (97%) create mode 100644 src/Telegrator.Hosting.WideBot/WideBotOptions.cs create mode 100644 src/Telegrator.Localized/README.md diff --git a/docs/Telegrator.Hosting.Web.xml b/docs/Telegrator.Hosting.Web.xml index b1cdf24..a564573 100644 --- a/docs/Telegrator.Hosting.Web.xml +++ b/docs/Telegrator.Hosting.Web.xml @@ -4,41 +4,12 @@ Telegrator.Hosting.Web - - - Configuration options for Telegram bot behavior and execution settings. - Controls various aspects of bot operation including concurrency, routing, webhook receiving, and execution policies. - - - - - Gets or sets HTTPS URL to send updates to. Use an empty string to remove webhook integration - - - - - 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. - - - - - 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. - - - - - Pass true to drop all pending updates - - Service for receiving updates for Hosted telegram bots via Webhooks and queuing them to router - + Initiallizes new instance of @@ -95,7 +66,7 @@ Provides method to configure Telegram Bot WebHost - + Adds WebhookerOptions to services @@ -138,5 +109,34 @@ + + + Configuration options for Telegram bot behavior and execution settings. + Controls various aspects of bot operation including concurrency, routing, webhook receiving, and execution policies. + + + + + Gets or sets HTTPS URL to send updates to. Use an empty string to remove webhook integration + + + + + 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. + + + + + 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. + + + + + Pass true to drop all pending updates + + diff --git a/docs/Telegrator.Hosting.WideBot.xml b/docs/Telegrator.Hosting.WideBot.xml index fa5e33f..7af72c1 100644 --- a/docs/Telegrator.Hosting.WideBot.xml +++ b/docs/Telegrator.Hosting.WideBot.xml @@ -13,7 +13,7 @@ - + Service for receiving updates for Hosted wide telegram bots and queuing them to router @@ -119,17 +119,17 @@ Provides extension methods for to configure Telegrator. - + Replaces TelegramBotHostBuilder. Configures DI, options, and handlers. - + Replaces TelegramBotHostBuilder. Configures DI, options, and handlers. - + Replaces TelegramBotHostBuilder. Configures DI, options, and handlers. @@ -148,6 +148,14 @@ + + + Adds WTelegramBotClientOptions to services + + + + + Adds WTelegramBotClient diff --git a/docs/Telegrator.Localized.xml b/docs/Telegrator.Localized.xml new file mode 100644 index 0000000..dda069d --- /dev/null +++ b/docs/Telegrator.Localized.xml @@ -0,0 +1,8 @@ + + + + Telegrator.Localized + + + + diff --git a/src/Telegartor.RedisStateStorage/Telegartor.RedisStateStorage.csproj b/src/Telegartor.RedisStateStorage/Telegartor.RedisStateStorage.csproj index bff2542..72bf7f6 100644 --- a/src/Telegartor.RedisStateStorage/Telegartor.RedisStateStorage.csproj +++ b/src/Telegartor.RedisStateStorage/Telegartor.RedisStateStorage.csproj @@ -13,6 +13,9 @@ True True True + True + snupkg + False Telegrator.RedisStateStorage 1.16.6 diff --git a/src/Telegrator.Hosting.Web/Mediation/HostedUpdateWebhooker.cs b/src/Telegrator.Hosting.Web/Mediation/HostedUpdateWebhooker.cs index 1f0f63d..c9beb5a 100644 --- a/src/Telegrator.Hosting.Web/Mediation/HostedUpdateWebhooker.cs +++ b/src/Telegrator.Hosting.Web/Mediation/HostedUpdateWebhooker.cs @@ -8,7 +8,6 @@ using System.Text.Json; using Telegram.Bot; using Telegram.Bot.Types; using Telegrator.Core; -using Telegrator.Hosting.Web; namespace Telegrator.Mediation; diff --git a/src/Telegrator.Hosting.Web/Telegrator.Hosting.Web.csproj b/src/Telegrator.Hosting.Web/Telegrator.Hosting.Web.csproj index cfdf037..40f793c 100644 --- a/src/Telegrator.Hosting.Web/Telegrator.Hosting.Web.csproj +++ b/src/Telegrator.Hosting.Web/Telegrator.Hosting.Web.csproj @@ -13,9 +13,12 @@ True True True + True + snupkg + False Telegrator.Hosting.Web - 1.17.0 + 1.17.1 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 963a09d..a8e40ec 100644 --- a/src/Telegrator.Hosting.Web/TypesExtensions.cs +++ b/src/Telegrator.Hosting.Web/TypesExtensions.cs @@ -9,7 +9,6 @@ using System.Diagnostics.CodeAnalysis; using Telegram.Bot; using Telegrator.Core; using Telegrator.Hosting; -using Telegrator.Hosting.Web; using Telegrator.Mediation; using Telegrator.Providers; diff --git a/src/Telegrator.Hosting.Web/Hosting.Web/WebhookerOptions.cs b/src/Telegrator.Hosting.Web/WebhookerOptions.cs similarity index 97% rename from src/Telegrator.Hosting.Web/Hosting.Web/WebhookerOptions.cs rename to src/Telegrator.Hosting.Web/WebhookerOptions.cs index c320575..1cf9c40 100644 --- a/src/Telegrator.Hosting.Web/Hosting.Web/WebhookerOptions.cs +++ b/src/Telegrator.Hosting.Web/WebhookerOptions.cs @@ -1,6 +1,6 @@ using System.Diagnostics.CodeAnalysis; -namespace Telegrator.Hosting.Web; +namespace Telegrator; /// /// Configuration options for Telegram bot behavior and execution settings. diff --git a/src/Telegrator.Hosting.WideBot/Mediation/HostedWideBotUpdateReceiver.cs b/src/Telegrator.Hosting.WideBot/Mediation/HostedWideBotUpdateReceiver.cs index c885dd6..041f32f 100644 --- a/src/Telegrator.Hosting.WideBot/Mediation/HostedWideBotUpdateReceiver.cs +++ b/src/Telegrator.Hosting.WideBot/Mediation/HostedWideBotUpdateReceiver.cs @@ -14,7 +14,7 @@ namespace Telegrator.Mediation; /// /// /// -public class HostedWideBotUpdateReceiver(ILogger logger, ITelegramBotClient botClient, IUpdateRouter updateRouter, IOptions? options) : BackgroundService +public class HostedWideBotUpdateReceiver(ILogger logger, ITelegramBotClient botClient, IUpdateRouter updateRouter, IOptions? options) : BackgroundService { /// protected override async Task ExecuteAsync(CancellationToken stoppingToken) diff --git a/src/Telegrator.Hosting.WideBot/Telegrator.Hosting.WideBot.csproj b/src/Telegrator.Hosting.WideBot/Telegrator.Hosting.WideBot.csproj index 2e2f70d..52e6287 100644 --- a/src/Telegrator.Hosting.WideBot/Telegrator.Hosting.WideBot.csproj +++ b/src/Telegrator.Hosting.WideBot/Telegrator.Hosting.WideBot.csproj @@ -13,9 +13,12 @@ True True True + True + snupkg + False Telegrator.Hosting.WideBot - 1.17.0 + 1.17.1 Rikitav Tim4ik Rikitav Tim4ik https://github.com/Rikitav/Telegrator diff --git a/src/Telegrator.Hosting.WideBot/TypesExtensions.cs b/src/Telegrator.Hosting.WideBot/TypesExtensions.cs index da4b685..d2a86aa 100644 --- a/src/Telegrator.Hosting.WideBot/TypesExtensions.cs +++ b/src/Telegrator.Hosting.WideBot/TypesExtensions.cs @@ -15,6 +15,7 @@ using Telegrator.Providers; using WUpdate = WTelegram.Types.Update; using TLUpdate = TL.Update; +using System.Data.Common; namespace Telegrator; @@ -92,9 +93,9 @@ public static class WideHostBuilderExtensions /// /// Replaces TelegramBotHostBuilder. Configures DI, options, and handlers. /// - public static IHostApplicationBuilder AddWideTelegrator(this IHostApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null, Action? action = null) + public static IHostApplicationBuilder AddWideTelegrator(this IHostApplicationBuilder builder, Func dbConnectionFactory, Action? action = null, TelegratorOptions? options = null, IHandlersCollection? handlers = null) { - AddWideTelegratorInternal(builder.Services, builder.Configuration, builder.Properties, ref handlers, options); + AddWideTelegratorInternal(builder.Services, builder.Configuration, builder.Properties, dbConnectionFactory, ref handlers, options); action?.Invoke(new TelegramBotHostBuilder(builder, handlers)); return builder; } @@ -102,16 +103,16 @@ public static class WideHostBuilderExtensions /// /// Replaces TelegramBotHostBuilder. Configures DI, options, and handlers. /// - public static IHostApplicationBuilder AddWideTelegrator(this IHostApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null) + public static IHostApplicationBuilder AddWideTelegrator(this IHostApplicationBuilder builder, Func dbConnectionFactory, TelegratorOptions? options = null, IHandlersCollection? handlers = null) { - AddWideTelegratorInternal(builder.Services, builder.Configuration, builder.Properties, ref handlers, options); + AddWideTelegratorInternal(builder.Services, builder.Configuration, builder.Properties, dbConnectionFactory, ref handlers, options); return builder; } /// /// Replaces TelegramBotHostBuilder. Configures DI, options, and handlers. /// - internal static void AddWideTelegratorInternal(IServiceCollection services, IConfiguration configuration, IDictionary properties, [NotNull] ref IHandlersCollection? handlers, TelegratorOptions? options = null) + internal static void AddWideTelegratorInternal(IServiceCollection services, IConfiguration configuration, IDictionary properties, Func dbConnectionFactory, [NotNull] ref IHandlersCollection? handlers, TelegratorOptions? options = null) { if (services.Any(srvc => srvc.ServiceType == typeof(HostedUpdateReceiver))) throw new InvalidOperationException("`HostedUpdateReceiver` found in services. WideHost extension is not compatible with default long-polling receiver. Please, remove `AddTelegrator` invocation from your Host configuration."); @@ -147,16 +148,32 @@ public static class WideHostBuilderExtensions if (!services.Any(srvc => srvc.ServiceType == typeof(IOptions))) { - // For now, there's no way to configure this from IConfiguration, use `ConfigureWideTelegram` instead - throw new MissingMemberException("No options of type 'WTelegramBotClientOptions' was registered. This configuration is runtime required! Use `ConfigureWideTelegram` to register options."); + WideBotOptions? wideBotOptions = configuration.GetSection(nameof(WideBotOptions)).Get(); + if (wideBotOptions == null) + throw new MissingMemberException("Auto configuration disabled, yet no options of type 'WideBotOptions' was registered. This configuration is runtime required!"); - /* - services.AddSingleton(Options.Create(new WTelegramBotClientOptions(options.Token, options.BaseUrl, options.UseTestEnvironment) + services.AddSingleton(provider => { - RetryCount = options.RetryCount, - RetryThreshold = options.RetryThreshold - })); - */ + IHostApplicationLifetime lifetime = provider.GetRequiredService(); + + DbConnection dbConnection = dbConnectionFactory.Invoke(provider); + lifetime.ApplicationStopping.Register(() => dbConnection.Dispose()); + + WTelegramBotClientOptions wideOptions = new WTelegramBotClientOptions( + token: options.Token, + apiId: wideBotOptions.ApiId, + apiHash: wideBotOptions.ApiHash, + dbConnection: dbConnection, + sqlCommands: wideBotOptions.SqlCommands, + useTestEnvironment: options.UseTestEnvironment, + mtproxy: wideBotOptions.MTProxy) + { + RetryCount = options.RetryCount, + RetryThreshold = options.RetryThreshold + }; + + return Options.Create(wideOptions); + }); } services.AddTelegramBotHostDefaults(); @@ -183,6 +200,19 @@ public static class WideBotServiceCollectionExtensions return services; } + /// + /// Adds WTelegramBotClientOptions to services + /// + /// + /// + /// + public static IServiceCollection ConfigureWideBot(this IServiceCollection services, WideBotOptions options) + { + services.RemoveAll>(); + services.AddSingleton(Options.Create(options)); + return services; + } + /// /// Adds WTelegramBotClient /// diff --git a/src/Telegrator.Hosting.WideBot/WideBotOptions.cs b/src/Telegrator.Hosting.WideBot/WideBotOptions.cs new file mode 100644 index 0000000..7beadc9 --- /dev/null +++ b/src/Telegrator.Hosting.WideBot/WideBotOptions.cs @@ -0,0 +1,16 @@ +using WTelegram; + +namespace Telegrator; + +public class WideBotOptions +{ + public required int ApiId { get; set; } + + public required string ApiHash { get; set; } + + public string? MTProxy { get; set; } + + public bool DropPendingUpdates { get; set; } + + public SqlCommands SqlCommands { get; set; } = WTelegram.SqlCommands.Detect; +} diff --git a/src/Telegrator.Hosting/Telegrator.Hosting.csproj b/src/Telegrator.Hosting/Telegrator.Hosting.csproj index e73f909..3a51eb7 100644 --- a/src/Telegrator.Hosting/Telegrator.Hosting.csproj +++ b/src/Telegrator.Hosting/Telegrator.Hosting.csproj @@ -13,9 +13,12 @@ True True True + True + snupkg + False Telegrator.Hosting - 1.17.0 + 1.17.1 Rikitav Tim4ik Rikitav Tim4ik https://github.com/Rikitav/Telegrator diff --git a/src/Telegrator.Localized/README.md b/src/Telegrator.Localized/README.md new file mode 100644 index 0000000..e69de29 diff --git a/src/Telegrator.Localized/Telegrator.Localized.csproj b/src/Telegrator.Localized/Telegrator.Localized.csproj index ee731d7..c76293f 100644 --- a/src/Telegrator.Localized/Telegrator.Localized.csproj +++ b/src/Telegrator.Localized/Telegrator.Localized.csproj @@ -2,7 +2,31 @@ netstandard2.1 + enable + enable latest + Telegrator + ..\..\bin + ..\..\docs\$(AssemblyName).xml + + False + True + True + True + True + snupkg + False + + Telegrator.Localized + 1.17.1 + Rikitav Tim4ik + Rikitav Tim4ik + https://github.com/Rikitav/Telegrator + telegram;bot;mediator;attributes;aspect;hosting;host;framework;easy;simple;handlers + + telegrator_nuget.png + README.md + LICENSE @@ -13,4 +37,10 @@ + + + + + + diff --git a/src/Telegrator/Telegrator.csproj b/src/Telegrator/Telegrator.csproj index 3323936..5e79dfc 100644 --- a/src/Telegrator/Telegrator.csproj +++ b/src/Telegrator/Telegrator.csproj @@ -12,9 +12,12 @@ True True True + True + snupkg + False Telegrator : Telegram.Bot mediator framework - 1.17.0 + 1.17.1 Rikitav Tim4ik Rikitav Tim4ik https://github.com/Rikitav/Telegrator diff --git a/tests/Telegrator.Tests/Program.cs b/tests/Telegrator.Tests/Program.cs index e90b378..73e0328 100644 --- a/tests/Telegrator.Tests/Program.cs +++ b/tests/Telegrator.Tests/Program.cs @@ -1,8 +1,7 @@ using Microsoft.AspNetCore.Builder; using Microsoft.Data.Sqlite; using Microsoft.Extensions.Hosting; -using System.Data.Common; -using Telegram.Bot; +using Telegram.Bot.Polling; namespace Telegrator.Tests; @@ -16,6 +15,12 @@ internal static class Program ApplicationName = "Host example", }); + builder.Services.ConfigureReceiver(new ReceiverOptions() + { + DropPendingUpdates = true, + Limit = 100 + }); + builder.AddTelegrator(action: builder => builder.Handlers .CollectHandlersAssemblyWide()); @@ -32,12 +37,16 @@ internal static class Program ApplicationName = "WBot example", }); - using DbConnection connection = new SqliteConnection("Data Source=wtgb.db"); - builder.Services.ConfigureWideTelegram( - new WTelegramBotClientOptions(token: "BOT_TOKEN", apiId: 123, apiHash: "API_HASH", dbConnection: connection)); + builder.Services.ConfigureWideBot(new WideBotOptions() + { + ApiId = 123, + ApiHash = "API_HASH", + DropPendingUpdates = true, + }); - builder.AddWideTelegrator(action: builder => builder.Handlers - .CollectHandlersAssemblyWide()); + builder.AddWideTelegrator( + dbConnectionFactory: provider => new SqliteConnection($"Data Source={Environment.ExpandEnvironmentVariables("%AppData%\\Telegrator\\%wtgb.db")}"), + action: builder => builder.Handlers.CollectHandlersAssemblyWide()); builder.Build() .UseWideTelegrator() @@ -52,6 +61,13 @@ internal static class Program ApplicationName = "WebApplication example", }); + builder.Services.ConfigureWebhooker(new WebhookerOptions() + { + WebhookUri = "https://medic-gaming.com/", + DropPendingUpdates = true, + SecretToken = "MEDIC_GAMING" + }); + builder.AddTelegratorWeb(action: builder => builder.Handlers .CollectHandlersAssemblyWide());