diff --git a/.gitignore b/.gitignore index 0010c68..6c06f36 100644 --- a/.gitignore +++ b/.gitignore @@ -361,8 +361,5 @@ MigrationBackup/ # Fody - auto-generated XML schema FodyWeavers.xsd -/GETTING_STARTED.md -/ANNOTATION_OVERVIEW.md -/.aider.input.history -/.aider.chat.history.md -/.aider.tags.cache.v4 + +.aider* diff --git a/README.md b/README.md index 53a7184..da9e099 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,10 @@ -# Telegrator - ![Telegrator Banner](https://github.com/Rikitav/Telegrator/blob/master/resources%2FTelegrator_banner.png) -> **A modern reactive framework for Telegram bots in C# with aspect-oriented design, mediator-based dispatching, and flexible architecture.** - --- ## 🚀 About Telegrator -Telegrator is a next-generation framework for building Telegram bots in C#, inspired by AOP (Aspect-Oriented Programming) and the mediator pattern. It enables decentralized, easily extensible, and maintainable bot logic without traditional state machines or monolithic handlers. +Telegrator is a modern C# framework for building Telegram bots, inspired by AOP (Aspect-Oriented Programming) and the mediator pattern. It enables decentralized, easily extensible, and maintainable bot logic without traditional state machines or monolithic handlers. --- diff --git a/Telegrator.slnx b/Telegrator.slnx index 8f71ba5..a61c426 100644 --- a/Telegrator.slnx +++ b/Telegrator.slnx @@ -1,4 +1,5 @@ + diff --git a/docs/Telegrator.Hosting.Web.xml b/docs/Telegrator.Hosting.Web.xml index 931193e..83cc9a4 100644 --- a/docs/Telegrator.Hosting.Web.xml +++ b/docs/Telegrator.Hosting.Web.xml @@ -100,6 +100,12 @@ + + + + + + Initializes a new instance of the class. @@ -107,6 +113,14 @@ + + + Initializes a new instance of the class. + + + + + Initializes a new instance of the class. @@ -115,12 +129,24 @@ + + + Initializes a new instance of the class. + + + + + + Builds the host. + + + Configuration options for Telegram bot behavior and execution settings. @@ -185,7 +211,7 @@ - + Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers. diff --git a/docs/Telegrator.Hosting.xml b/docs/Telegrator.Hosting.xml index 4a50bf5..f576f08 100644 --- a/docs/Telegrator.Hosting.xml +++ b/docs/Telegrator.Hosting.xml @@ -13,7 +13,7 @@ - + Implementation of that provides bot information. Contains metadata about the Telegram bot including user details and service provider for wider filterring abilities @@ -35,27 +35,6 @@ Provides access to configuration of this Hosted telegram bot - - - Interface for building Telegram bot hosts with dependency injection support. - Combines host application building capabilities with handler collection functionality. - - - - - Gets the set of key/value configuration properties. - - - - - Gets a collection of logging providers for the application to compose. This is useful for adding new logging providers. - - - - - Gets a collection of services for the application to compose. This is useful for adding user provided or framework provided services. - - Represents a hosted telegram bot @@ -133,6 +112,12 @@ + + + + + + Initializes a new instance of the class. @@ -140,6 +125,14 @@ + + + Initializes a new instance of the class. + + + + + Initializes a new instance of the class. @@ -148,12 +141,24 @@ + + + Initializes a new instance of the class. + + + + + + Builds the host. + + + Adapter for Microsoft.Extensions.Logging to work with Telegrator logging system. @@ -190,12 +195,6 @@ - - - - - - @@ -204,7 +203,7 @@ of this router - + @@ -259,7 +258,7 @@ - + Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers. diff --git a/docs/Telegrator.xml b/docs/Telegrator.xml index 0b5c2ab..88ee2c8 100644 --- a/docs/Telegrator.xml +++ b/docs/Telegrator.xml @@ -6345,16 +6345,6 @@ The bot configuration options. - - - Initializes a new instance of the class with a custom handlers pool. - - The provider for regular handlers. - The provider for awaiting handlers. - The bot configuration options. - The custom handlers pool to use. - - Handles errors that occur during update processing. diff --git a/examples/EchoBot/EchoBot.csproj b/examples/EchoBot/EchoBot.csproj new file mode 100644 index 0000000..7b14237 --- /dev/null +++ b/examples/EchoBot/EchoBot.csproj @@ -0,0 +1,13 @@ + + + + Exe + net10.0 + enable + enable + + + + + + diff --git a/examples/EchoBot/Program.cs b/examples/EchoBot/Program.cs new file mode 100644 index 0000000..ed3a8ea --- /dev/null +++ b/examples/EchoBot/Program.cs @@ -0,0 +1,11 @@ +using Telegrator; + +namespace Telegrator.Examples; + +public class EchoBot +{ + public static void Main(string[] args) + { + var client = new TelegratorClient(); + } +} \ No newline at end of file diff --git a/src/Telegrator.Hosting.Web/Hosting.Web/TelegramBotWebHostBuilder.cs b/src/Telegrator.Hosting.Web/Hosting.Web/TelegramBotWebHostBuilder.cs index 152496b..16f8de7 100644 --- a/src/Telegrator.Hosting.Web/Hosting.Web/TelegramBotWebHostBuilder.cs +++ b/src/Telegrator.Hosting.Web/Hosting.Web/TelegramBotWebHostBuilder.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Diagnostics.Metrics; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Telegrator.Core; @@ -11,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 : ITelegramBotHostBuilder + public class TelegramBotWebHostBuilder : IHostApplicationBuilder, ICollectingProvider { private readonly WebApplicationBuilder _innerBuilder; private readonly WebApplicationOptions _settings; @@ -32,17 +33,37 @@ namespace Telegrator.Hosting.Web /// public IHostEnvironment Environment => _innerBuilder.Environment; + /// + public IDictionary Properties => ((IHostApplicationBuilder)_innerBuilder).Properties; + + /// + public IMetricsBuilder Metrics => _innerBuilder.Metrics; + /// /// Initializes a new instance of the class. /// /// /// - public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder, WebApplicationOptions settings) + public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder, WebApplicationOptions? settings = null) { _innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder)); _settings = settings ?? throw new ArgumentNullException(nameof(settings)); - _innerBuilder.AddTelegratorWeb(); + this.AddTelegratorWeb(); + } + + /// + /// Initializes a new instance of the class. + /// + /// + /// + /// + 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); } /// @@ -56,7 +77,22 @@ namespace Telegrator.Hosting.Web _innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder)); _settings = settings ?? throw new ArgumentNullException(nameof(settings)); - _innerBuilder.AddTelegratorWeb(null, handlers); + this.AddTelegratorWeb(null, handlers); + } + + /// + /// Initializes a new instance of the class. + /// + /// + /// + /// + /// + 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); } /// @@ -69,5 +105,11 @@ namespace Telegrator.Hosting.Web host.UseTelegrator(); return host; } + + /// + public void ConfigureContainer(IServiceProviderFactory factory, Action? configure = null) where TContainerBuilder : notnull + { + ((IHostApplicationBuilder)_innerBuilder).ConfigureContainer(factory, configure); + } } } diff --git a/src/Telegrator.Hosting.Web/Program.cs b/src/Telegrator.Hosting.Web/Program.cs index 910d903..bdc26f9 100644 --- a/src/Telegrator.Hosting.Web/Program.cs +++ b/src/Telegrator.Hosting.Web/Program.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Telegrator.Hosting; using Telegrator.Hosting.Web; @@ -43,10 +44,14 @@ internal class Program public static void TelegramBotHostBuilder_Example(string[] args) { + ConfigurationManager configuration = new ConfigurationManager(); + configuration.AddJsonFile("appsettings.json"); + TelegramBotHostBuilder builder = TelegramBotHost.CreateBuilder(new HostApplicationBuilderSettings() { Args = args, ApplicationName = "TelegramBotHost example", + Configuration = configuration }); builder.Handlers.CollectHandlersAssemblyWide(); diff --git a/src/Telegrator.Hosting.Web/Telegrator.Hosting.Web.csproj b/src/Telegrator.Hosting.Web/Telegrator.Hosting.Web.csproj index 63bcbf4..b886efd 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.1 + 1.16.2 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 1d6cf25..f55bff0 100644 --- a/src/Telegrator.Hosting.Web/TypesExtensions.cs +++ b/src/Telegrator.Hosting.Web/TypesExtensions.cs @@ -7,6 +7,7 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; 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/Hosting/HostedTelegramBotInfo.cs b/src/Telegrator.Hosting/Hosting/HostedTelegramBotInfo.cs index 4165803..207abc5 100644 --- a/src/Telegrator.Hosting/Hosting/HostedTelegramBotInfo.cs +++ b/src/Telegrator.Hosting/Hosting/HostedTelegramBotInfo.cs @@ -12,7 +12,7 @@ namespace Telegrator.Hosting /// /// /// - public class HostedTelegramBotInfo(ITelegramBotClient client, IServiceProvider services, IConfigurationManager configuration) : ITelegramBotInfo + public class HostedTelegramBotInfo(ITelegramBotClient client, IServiceProvider services, IConfiguration configuration) : ITelegramBotInfo { /// public User User { get; } = client.GetMe().Result; @@ -25,6 +25,6 @@ namespace Telegrator.Hosting /// /// Provides access to configuration of this Hosted telegram bot /// - public IConfigurationManager Configuration { get; } = configuration; + public IConfiguration Configuration { get; } = configuration; } } diff --git a/src/Telegrator.Hosting/Hosting/ITelegramBotHostBuilder.cs b/src/Telegrator.Hosting/Hosting/ITelegramBotHostBuilder.cs deleted file mode 100644 index 49a4247..0000000 --- a/src/Telegrator.Hosting/Hosting/ITelegramBotHostBuilder.cs +++ /dev/null @@ -1,29 +0,0 @@ -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Telegrator.Core; - -namespace Telegrator.Hosting -{ - /// - /// Interface for building Telegram bot hosts with dependency injection support. - /// Combines host application building capabilities with handler collection functionality. - /// - public interface ITelegramBotHostBuilder : ICollectingProvider - { - /// - /// Gets the set of key/value configuration properties. - /// - IConfigurationManager Configuration { get; } - - /// - /// Gets a collection of logging providers for the application to compose. This is useful for adding new logging providers. - /// - ILoggingBuilder Logging { get; } - - /// - /// Gets a collection of services for the application to compose. This is useful for adding user provided or framework provided services. - /// - IServiceCollection Services { get; } - } -} diff --git a/src/Telegrator.Hosting/Hosting/TelegramBotHost.cs b/src/Telegrator.Hosting/Hosting/TelegramBotHost.cs index 73127a1..dadddcd 100644 --- a/src/Telegrator.Hosting/Hosting/TelegramBotHost.cs +++ b/src/Telegrator.Hosting/Hosting/TelegramBotHost.cs @@ -56,9 +56,6 @@ namespace Telegrator.Hosting { HostApplicationBuilder innerBuilder = new HostApplicationBuilder(settings: null); TelegramBotHostBuilder builder = new TelegramBotHostBuilder(innerBuilder, null); - - builder.Services.AddTelegramBotHostDefaults(); - builder.Services.AddTelegramReceiver(); return builder; } @@ -70,9 +67,6 @@ namespace Telegrator.Hosting { HostApplicationBuilder innerBuilder = new HostApplicationBuilder(settings); TelegramBotHostBuilder builder = new TelegramBotHostBuilder(innerBuilder, settings); - - builder.Services.AddTelegramBotHostDefaults(); - builder.Services.AddTelegramReceiver(); return builder; } diff --git a/src/Telegrator.Hosting/Hosting/TelegramBotHostBuilder.cs b/src/Telegrator.Hosting/Hosting/TelegramBotHostBuilder.cs index 70ae0c9..ce12df9 100644 --- a/src/Telegrator.Hosting/Hosting/TelegramBotHostBuilder.cs +++ b/src/Telegrator.Hosting/Hosting/TelegramBotHostBuilder.cs @@ -1,7 +1,9 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Diagnostics.Metrics; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using Telegrator.Core; using Telegrator.Providers; @@ -11,7 +13,7 @@ namespace Telegrator.Hosting /// /// Represents a hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more. /// - public class TelegramBotHostBuilder : ICollectingProvider + public class TelegramBotHostBuilder : IHostApplicationBuilder, ICollectingProvider { private readonly HostApplicationBuilder _innerBuilder; private readonly HostApplicationBuilderSettings _settings; @@ -32,6 +34,12 @@ namespace Telegrator.Hosting /// public IHostEnvironment Environment => _innerBuilder.Environment; + /// + public IDictionary Properties => ((IHostApplicationBuilder)_innerBuilder).Properties; + + /// + public IMetricsBuilder Metrics => _innerBuilder.Metrics; + /// /// Initializes a new instance of the class. /// @@ -42,8 +50,21 @@ namespace Telegrator.Hosting _innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder)); _settings = settings ?? new HostApplicationBuilderSettings(); - _innerBuilder.AddTelegrator(); - _innerBuilder.Logging.ClearProviders(); + this.AddTelegrator(); + } + + /// + /// Initializes a new instance of the class. + /// + /// + /// + /// + public TelegramBotHostBuilder(HostApplicationBuilder hostApplicationBuilder, TelegratorOptions? options, HostApplicationBuilderSettings? settings) + { + _innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder)); + _settings = settings ?? new HostApplicationBuilderSettings(); + + this.AddTelegrator(options, null); } /// @@ -52,13 +73,27 @@ namespace Telegrator.Hosting /// /// /// - public TelegramBotHostBuilder(HostApplicationBuilder hostApplicationBuilder, IHandlersCollection handlers, HostApplicationBuilderSettings? settings = null) + public TelegramBotHostBuilder(HostApplicationBuilder hostApplicationBuilder, IHandlersCollection handlers, HostApplicationBuilderSettings? settings) { _innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder)); _settings = settings ?? new HostApplicationBuilderSettings(); - _innerBuilder.AddTelegrator(null, handlers); - _innerBuilder.Logging.ClearProviders(); + this.AddTelegrator(null, handlers); + } + + /// + /// Initializes a new instance of the class. + /// + /// + /// + /// + /// + 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); } /// @@ -71,5 +106,11 @@ namespace Telegrator.Hosting host.UseTelegrator(); return host; } + + /// + public void ConfigureContainer(IServiceProviderFactory factory, Action? configure = null) where TContainerBuilder : notnull + { + this.ConfigureContainer(factory, configure); + } } } diff --git a/src/Telegrator.Hosting/Polling/HostUpdateHandlersPool.cs b/src/Telegrator.Hosting/Polling/HostUpdateHandlersPool.cs deleted file mode 100644 index 0601a67..0000000 --- a/src/Telegrator.Hosting/Polling/HostUpdateHandlersPool.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Microsoft.Extensions.Options; -using Telegrator.Core; -using Telegrator.Mediation; - -namespace Telegrator.Polling -{ - /// - public class HostUpdateHandlersPool(IUpdateRouter router, IOptions options) - : UpdateHandlersPool(router, options.Value, options.Value.GlobalCancellationToken) - { - - } -} diff --git a/src/Telegrator.Hosting/Polling/HostUpdateRouter.cs b/src/Telegrator.Hosting/Polling/HostUpdateRouter.cs index ae70fa7..9258bf1 100644 --- a/src/Telegrator.Hosting/Polling/HostUpdateRouter.cs +++ b/src/Telegrator.Hosting/Polling/HostUpdateRouter.cs @@ -21,9 +21,8 @@ namespace Telegrator.Polling IHandlersProvider handlersProvider, IAwaitingProvider awaitingProvider, IOptions options, - IUpdateHandlersPool handlersPool, ITelegramBotInfo botInfo, - ILogger logger) : base(handlersProvider, awaitingProvider, options.Value, handlersPool, botInfo) + ILogger logger) : base(handlersProvider, awaitingProvider, options.Value, botInfo) { Logger = logger; ExceptionHandler = new DefaultRouterExceptionHandler(HandleException); diff --git a/src/Telegrator.Hosting/Telegrator.Hosting.csproj b/src/Telegrator.Hosting/Telegrator.Hosting.csproj index 740fda4..4f095f3 100644 --- a/src/Telegrator.Hosting/Telegrator.Hosting.csproj +++ b/src/Telegrator.Hosting/Telegrator.Hosting.csproj @@ -15,7 +15,7 @@ True Telegrator.Hosting - 1.16.1 + 1.16.2 Rikitav Tim4ik Rikitav Tim4ik https://github.com/Rikitav/Telegrator diff --git a/src/Telegrator.Hosting/TypesExtensions.cs b/src/Telegrator.Hosting/TypesExtensions.cs index 159bae4..1ecc7b5 100644 --- a/src/Telegrator.Hosting/TypesExtensions.cs +++ b/src/Telegrator.Hosting/TypesExtensions.cs @@ -120,7 +120,6 @@ public static class ServicesCollectionExtensions public static IServiceCollection AddTelegramBotHostDefaults(this IServiceCollection services) { services.AddLogging(builder => builder.AddConsole().AddDebug()); - services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); diff --git a/src/Telegrator/Core/Descriptors/DescribedHandlerDescriptor.cs b/src/Telegrator/Core/Descriptors/DescribedHandlerDescriptor.cs index 08cf0dd..1079274 100644 --- a/src/Telegrator/Core/Descriptors/DescribedHandlerDescriptor.cs +++ b/src/Telegrator/Core/Descriptors/DescribedHandlerDescriptor.cs @@ -114,7 +114,7 @@ namespace Telegrator.Core.Descriptors /// The execution result. public void ReportResult(Result? result) { - if (result != null) + if (Result != null) throw new InvalidOperationException("Result already reported"); Result = result; diff --git a/src/Telegrator/Mediation/UpdateRouter.cs b/src/Telegrator/Mediation/UpdateRouter.cs index 7dc8fa1..d14b545 100644 --- a/src/Telegrator/Mediation/UpdateRouter.cs +++ b/src/Telegrator/Mediation/UpdateRouter.cs @@ -58,23 +58,6 @@ namespace Telegrator.Mediation _botInfo = botInfo; } - /// - /// Initializes a new instance of the class with a custom handlers pool. - /// - /// The provider for regular handlers. - /// The provider for awaiting handlers. - /// The bot configuration options. - /// The custom handlers pool to use. - /// - public UpdateRouter(IHandlersProvider handlersProvider, IAwaitingProvider awaitingProvider, TelegratorOptions options, IUpdateHandlersPool handlersPool, ITelegramBotInfo botInfo) - { - _options = options; - _handlersProvider = handlersProvider; - _awaitingProvider = awaitingProvider; - _HandlersPool = handlersPool; - _botInfo = botInfo; - } - /// /// Handles errors that occur during update processing. /// diff --git a/src/Telegrator/Telegrator.csproj b/src/Telegrator/Telegrator.csproj index bd677f9..4eaee42 100644 --- a/src/Telegrator/Telegrator.csproj +++ b/src/Telegrator/Telegrator.csproj @@ -14,7 +14,7 @@ True Telegrator : Telegram.Bot mediator framework - 1.16.1 + 1.16.2 Rikitav Tim4ik Rikitav Tim4ik https://github.com/Rikitav/Telegrator