11 Commits

Author SHA1 Message Date
Rikitav e28cc2b8da Removed test application 2026-03-08 23:17:52 +04:00
Rikitav c6cda703ef version incremented 2026-03-08 22:02:30 +04:00
Rikitav 401c8d02aa fixed double UseTelegrator invokation 2026-03-08 22:02:01 +04:00
Rikitav 2cf4910abd * fixed loop dependency
* fixed router not getting result
* fixed hosts configuration
2026-03-08 21:59:50 +04:00
Rikitav 81da5e0bc7 * fixed loop dependency
* fixed router not getting result
* fixed hosts configuration
2026-03-08 19:43:48 +04:00
Rikitav b42e03fe06 docs updated 2026-03-07 23:38:14 +04:00
Rikitav 4b14751c8d updated docs 2026-03-07 23:30:47 +04:00
Rikitav 3fe0cc3b7c errors fixes 2026-03-07 23:23:30 +04:00
Rikitav ff2060e250 incrementedd version 2026-03-07 23:22:40 +04:00
Rikitav 7df9e1e952 Updated readmes 2026-03-07 20:53:41 +04:00
Rikitav a3fd6a6007 Updated readmes 2026-03-07 20:46:04 +04:00
26 changed files with 3015 additions and 185 deletions
+2 -2
View File
@@ -361,5 +361,5 @@ MigrationBackup/
# Fody - auto-generated XML schema
FodyWeavers.xsd
/GETTING_STARTED.md
/ANNOTATION_OVERVIEW.md
.aider*
+7 -11
View File
@@ -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.
---
@@ -55,7 +51,7 @@ using Telegrator.Annotations;
[MessageHandler]
public class HelloHandler : MessageHandler
{
public override async Task<Result> Execute(IAbstractHandlerContainer<Message> container, CancellationToken cancellation)
public override async Task<Result> Execute(IHandlerContainer<Message> container, CancellationToken cancellation)
{
await Reply("Hello, world!", cancellationToken: cancellation);
return Result.Ok();
@@ -75,10 +71,10 @@ using Telegram.Bot.Types.Enums;
using Telegrator.Handlers;
using Telegrator.Annotations;
[CommandHandler, CommandAllias("start", "hello"), ChatType(ChatType.Private)]
[CommandHandler, CommandAlias("start", "hello"), ChatType(ChatType.Private)]
public class StartCommandHandler : CommandHandler
{
public override async Task<Result> Execute(IAbstractHandlerContainer<Message> container, CancellationToken cancellation)
public override async Task<Result> Execute(IHandlerContainer<Message> container, CancellationToken cancellation)
{
await Responce("Welcome!", cancellationToken: cancellation);
return Result.Ok();
@@ -95,10 +91,10 @@ bot.Handlers.AddHandler<StartCommandHandler>();
using Telegrator.Handlers;
using Telegrator.Annotations;
[CommandHandler, CommandAllias("first"), NumericState(SpecialState.NoState)]
[CommandHandler, CommandAlias("first"), NumericState(SpecialState.NoState)]
public class StateKeepFirst : CommandHandler
{
public override async Task<Result> Execute(IAbstractHandlerContainer<Message> container, CancellationToken cancellation)
public override async Task<Result> Execute(IHandlerContainer<Message> container, CancellationToken cancellation)
{
container.CreateNumericState();
container.ForwardNumericState();
@@ -126,7 +122,7 @@ bot.Handlers.AddHandler<StateKeepFirst>();
## 📚 Documentation & Examples
- [Documentation](https://github.com/Rikitav/Telegrator/wiki/)
- [Usage examples (WIP)](https://github.com/Rikitav/Telegrator/tree/master/Examples)
- [Usage examples (WIP)](https://github.com/Rikitav/Telegrator/tree/master/examples)
---
File diff suppressed because it is too large Load Diff
+27 -1
View File
@@ -100,6 +100,12 @@
<member name="P:Telegrator.Hosting.Web.TelegramBotWebHostBuilder.Environment">
<inheritdoc/>
</member>
<member name="P:Telegrator.Hosting.Web.TelegramBotWebHostBuilder.Properties">
<inheritdoc/>
</member>
<member name="P:Telegrator.Hosting.Web.TelegramBotWebHostBuilder.Metrics">
<inheritdoc/>
</member>
<member name="M:Telegrator.Hosting.Web.TelegramBotWebHostBuilder.#ctor(Microsoft.AspNetCore.Builder.WebApplicationBuilder,Microsoft.AspNetCore.Builder.WebApplicationOptions)">
<summary>
Initializes a new instance of the <see cref="T:Telegrator.Hosting.Web.TelegramBotWebHostBuilder"/> class.
@@ -107,6 +113,14 @@
<param name="webApplicationBuilder"></param>
<param name="settings"></param>
</member>
<member name="M:Telegrator.Hosting.Web.TelegramBotWebHostBuilder.#ctor(Microsoft.AspNetCore.Builder.WebApplicationBuilder,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="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>
Initializes a new instance of the <see cref="T:Telegrator.Hosting.Web.TelegramBotWebHostBuilder"/> class.
@@ -115,12 +129,24 @@
<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 name="M:Telegrator.Hosting.Web.TelegramBotWebHostBuilder.Build">
<summary>
Builds the host.
</summary>
<returns></returns>
</member>
<member name="M:Telegrator.Hosting.Web.TelegramBotWebHostBuilder.ConfigureContainer``1(Microsoft.Extensions.DependencyInjection.IServiceProviderFactory{``0},System.Action{``0})">
<inheritdoc/>
</member>
<member name="T:Telegrator.Hosting.Web.WebhookerOptions">
<summary>
Configuration options for Telegram bot behavior and execution settings.
@@ -185,7 +211,7 @@
<member name="M:Telegrator.ServicesCollectionExtensions.get_Handlers(Microsoft.AspNetCore.Builder.WebApplicationBuilder)">
<inheritdoc cref="P:Telegrator.ServicesCollectionExtensions.&lt;G&gt;$41F16C2D39AF52899E745C9C9F42FF83.Handlers"/>
</member>
<member name="M:Telegrator.ServicesCollectionExtensions.AddTelegratorWeb(Microsoft.Extensions.Hosting.IHostApplicationBuilder,Microsoft.AspNetCore.Builder.WebApplicationOptions,Telegrator.TelegratorOptions,Telegrator.Core.IHandlersCollection)">
<member name="M:Telegrator.ServicesCollectionExtensions.AddTelegratorWeb(Microsoft.Extensions.Hosting.IHostApplicationBuilder,Telegrator.TelegratorOptions,Telegrator.Core.IHandlersCollection)">
<summary>
Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers.
</summary>
+29 -30
View File
@@ -13,7 +13,7 @@
<param name="services"></param>
<param name="configuration"></param>
</member>
<member name="M:Telegrator.Hosting.HostedTelegramBotInfo.#ctor(Telegram.Bot.ITelegramBotClient,System.IServiceProvider,Microsoft.Extensions.Configuration.IConfigurationManager)">
<member name="M:Telegrator.Hosting.HostedTelegramBotInfo.#ctor(Telegram.Bot.ITelegramBotClient,System.IServiceProvider,Microsoft.Extensions.Configuration.IConfiguration)">
<summary>
Implementation of <see cref="T:Telegrator.Core.ITelegramBotInfo"/> 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
</summary>
</member>
<member name="T:Telegrator.Hosting.ITelegramBotHostBuilder">
<summary>
Interface for building Telegram bot hosts with dependency injection support.
Combines host application building capabilities with handler collection functionality.
</summary>
</member>
<member name="P:Telegrator.Hosting.ITelegramBotHostBuilder.Configuration">
<summary>
Gets the set of key/value configuration properties.
</summary>
</member>
<member name="P:Telegrator.Hosting.ITelegramBotHostBuilder.Logging">
<summary>
Gets a collection of logging providers for the application to compose. This is useful for adding new logging providers.
</summary>
</member>
<member name="P:Telegrator.Hosting.ITelegramBotHostBuilder.Services">
<summary>
Gets a collection of services for the application to compose. This is useful for adding user provided or framework provided services.
</summary>
</member>
<member name="T:Telegrator.Hosting.TelegramBotHost">
<summary>
Represents a hosted telegram bot
@@ -133,6 +112,12 @@
<member name="P:Telegrator.Hosting.TelegramBotHostBuilder.Environment">
<inheritdoc/>
</member>
<member name="P:Telegrator.Hosting.TelegramBotHostBuilder.Properties">
<inheritdoc/>
</member>
<member name="P:Telegrator.Hosting.TelegramBotHostBuilder.Metrics">
<inheritdoc/>
</member>
<member name="M:Telegrator.Hosting.TelegramBotHostBuilder.#ctor(Microsoft.Extensions.Hosting.HostApplicationBuilder,Microsoft.Extensions.Hosting.HostApplicationBuilderSettings)">
<summary>
Initializes a new instance of the <see cref="T:Telegrator.Hosting.TelegramBotHostBuilder"/> class.
@@ -140,6 +125,14 @@
<param name="hostApplicationBuilder"></param>
<param name="settings"></param>
</member>
<member name="M:Telegrator.Hosting.TelegramBotHostBuilder.#ctor(Microsoft.Extensions.Hosting.HostApplicationBuilder,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="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>
Initializes a new instance of the <see cref="T:Telegrator.Hosting.TelegramBotHostBuilder"/> class.
@@ -148,12 +141,24 @@
<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 name="M:Telegrator.Hosting.TelegramBotHostBuilder.Build">
<summary>
Builds the host.
</summary>
<returns></returns>
</member>
<member name="M:Telegrator.Hosting.TelegramBotHostBuilder.ConfigureContainer``1(Microsoft.Extensions.DependencyInjection.IServiceProviderFactory{``0},System.Action{``0})">
<inheritdoc/>
</member>
<member name="T:Telegrator.Logging.MicrosoftLoggingAdapter">
<summary>
Adapter for Microsoft.Extensions.Logging to work with Telegrator logging system.
@@ -190,12 +195,6 @@
<member name="M:Telegrator.Polling.HostedUpdateReceiver.ExecuteAsync(System.Threading.CancellationToken)">
<inheritdoc/>
</member>
<member name="T:Telegrator.Polling.HostUpdateHandlersPool">
<inheritdoc/>
</member>
<member name="M:Telegrator.Polling.HostUpdateHandlersPool.#ctor(Telegrator.Core.IUpdateRouter,Microsoft.Extensions.Options.IOptions{Telegrator.TelegratorOptions})">
<inheritdoc/>
</member>
<member name="T:Telegrator.Polling.HostUpdateRouter">
<inheritdoc/>
</member>
@@ -204,7 +203,7 @@
<see cref="T:Microsoft.Extensions.Logging.ILogger"/> of this router
</summary>
</member>
<member name="M:Telegrator.Polling.HostUpdateRouter.#ctor(Telegrator.Core.IHandlersProvider,Telegrator.Core.IAwaitingProvider,Microsoft.Extensions.Options.IOptions{Telegrator.TelegratorOptions},Telegrator.Core.IUpdateHandlersPool,Telegrator.Core.ITelegramBotInfo,Microsoft.Extensions.Logging.ILogger{Telegrator.Polling.HostUpdateRouter})">
<member name="M:Telegrator.Polling.HostUpdateRouter.#ctor(Telegrator.Core.IHandlersProvider,Telegrator.Core.IAwaitingProvider,Microsoft.Extensions.Options.IOptions{Telegrator.TelegratorOptions},Telegrator.Core.ITelegramBotInfo,Microsoft.Extensions.Logging.ILogger{Telegrator.Polling.HostUpdateRouter})">
<inheritdoc/>
</member>
<member name="M:Telegrator.Polling.HostUpdateRouter.HandleUpdateAsync(Telegram.Bot.ITelegramBotClient,Telegram.Bot.Types.Update,System.Threading.CancellationToken)">
@@ -259,7 +258,7 @@
<member name="M:Telegrator.HostBuilderExtensions.get_Handlers(Microsoft.Extensions.Hosting.IHostApplicationBuilder)">
<inheritdoc cref="P:Telegrator.HostBuilderExtensions.&lt;G&gt;$605D8CCF64349EA050C790D67C500BD9.Handlers"/>
</member>
<member name="M:Telegrator.HostBuilderExtensions.AddTelegrator(Microsoft.Extensions.Hosting.IHostApplicationBuilder,Microsoft.Extensions.Hosting.HostApplicationBuilderSettings,Telegrator.TelegratorOptions,Telegrator.Core.IHandlersCollection)">
<member name="M:Telegrator.HostBuilderExtensions.AddTelegrator(Microsoft.Extensions.Hosting.IHostApplicationBuilder,Telegrator.TelegratorOptions,Telegrator.Core.IHandlersCollection)">
<summary>
Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers.
</summary>
-10
View File
@@ -6345,16 +6345,6 @@
<param name="options">The bot configuration options.</param>
<param name="botInfo"></param>
</member>
<member name="M:Telegrator.Mediation.UpdateRouter.#ctor(Telegrator.Core.IHandlersProvider,Telegrator.Core.IAwaitingProvider,Telegrator.TelegratorOptions,Telegrator.Core.IUpdateHandlersPool,Telegrator.Core.ITelegramBotInfo)">
<summary>
Initializes a new instance of the <see cref="T:Telegrator.Mediation.UpdateRouter"/> class with a custom handlers pool.
</summary>
<param name="handlersProvider">The provider for regular handlers.</param>
<param name="awaitingProvider">The provider for awaiting handlers.</param>
<param name="options">The bot configuration options.</param>
<param name="handlersPool">The custom handlers pool to use.</param>
<param name="botInfo"></param>
</member>
<member name="M:Telegrator.Mediation.UpdateRouter.HandleErrorAsync(Telegram.Bot.ITelegramBotClient,System.Exception,Telegram.Bot.Polling.HandleErrorSource,System.Threading.CancellationToken)">
<summary>
Handles errors that occur during update processing.
+13
View File
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Telegrator" Version="1.16.2"/>
</ItemGroup>
</Project>
+11
View File
@@ -0,0 +1,11 @@
using Telegrator;
namespace Telegrator.Examples;
public class EchoBot
{
public static void Main(string[] args)
{
var client = new TelegratorClient();
}
}
@@ -53,7 +53,6 @@ namespace Telegrator.Hosting.Web
{
// Building proxy application
_innerApp = webApplicationBuilder.Build();
_innerApp.UseTelegratorWeb();
// Reruesting services for this host
_updateRouter = Services.GetRequiredService<IUpdateRouter>();
@@ -1,13 +1,10 @@
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 Microsoft.Extensions.Options;
using Telegram.Bot;
using Telegrator;
using Telegrator.Core;
using Telegrator.Providers;
#pragma warning disable IDE0001
namespace Telegrator.Hosting.Web
@@ -15,7 +12,7 @@ namespace Telegrator.Hosting.Web
/// <summary>
/// Represents a web hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more.
/// </summary>
public class TelegramBotWebHostBuilder : ITelegramBotHostBuilder
public class TelegramBotWebHostBuilder : IHostApplicationBuilder, ICollectingProvider
{
private readonly WebApplicationBuilder _innerBuilder;
private readonly WebApplicationOptions _settings;
@@ -36,17 +33,37 @@ namespace Telegrator.Hosting.Web
/// <inheritdoc/>
public IHostEnvironment Environment => _innerBuilder.Environment;
/// <inheritdoc/>
public IDictionary<object, object> Properties => ((IHostApplicationBuilder)_innerBuilder).Properties;
/// <inheritdoc/>
public IMetricsBuilder Metrics => _innerBuilder.Metrics;
/// <summary>
/// Initializes a new instance of the <see cref="TelegramBotWebHostBuilder"/> class.
/// </summary>
/// <param name="webApplicationBuilder"></param>
/// <param name="settings"></param>
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(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>
@@ -60,7 +77,22 @@ namespace Telegrator.Hosting.Web
_innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder));
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
_innerBuilder.AddTelegratorWeb(settings, null, 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>
@@ -73,5 +105,11 @@ namespace Telegrator.Hosting.Web
host.UseTelegrator();
return host;
}
/// <inheritdoc/>
public void ConfigureContainer<TContainerBuilder>(IServiceProviderFactory<TContainerBuilder> factory, Action<TContainerBuilder>? configure = null) where TContainerBuilder : notnull
{
((IHostApplicationBuilder)_innerBuilder).ConfigureContainer(factory, configure);
}
}
}
+82
View File
@@ -0,0 +1,82 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Telegrator.Hosting;
using Telegrator.Hosting.Web;
namespace Telegrator;
internal class Program
{
public static void TelegramBotWebHostBuilder_Example(string[] args)
{
TelegramBotWebHostBuilder builder = TelegramBotWebHost.CreateBuilder(new WebApplicationOptions()
{
Args = args,
ApplicationName = "TelegramBotWebHost example",
});
builder.Handlers.CollectHandlersAssemblyWide();
builder.Build()
.AddLoggingAdapter()
.SetBotCommands()
.Run();
}
public static void WebApplicationBuilder_Example(string[] args)
{
WebApplicationBuilder builder = WebApplication.CreateBuilder(new WebApplicationOptions()
{
Args = args,
ApplicationName = "WebApplication example",
});
builder.AddTelegratorWeb();
builder.Handlers.CollectHandlersAssemblyWide();
builder.Build()
.UseTelegratorWeb()
.AddLoggingAdapter()
.SetBotCommands()
.Run();
}
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();
builder.Build()
.AddLoggingAdapter()
.SetBotCommands()
.Run();
}
public static void HostApplicationBuilder_Example(string[] args)
{
HostApplicationBuilder builder = Host.CreateApplicationBuilder(new HostApplicationBuilderSettings()
{
Args = args,
ApplicationName = "Host example",
});
builder.AddTelegrator();
builder.Handlers.CollectHandlersAssemblyWide();
builder.Build()
.UseTelegrator()
.AddLoggingAdapter()
.SetBotCommands()
.Run();
}
}
+42 -9
View File
@@ -16,8 +16,9 @@
---
## Requirements
- .NET 8.0 or later
- .NET 10.0 or later
- ASP.NET Core
- [Telegrator.Hosting](https://github.com/Rikitav/Telegrator)
---
@@ -37,10 +38,10 @@ using Telegrator.Hosting;
using Telegrator.Hosting.Web;
// Creating builder
TelegramBotWebHostBuilder builder = TelegramBotWebHost.CreateBuilder(new TelegramBotWebOptions()
TelegramBotWebHostBuilder builder = TelegramBotWebHost.CreateBuilder(new WebApplicationOptions()
{
Args = args,
ExceptIntersectingCommandAliases = true
ApplicationName = "TelegramBotWebHost example",
});
// Register handlers
@@ -57,27 +58,59 @@ telegramBot.Run();
---
## Application integration Example
**Program.cs (ASP.NET Core):**
```csharp
using Telegrator.Hosting;
using Telegrator.Hosting.Web;
// Creating builder
WebApplicationBuilder builder = WebApplication.CreateBuilder(new WebApplicationOptions()
{
Args = args,
ApplicationName = "WebApplication example",
});
// Adding Telegrator
builder.AddTelegratorWeb();
// Register handlers
builder.Handlers.CollectHandlersAssemblyWide();
// Register your services
builder.Services.AddSingleton<IMyService, MyService>();
// Building and running application
builder.Build()
.UseTelegratorWeb()
.SetBotCommands();
.Run();
```
---
## Configuration (appsettings.json)
```json
{
"TelegramBotClientOptions": {
"Token": "YOUR_BOT_TOKEN"
"TelegratorOptions": {
"Token": "YOUR_BOT_TOKEN",
"ExceptIntersectingCommandAliases": true
}
"TelegratorWebOptions": {
"WebhookerOptioons": {
"WebhookUri" = "https://you-public-host.ru/bot",
"SecretToken": "MEDIC_GAMING"
"DropPendingUpdates": true
}
}
```
- `TelegramBotClientOptions`: Bot token and client settings
---
## Documentation
- [Telegrator Main Docs](https://github.com/Rikitav/Telegrator)
- [Telegrator Main Repository](https://github.com/Rikitav/Telegrator)
- [Getting Started Guide](https://github.com/Rikitav/Telegrator/wiki/Getting-started)
- [Annotation Overview](https://github.com/Rikitav/Telegrator/wiki/Annotation-overview)
@@ -15,7 +15,7 @@
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<Title>Telegrator.Hosting.Web</Title>
<Version>1.16.0</Version>
<Version>1.16.3</Version>
<Authors>Rikitav Tim4ik</Authors>
<Company>Rikitav Tim4ik</Company>
<RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl>
@@ -26,21 +26,18 @@
<PackageLicenseFile>LICENSE</PackageLicenseFile>
</PropertyGroup>
<ItemGroup>
<None Include="..\..\LICENSE" Pack="True" PackagePath="\" />
<None Include="..\..\README.md" Pack="True" PackagePath="\" />
<None Include="..\..\resources\telegrator_nuget.png" Pack="True" PackagePath="\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Telegrator.Hosting\Telegrator.Hosting.csproj" />
</ItemGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Telegram.Bot.AspNetCore" Version="22.5.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Telegram.Bot.AspNetCore" Version="22.5.0" />
<None Include=".\README.md" Pack="True" PackagePath="\" />
<None Include="..\..\LICENSE" Pack="True" PackagePath="\" />
<None Include="..\..\resources\telegrator_nuget.png" Pack="True" PackagePath="\" />
</ItemGroup>
</Project>
@@ -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;
@@ -41,11 +42,8 @@ namespace Telegrator
/// <summary>
/// Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers.
/// </summary>
public static IHostApplicationBuilder AddTelegratorWeb(this IHostApplicationBuilder builder, WebApplicationOptions settings, TelegratorOptions? options = null, IHandlersCollection? handlers = null)
public static IHostApplicationBuilder AddTelegratorWeb(this IHostApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null)
{
if (settings is null)
throw new ArgumentNullException(nameof(settings));
IServiceCollection services = builder.Services;
IConfigurationManager configuration = builder.Configuration;
@@ -12,7 +12,7 @@ namespace Telegrator.Hosting
/// <param name="client"></param>
/// <param name="services"></param>
/// <param name="configuration"></param>
public class HostedTelegramBotInfo(ITelegramBotClient client, IServiceProvider services, IConfigurationManager configuration) : ITelegramBotInfo
public class HostedTelegramBotInfo(ITelegramBotClient client, IServiceProvider services, IConfiguration configuration) : ITelegramBotInfo
{
/// <inheritdoc/>
public User User { get; } = client.GetMe().Result;
@@ -25,6 +25,6 @@ namespace Telegrator.Hosting
/// <summary>
/// Provides access to configuration of this Hosted telegram bot
/// </summary>
public IConfigurationManager Configuration { get; } = configuration;
public IConfiguration Configuration { get; } = configuration;
}
}
@@ -1,29 +0,0 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Telegrator.Core;
namespace Telegrator.Hosting
{
/// <summary>
/// Interface for building Telegram bot hosts with dependency injection support.
/// Combines host application building capabilities with handler collection functionality.
/// </summary>
public interface ITelegramBotHostBuilder : ICollectingProvider
{
/// <summary>
/// Gets the set of key/value configuration properties.
/// </summary>
IConfigurationManager Configuration { get; }
/// <summary>
/// Gets a collection of logging providers for the application to compose. This is useful for adding new logging providers.
/// </summary>
ILoggingBuilder Logging { get; }
/// <summary>
/// Gets a collection of services for the application to compose. This is useful for adding user provided or framework provided services.
/// </summary>
IServiceCollection Services { get; }
}
}
@@ -1,5 +1,4 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Telegrator.Core;
@@ -12,14 +11,13 @@ namespace Telegrator.Hosting
public class TelegramBotHost : IHost, ITelegratorBot
{
private readonly IHost _innerHost;
private readonly IServiceProvider _serviceProvider;
private readonly IUpdateRouter _updateRouter;
private readonly ILogger<TelegramBotHost> _logger;
private bool _disposed;
/// <inheritdoc/>
public IServiceProvider Services => _serviceProvider;
public IServiceProvider Services => _innerHost.Services;
/// <inheritdoc/>
public IUpdateRouter UpdateRouter => _updateRouter;
@@ -40,8 +38,6 @@ namespace Telegrator.Hosting
// Building proxy hoster
_innerHost = hostApplicationBuilder.Build();
_serviceProvider = _innerHost.Services;
_innerHost.UseTelegrator();
// Reruesting services for this host
_updateRouter = Services.GetRequiredService<IUpdateRouter>();
@@ -56,9 +52,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 +63,6 @@ namespace Telegrator.Hosting
{
HostApplicationBuilder innerBuilder = new HostApplicationBuilder(settings);
TelegramBotHostBuilder builder = new TelegramBotHostBuilder(innerBuilder, settings);
builder.Services.AddTelegramBotHostDefaults();
builder.Services.AddTelegramReceiver();
return builder;
}
@@ -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
/// <summary>
/// Represents a hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more.
/// </summary>
public class TelegramBotHostBuilder : ICollectingProvider
public class TelegramBotHostBuilder : IHostApplicationBuilder, ICollectingProvider
{
private readonly HostApplicationBuilder _innerBuilder;
private readonly HostApplicationBuilderSettings _settings;
@@ -32,6 +34,12 @@ namespace Telegrator.Hosting
/// <inheritdoc/>
public IHostEnvironment Environment => _innerBuilder.Environment;
/// <inheritdoc/>
public IDictionary<object, object> Properties => ((IHostApplicationBuilder)_innerBuilder).Properties;
/// <inheritdoc/>
public IMetricsBuilder Metrics => _innerBuilder.Metrics;
/// <summary>
/// Initializes a new instance of the <see cref="TelegramBotHostBuilder"/> class.
/// </summary>
@@ -42,8 +50,21 @@ namespace Telegrator.Hosting
_innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder));
_settings = settings ?? new HostApplicationBuilderSettings();
_innerBuilder.AddTelegrator(_settings);
_innerBuilder.Logging.ClearProviders();
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>
@@ -52,13 +73,27 @@ namespace Telegrator.Hosting
/// <param name="hostApplicationBuilder"></param>
/// <param name="handlers"></param>
/// <param name="settings"></param>
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(_settings, null, handlers);
_innerBuilder.Logging.ClearProviders();
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>
@@ -71,5 +106,11 @@ namespace Telegrator.Hosting
host.UseTelegrator();
return host;
}
/// <inheritdoc/>
public void ConfigureContainer<TContainerBuilder>(IServiceProviderFactory<TContainerBuilder> factory, Action<TContainerBuilder>? configure = null) where TContainerBuilder : notnull
{
this.ConfigureContainer(factory, configure);
}
}
}
@@ -1,13 +0,0 @@
using Microsoft.Extensions.Options;
using Telegrator.Core;
using Telegrator.Mediation;
namespace Telegrator.Polling
{
/// <inheritdoc/>
public class HostUpdateHandlersPool(IUpdateRouter router, IOptions<TelegratorOptions> options)
: UpdateHandlersPool(router, options.Value, options.Value.GlobalCancellationToken)
{
}
}
@@ -21,9 +21,8 @@ namespace Telegrator.Polling
IHandlersProvider handlersProvider,
IAwaitingProvider awaitingProvider,
IOptions<TelegratorOptions> options,
IUpdateHandlersPool handlersPool,
ITelegramBotInfo botInfo,
ILogger<HostUpdateRouter> logger) : base(handlersProvider, awaitingProvider, options.Value, handlersPool, botInfo)
ILogger<HostUpdateRouter> logger) : base(handlersProvider, awaitingProvider, options.Value, botInfo)
{
Logger = logger;
ExceptionHandler = new DefaultRouterExceptionHandler(HandleException);
+35 -5
View File
@@ -15,7 +15,7 @@
---
## Requirements
- .NET 8.0 or later
- .NET 10.0 or later
- [Telegrator](https://github.com/Rikitav/Telegrator)
---
@@ -35,10 +35,10 @@ dotnet add package Telegrator.Hosting
using Telegrator.Hosting;
// Creating builder
TelegramBotHostBuilder builder = TelegramBotHost.CreateBuilder(new TelegramBotHostBuilderSettings()
TelegramBotHostBuilder builder = TelegramBotHost.CreateBuilder(new HostApplicationBuilderSettings()
{
Args = args,
ExceptIntersectingCommandAliases = true
ApplicationName = "TelegramBotHost example",
});
// Registerring handlers
@@ -55,12 +55,42 @@ telegramBot.Run();
---
## Application integration Example
**Program.cs:**
```csharp
using Telegrator.Hosting;
// Creating builder
HostApplicationBuilder builder = Host.CreateApplicationBuilder(new HostApplicationBuilderSettings()
{
Args = args,
ApplicationName = "Host example",
});
// Adding Telegrator
builder.AddTelegrator();
// Registerring handlers (extension)
builder.Handlers.CollectHandlersAssemblyWide();
// Building and running application
builder.Build()
.UseTelegrator()
.SetBotCommands()
.Run();
```
---
## Configuration (appsettings.json)
```json
{
"TelegramBotClientOptions": {
"Token": "YOUR_BOT_TOKEN"
"TelegratorOptions": {
"Token": "YOUR_BOT_TOKEN",
"ExceptIntersectingCommandAliases": true
},
"HostOptions": {
@@ -15,7 +15,7 @@
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<Title>Telegrator.Hosting</Title>
<Version>1.16.0</Version>
<Version>1.16.3</Version>
<Authors>Rikitav Tim4ik</Authors>
<Company>Rikitav Tim4ik</Company>
<RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl>
@@ -26,19 +26,19 @@
<PackageLicenseFile>LICENSE</PackageLicenseFile>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Telegrator\Telegrator.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.3" />
<PackageReference Include="Microsoft.Extensions.Http" Version="10.0.3" />
</ItemGroup>
<ItemGroup>
<None Include=".\README.md" Pack="True" PackagePath="\" />
<None Include="..\..\LICENSE" Pack="True" PackagePath="\" />
<None Include="..\..\README.md" Pack="True" PackagePath="\" />
<None Include="..\..\resources\telegrator_nuget.png" Pack="True" PackagePath="\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Telegrator\Telegrator.csproj" />
</ItemGroup>
</Project>
+1 -5
View File
@@ -48,11 +48,8 @@ public static class HostBuilderExtensions
/// <summary>
/// Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers.
/// </summary>
public static IHostApplicationBuilder AddTelegrator(this IHostApplicationBuilder builder, HostApplicationBuilderSettings settings, TelegratorOptions? options = null, IHandlersCollection? handlers = null)
public static IHostApplicationBuilder AddTelegrator(this IHostApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null)
{
if (settings is null)
throw new ArgumentNullException(nameof(settings));
IServiceCollection services = builder.Services;
IConfigurationManager configuration = builder.Configuration;
@@ -123,7 +120,6 @@ public static class ServicesCollectionExtensions
public static IServiceCollection AddTelegramBotHostDefaults(this IServiceCollection services)
{
services.AddLogging(builder => builder.AddConsole().AddDebug());
services.AddSingleton<IUpdateHandlersPool, HostUpdateHandlersPool>();
services.AddSingleton<IAwaitingProvider, HostAwaitingProvider>();
services.AddSingleton<IHandlersProvider, HostHandlersProvider>();
services.AddSingleton<IUpdateRouter, HostUpdateRouter>();
@@ -1,6 +1,5 @@
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegrator.Core;
using Telegrator.Core.Filters;
using Telegrator.Core.Handlers;
@@ -115,7 +114,7 @@ namespace Telegrator.Core.Descriptors
/// <param name="result">The execution result.</param>
public void ReportResult(Result? result)
{
if (result != null)
if (Result != null)
throw new InvalidOperationException("Result already reported");
Result = result;
+27 -18
View File
@@ -58,23 +58,6 @@ namespace Telegrator.Mediation
_botInfo = botInfo;
}
/// <summary>
/// Initializes a new instance of the <see cref="UpdateRouter"/> class with a custom handlers pool.
/// </summary>
/// <param name="handlersProvider">The provider for regular handlers.</param>
/// <param name="awaitingProvider">The provider for awaiting handlers.</param>
/// <param name="options">The bot configuration options.</param>
/// <param name="handlersPool">The custom handlers pool to use.</param>
/// <param name="botInfo"></param>
public UpdateRouter(IHandlersProvider handlersProvider, IAwaitingProvider awaitingProvider, TelegratorOptions options, IUpdateHandlersPool handlersPool, ITelegramBotInfo botInfo)
{
_options = options;
_handlersProvider = handlersProvider;
_awaitingProvider = awaitingProvider;
_HandlersPool = handlersPool;
_botInfo = botInfo;
}
/// <summary>
/// Handles errors that occur during update processing.
/// </summary>
@@ -98,6 +81,11 @@ namespace Telegrator.Mediation
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A task representing the asynchronous update handling operation.</returns>
public virtual async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
{
_ = HandleUpdateAsyncInternal(botClient, update, cancellationToken);
}
private async Task HandleUpdateAsyncInternal(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
{
// Logging
LogUpdate(update);
@@ -135,7 +123,28 @@ namespace Telegrator.Mediation
}
// Queuing reagular handlers for execution
await HandlersPool.Enqueue(GetHandlers(HandlersProvider, botClient, update, cancellationToken));
foreach (DescribedHandlerDescriptor handlerInfo in GetHandlers(HandlersProvider, botClient, update, cancellationToken))
{
if (lastResult?.NextType != null)
{
if (lastResult.NextType != handlerInfo.From.HandlerType)
continue;
}
// Enqueuing found handlers
await HandlersPool.Enqueue(handlerInfo);
await handlerInfo.AwaitResult(cancellationToken).ConfigureAwait(false);
lastResult = handlerInfo.Result;
if (lastResult == null)
break; // Smth went horribly wrong, better to stop routing
if (lastResult != null && !lastResult.RouteNext)
break;
TelegratorLogging.LogTrace("Handler '{0}' requested route continuation (Update {1})", handlerInfo.DisplayString, handlerInfo.HandlingUpdate.Id);
}
TelegratorLogging.LogTrace("Receiving Update ({0}) finished", update.Id);
}
catch (OperationCanceledException)
+1 -1
View File
@@ -14,7 +14,7 @@
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<Title>Telegrator : Telegram.Bot mediator framework</Title>
<Version>1.16.0</Version>
<Version>1.16.3</Version>
<Authors>Rikitav Tim4ik</Authors>
<Company>Rikitav Tim4ik</Company>
<RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl>