* Added StartReceiving method as ITelegratoBot interface member

* Added missing summaries
* Fixed compiler warning and infos
* Code cleanup and bugfixes
This commit is contained in:
gutii
2026-04-27 22:13:47 +04:00
parent 5433a2de0d
commit 96b3241716
21 changed files with 379 additions and 68 deletions
+9 -1
View File
@@ -35,7 +35,7 @@
</member>
<member name="T:Telegrator.Mediation.HostedUpdateWebhooker">
<summary>
Service for receiving updates for Hosted telegram bots via Webhooks
Service for receiving updates for Hosted telegram bots via Webhooks and queuing them to router
</summary>
</member>
<member name="M:Telegrator.Mediation.HostedUpdateWebhooker.#ctor(Telegram.Bot.ITelegramBotClient,Telegrator.Core.IUpdateRouter,Microsoft.Extensions.Options.IOptions{Telegrator.Hosting.Web.WebhookerOptions})">
@@ -95,6 +95,14 @@
Provides method to configure Telegram Bot WebHost
</summary>
</member>
<member name="M:Telegrator.WebServicesCollectionExtensions.ConfigureWebhooker(Microsoft.Extensions.DependencyInjection.IServiceCollection,Telegrator.Hosting.Web.WebhookerOptions)">
<summary>
Adds WebhookerOptions to services
</summary>
<param name="services"></param>
<param name="options"></param>
<returns></returns>
</member>
<member name="M:Telegrator.WebServicesCollectionExtensions.TryFindWebhooker(System.IServiceProvider,Telegrator.Mediation.HostedUpdateWebhooker@)">
<summary>
Searchs for <see cref="T:Telegrator.Mediation.HostedUpdateWebhooker"/> hosted service inside hosts services
+126
View File
@@ -4,6 +4,116 @@
<name>Telegrator.Hosting.WideBot</name>
</assembly>
<members>
<member name="T:Telegrator.Mediation.HostedWideBotUpdateReceiver">
<summary>
Service for receiving updates for Hosted wide telegram bots and queuing them to router
</summary>
<param name="logger"></param>
<param name="botClient"></param>
<param name="updateRouter"></param>
<param name="options"></param>
</member>
<member name="M:Telegrator.Mediation.HostedWideBotUpdateReceiver.#ctor(Microsoft.Extensions.Logging.ILogger{Telegrator.Mediation.HostedWideBotUpdateReceiver},Telegram.Bot.ITelegramBotClient,Telegrator.Core.IUpdateRouter,Microsoft.Extensions.Options.IOptions{Telegram.Bot.Polling.ReceiverOptions})">
<summary>
Service for receiving updates for Hosted wide telegram bots and queuing them to router
</summary>
<param name="logger"></param>
<param name="botClient"></param>
<param name="updateRouter"></param>
<param name="options"></param>
</member>
<member name="M:Telegrator.Mediation.HostedWideBotUpdateReceiver.ExecuteAsync(System.Threading.CancellationToken)">
<inheritdoc/>
</member>
<member name="T:Telegrator.Mediation.WideUpdateReceiver">
<summary>
Reactive implementation of <see cref="T:Telegrator.Core.IUpdateReceiver"/> for polling updates from Telegram.
Provides custom update receiving logic with error handling and configuration options.
</summary>
<param name="client">The Telegram bot client for making API requests.</param>
</member>
<member name="M:Telegrator.Mediation.WideUpdateReceiver.#ctor(Telegram.Bot.WTelegramBotClient)">
<summary>
Reactive implementation of <see cref="T:Telegrator.Core.IUpdateReceiver"/> for polling updates from Telegram.
Provides custom update receiving logic with error handling and configuration options.
</summary>
<param name="client">The Telegram bot client for making API requests.</param>
</member>
<member name="M:Telegrator.Mediation.WideUpdateReceiver.ReceiveAsync(Telegram.Bot.Polling.IUpdateHandler,System.Threading.CancellationToken)">
<inheritdoc/>
</member>
<member name="T:Telegrator.TelegratorWClient">
<summary>
Client class for the Telegrator library with Wider functionality, provided by WTelegramBotClient.
Extends TelegramBotClient with reactive capabilities for handling updates.
</summary>
</member>
<member name="P:Telegrator.TelegratorWClient.Options">
<inheritdoc/>
</member>
<member name="P:Telegrator.TelegratorWClient.Handlers">
<inheritdoc/>
</member>
<member name="P:Telegrator.TelegratorWClient.BotInfo">
<inheritdoc/>
</member>
<member name="P:Telegrator.TelegratorWClient.UpdateRouter">
<inheritdoc/>
</member>
<member name="M:Telegrator.TelegratorWClient.#ctor(Telegram.Bot.WTelegramBotClientOptions,Telegrator.TelegratorOptions,System.Net.Http.HttpClient,System.Threading.CancellationToken)">
<summary>
Initializes new instance of <see cref="T:Telegrator.TelegratorWClient"/>
</summary>
<param name="wOptions"></param>
<param name="telegratorOptions"></param>
<param name="httpClient"></param>
<param name="cancellationToken"></param>
</member>
<member name="M:Telegrator.TelegratorWClient.StartReceiving(Telegram.Bot.Polling.ReceiverOptions,System.Threading.CancellationToken)">
<inheritdoc/>
</member>
<member name="M:Telegrator.TelegratorWClient.StartReceivingAsync(Telegram.Bot.Polling.ReceiverOptions,System.Threading.CancellationToken)">
<inheritdoc/>
</member>
<member name="T:Telegrator.HandlersExtensions">
<summary>
Provides extensions memebrs for <see cref="T:Telegrator.Core.Handlers.UpdateHandlerBase"/> for easy access to Wider bot functions and update
</summary>
</member>
<member name="M:Telegrator.HandlersExtensions.get_WClient``1(Telegrator.Core.Handlers.AbstractUpdateHandler{``0})">
<inheritdoc cref="P:Telegrator.HandlersExtensions.&lt;G&gt;$F89AF16C011F73161937A1614DCDDD7A`1.WClient"/>
</member>
<member name="M:Telegrator.HandlersExtensions.get_WideUpdate``1(Telegrator.Core.Handlers.AbstractUpdateHandler{``0})">
<inheritdoc cref="P:Telegrator.HandlersExtensions.&lt;G&gt;$F89AF16C011F73161937A1614DCDDD7A`1.WideUpdate"/>
</member>
<member name="M:Telegrator.HandlersExtensions.get_TLUpdate``1(Telegrator.Core.Handlers.AbstractUpdateHandler{``0})">
<inheritdoc cref="P:Telegrator.HandlersExtensions.&lt;G&gt;$F89AF16C011F73161937A1614DCDDD7A`1.TLUpdate"/>
</member>
<member name="M:Telegrator.HandlersExtensions.AsWClient(Telegram.Bot.ITelegramBotClient)">
<summary>
Casts Update to <see cref="T:Telegram.Bot.WTelegramBotClient"/>
</summary>
</member>
<member name="M:Telegrator.HandlersExtensions.AsWUpdate(Telegram.Bot.Types.Update)">
<summary>
Casts Update to <see cref="T:WTelegram.Types.Update"/>
</summary>
</member>
<member name="P:Telegrator.HandlersExtensions.&lt;G&gt;$F89AF16C011F73161937A1614DCDDD7A`1.WClient">
<summary>
Casts Update to <see cref="T:Telegram.Bot.WTelegramBotClient"/>
</summary>
</member>
<member name="P:Telegrator.HandlersExtensions.&lt;G&gt;$F89AF16C011F73161937A1614DCDDD7A`1.WideUpdate">
<summary>
Casts Update to <see cref="T:WTelegram.Types.Update"/>
</summary>
</member>
<member name="P:Telegrator.HandlersExtensions.&lt;G&gt;$F89AF16C011F73161937A1614DCDDD7A`1.TLUpdate">
<summary>
Casts Update to <see cref="T:TL.Update"/>
</summary>
</member>
<member name="T:Telegrator.WideHostBuilderExtensions">
<summary>
Provides extension methods for <see cref="T:Microsoft.Extensions.Hosting.IHostApplicationBuilder"/> to configure Telegrator.
@@ -30,6 +140,22 @@
Provides method to configure Telegram Bot WebHost
</summary>
</member>
<member name="M:Telegrator.WideBotServiceCollectionExtensions.ConfigureWideTelegram(Microsoft.Extensions.DependencyInjection.IServiceCollection,Telegram.Bot.WTelegramBotClientOptions)">
<summary>
Adds WTelegramBotClientOptions to services
</summary>
<param name="services"></param>
<param name="options"></param>
<returns></returns>
</member>
<member name="M:Telegrator.WideBotServiceCollectionExtensions.AddMTProtoUpdateReceiver(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Boolean)">
<summary>
Adds WTelegramBotClient
</summary>
<param name="services"></param>
<param name="useHttp"></param>
<returns></returns>
</member>
<member name="T:Telegrator.WideTelegramBotHostExtensions">
<summary>
Provides useful methods to adjust Telegram bot Host
+18 -2
View File
@@ -97,7 +97,7 @@
</member>
<member name="T:Telegrator.Mediation.HostedUpdateReceiver">
<summary>
Service for receiving updates for Hosted telegram bots
Service for receiving updates for Hosted telegram botsand queuing them to router
</summary>
<param name="botClient"></param>
<param name="updateRouter"></param>
@@ -106,7 +106,7 @@
</member>
<member name="M:Telegrator.Mediation.HostedUpdateReceiver.#ctor(Telegram.Bot.ITelegramBotClient,Telegrator.Core.IUpdateRouter,Microsoft.Extensions.Options.IOptions{Telegram.Bot.Polling.ReceiverOptions},Microsoft.Extensions.Logging.ILogger{Telegrator.Mediation.HostedUpdateReceiver})">
<summary>
Service for receiving updates for Hosted telegram bots
Service for receiving updates for Hosted telegram botsand queuing them to router
</summary>
<param name="botClient"></param>
<param name="updateRouter"></param>
@@ -207,6 +207,22 @@
Provides method to configure Telegram Bot Host
</summary>
</member>
<member name="M:Telegrator.HostServicesCollectionExtensions.ConfigureTelegram(Microsoft.Extensions.DependencyInjection.IServiceCollection,Telegram.Bot.TelegramBotClientOptions)">
<summary>
Adds TelegramBotClientOptions to services
</summary>
<param name="services"></param>
<param name="options"></param>
<returns></returns>
</member>
<member name="M:Telegrator.HostServicesCollectionExtensions.ConfigureReceiver(Microsoft.Extensions.DependencyInjection.IServiceCollection,Telegram.Bot.Polling.ReceiverOptions)">
<summary>
Adds ReceiverOptions to services
</summary>
<param name="services"></param>
<param name="options"></param>
<returns></returns>
</member>
<member name="M:Telegrator.HostServicesCollectionExtensions.AddStateStorage``1(Microsoft.Extensions.DependencyInjection.IServiceCollection)">
<summary>
Registers <see cref="T:Telegrator.Core.States.IStateStorage"/> service
+23 -7
View File
@@ -5483,6 +5483,14 @@
Gets the update router for handling incoming updates.
</summary>
</member>
<member name="M:Telegrator.ITelegratorBot.StartReceivingAsync(Telegram.Bot.Polling.ReceiverOptions,System.Threading.CancellationToken)">
<summary>
Initializes the update router and begins polling for updates asynchronously.
</summary>
<param name="receiverOptions">Optional receiver options for configuring update polling.</param>
<param name="cancellationToken">The cancellation token to stop receiving updates.</param>
<returns></returns>
</member>
<member name="T:Telegrator.Logging.ConsoleLogger">
<summary>
Console logger implementation that writes to System.Console.
@@ -6804,13 +6812,8 @@
<param name="httpClient">Optional HTTP client for making requests.</param>
<param name="cancellationToken">The cancellation token.</param>
</member>
<member name="M:Telegrator.TelegratorClient.StartReceiving(Telegram.Bot.Polling.ReceiverOptions,System.Threading.CancellationToken)">
<summary>
Starts receiving updates from Telegram.
Initializes the update router and begins polling for updates.
</summary>
<param name="receiverOptions">Optional receiver options for configuring update polling.</param>
<param name="globalCancellationToken">The cancellation token to stop receiving updates.</param>
<member name="M:Telegrator.TelegratorClient.StartReceivingAsync(Telegram.Bot.Polling.ReceiverOptions,System.Threading.CancellationToken)">
<inheritdoc/>
</member>
<member name="M:Telegrator.TelegratorClient.StartReceivingInternal(Telegram.Bot.Polling.ReceiverOptions,System.Threading.CancellationToken)">
<summary>
@@ -6871,6 +6874,19 @@
Gets or sets the global cancellation token for all bot operations.
</summary>
</member>
<member name="T:Telegrator.TelegratorBotExtensions">
<summary>
Provides usefull helper methods for TelegratorBot
</summary>
</member>
<member name="M:Telegrator.TelegratorBotExtensions.StartReceiving(Telegrator.ITelegratorBot,Telegram.Bot.Polling.ReceiverOptions,System.Threading.CancellationToken)">
<summary>
Initializes the update router and begins polling for updates synchronously (Blocking calling thread).
</summary>
<param name="bot"></param>
<param name="receiverOptions">Optional receiver options for configuring update polling.</param>
<param name="cancellationToken">The cancellation token to stop receiving updates.</param>
</member>
<member name="T:Telegrator.MessageExtensions">
<summary>
Provides usefull helper methods for messages
@@ -13,7 +13,7 @@ using Telegrator.Hosting.Web;
namespace Telegrator.Mediation;
/// <summary>
/// Service for receiving updates for Hosted telegram bots via Webhooks
/// Service for receiving updates for Hosted telegram bots via Webhooks and queuing them to router
/// </summary>
public class HostedUpdateWebhooker : IHostedService
{
@@ -99,8 +99,15 @@ public static class WebHostBuilderExtensions
/// </summary>
public static class WebServicesCollectionExtensions
{
/// <summary>
/// Adds WebhookerOptions to services
/// </summary>
/// <param name="services"></param>
/// <param name="options"></param>
/// <returns></returns>
public static IServiceCollection ConfigureWebhooker(this IServiceCollection services, WebhookerOptions options)
{
services.RemoveAll<IOptions<WebhookerOptions>>();
services.AddSingleton(Options.Create(options));
return services;
}
@@ -176,6 +183,9 @@ public static class WebTelegramBotHostExtensions
/// <returns></returns>
public static IServiceCollection AddTelegramWebhook(this IServiceCollection services)
{
services.RemoveAll<IOptions<HostedUpdateWebhooker>>();
services.RemoveAll<ITelegramBotClient>();
services.AddHttpClient<ITelegramBotClient>("tgwebhook").RemoveAllLoggers().AddTypedClient(TypedTelegramBotClientFactory);
services.AddHostedService<HostedUpdateWebhooker>();
return services;
@@ -6,3 +6,5 @@
using System.Diagnostics.CodeAnalysis;
[assembly: SuppressMessage("Roslynator", "RCS1037")]
[assembly: SuppressMessage("Style", "IDE0090")]
[assembly: SuppressMessage("Style", "IDE0270")]
@@ -1,18 +1,22 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Threading;
using System.Threading.Tasks;
using Telegram.Bot;
using Telegram.Bot.Polling;
using Telegrator.Core;
namespace Telegrator.Mediation;
//Hosting.WideBot
/// <summary>
/// Service for receiving updates for Hosted wide telegram bots and queuing them to router
/// </summary>
/// <param name="logger"></param>
/// <param name="botClient"></param>
/// <param name="updateRouter"></param>
/// <param name="options"></param>
public class HostedWideBotUpdateReceiver(ILogger<HostedWideBotUpdateReceiver> logger, ITelegramBotClient botClient, IUpdateRouter updateRouter, IOptions<ReceiverOptions>? options) : BackgroundService
{
/// <inheritdoc/>
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
if (botClient is not WTelegramBotClient wideBotClient)
@@ -8,12 +8,19 @@ using WUpdate = WTelegram.Types.Update;
namespace Telegrator.Mediation;
/// <summary>
/// Reactive implementation of <see cref="IUpdateReceiver"/> for polling updates from Telegram.
/// Provides custom update receiving logic with error handling and configuration options.
/// </summary>
/// <param name="client">The Telegram bot client for making API requests.</param>
// <param name="options">Optional receiver options for configuring update polling behavior.</param>
public class WideUpdateReceiver(WTelegramBotClient client) : IUpdateReceiver
{
private readonly WTelegramBotClient _client = client;
private IUpdateHandler? _updateHandler = null;
private CancellationToken _cancellation = default;
/// <inheritdoc/>
public async Task ReceiveAsync(IUpdateHandler updateHandler, CancellationToken cancellationToken = default)
{
_updateHandler = updateHandler;
@@ -13,18 +13,33 @@ using Telegrator.States;
namespace Telegrator;
/// <summary>
/// Client class for the Telegrator library with Wider functionality, provided by WTelegramBotClient.
/// Extends TelegramBotClient with reactive capabilities for handling updates.
/// </summary>
public class TelegratorWClient : WTelegramBotClient, ITelegratorBot, ICollectingProvider
{
private IUpdateRouter? _updateRouter = null;
/// <inheritdoc/>
public TelegratorOptions Options { get; }
/// <inheritdoc/>
public IHandlersCollection Handlers { get; }
/// <inheritdoc/>
public ITelegramBotInfo BotInfo { get; }
/// <inheritdoc/>
public IUpdateRouter UpdateRouter => _updateRouter ?? throw new InvalidOperationException("Router's not created yet. Invoke `StartReceiving` to initialize this property.");
/// <summary>
/// Initializes new instance of <see cref="TelegratorWClient"/>
/// </summary>
/// <param name="wOptions"></param>
/// <param name="telegratorOptions"></param>
/// <param name="httpClient"></param>
/// <param name="cancellationToken"></param>
public TelegratorWClient(WTelegramBotClientOptions wOptions, TelegratorOptions? telegratorOptions = null, HttpClient? httpClient = null, CancellationToken cancellationToken = default)
: base(wOptions, httpClient, cancellationToken)
{
@@ -33,7 +48,8 @@ public class TelegratorWClient : WTelegramBotClient, ITelegratorBot, ICollecting
BotInfo = new TelegramBotInfo(GetMe(cancellationToken).Result);
}
public void StartReceiving(CancellationToken cancellationToken = default)
/// <inheritdoc/>
public void StartReceiving(ReceiverOptions? _, CancellationToken cancellationToken = default)
{
if (Options.GlobalCancellationToken == CancellationToken.None)
Options.GlobalCancellationToken = cancellationToken;
@@ -45,10 +61,27 @@ public class TelegratorWClient : WTelegramBotClient, ITelegratorBot, ICollecting
_updateRouter = new UpdateRouter(handlerProvider, awaitingProvider, stateStorage, Options, BotInfo);
TelegratorLogging.LogInformation($"TelegratorW bot starting up - BotId: {BotInfo.User.Id}, Username: {BotInfo.User.Username}");
StartReceivingInternal(Options.GlobalCancellationToken);
StartReceivingInternal(Options.GlobalCancellationToken)
.ConfigureAwait(false).GetAwaiter().GetResult();
}
private async void StartReceivingInternal(CancellationToken cancellationToken)
/// <inheritdoc/>
public async Task StartReceivingAsync(ReceiverOptions? receiverOptions = null,CancellationToken cancellationToken = default)
{
if (Options.GlobalCancellationToken == CancellationToken.None)
Options.GlobalCancellationToken = cancellationToken;
HandlersProvider handlerProvider = new HandlersProvider(Handlers, Options);
AwaitingProvider awaitingProvider = new AwaitingProvider(Options);
DefaultStateStorage stateStorage = new DefaultStateStorage();
_updateRouter = new UpdateRouter(handlerProvider, awaitingProvider, stateStorage, Options, BotInfo);
TelegratorLogging.LogInformation($"TelegratorW bot starting up - BotId: {BotInfo.User.Id}, Username: {BotInfo.User.Username}");
await StartReceivingInternal(Options.GlobalCancellationToken);
}
private async Task StartReceivingInternal(CancellationToken cancellationToken)
{
try
{
@@ -13,15 +13,21 @@ using Telegrator.Hosting;
using Telegrator.Mediation;
using Telegrator.Providers;
using TLUpdate = TL.Update;
using WUpdate = WTelegram.Types.Update;
using TLUpdate = TL.Update;
namespace Telegrator;
/// <summary>
/// Provides extensions memebrs for <see cref="UpdateHandlerBase"/> for easy access to Wider bot functions and update
/// </summary>
public static class HandlersExtensions
{
extension<TUpdate>(AbstractUpdateHandler<TUpdate> handler) where TUpdate : class
{
/// <summary>
/// Casts Update to <see cref="WTelegramBotClient"/>
/// </summary>
public WTelegramBotClient WClient
{
get
@@ -35,6 +41,9 @@ public static class HandlersExtensions
}
/// <summary>
/// Casts Update to <see cref="WUpdate"/>
/// </summary>
public WUpdate WideUpdate
{
get
@@ -47,18 +56,27 @@ public static class HandlersExtensions
}
}
/// <summary>
/// Casts Update to <see cref="TLUpdate"/>
/// </summary>
public TLUpdate? TLUpdate
{
get => handler.WideUpdate.TLUpdate;
}
}
/// <summary>
/// Casts Update to <see cref="WTelegramBotClient"/>
/// </summary>
public static WTelegramBotClient AsWClient(this ITelegramBotClient client)
{
return client as WTelegramBotClient
?? throw new InvalidCastException("Client is not assignable to `WTelegram.Bot.WTelegramBotClient`");
}
/// <summary>
/// Casts Update to <see cref="WUpdate"/>
/// </summary>
public static WUpdate AsWUpdate(this Update update)
{
return update as WUpdate
@@ -149,20 +167,45 @@ public static class WideHostBuilderExtensions
/// </summary>
public static class WideBotServiceCollectionExtensions
{
/// <summary>
/// Adds WTelegramBotClientOptions to services
/// </summary>
/// <param name="services"></param>
/// <param name="options"></param>
/// <returns></returns>
public static IServiceCollection ConfigureWideTelegram(this IServiceCollection services, WTelegramBotClientOptions options)
{
services.RemoveAll<IOptions<WTelegramBotClientOptions>>();
services.AddSingleton(Options.Create(options));
return services;
}
public static IServiceCollection AddMTProtoUpdateReceiver(this IServiceCollection services)
/// <summary>
/// Adds WTelegramBotClient
/// </summary>
/// <param name="services"></param>
/// <param name="useHttp"></param>
/// <returns></returns>
public static IServiceCollection AddMTProtoUpdateReceiver(this IServiceCollection services, bool useHttp = false)
{
services.RemoveAll<ITelegramBotClient>();
services.RemoveAll<HostedWideBotUpdateReceiver>();
if (useHttp)
{
services.AddHttpClient<WTelegramBotClient>("tgmtproto").RemoveAllLoggers().AddTypedClient(TypedTelegramBotClientFactory);
}
else
{
services.AddSingleton(TypedTelegramBotClientFactory);
}
services.AddSingleton<ITelegramBotClient>(sp => sp.GetRequiredService<WTelegramBotClient>());
services.AddHostedService<HostedWideBotUpdateReceiver>();
return services;
}
#pragma warning disable CA2254
private static WTelegramBotClient TypedTelegramBotClientFactory(HttpClient httpClient, IServiceProvider provider)
{
ILogger<WTelegramBotClient> logger = provider.GetRequiredService<ILogger<WTelegramBotClient>>();
@@ -171,6 +214,16 @@ public static class WideBotServiceCollectionExtensions
WTelegram.Helpers.Log = (lvl, str) => logger.Log((LogLevel)lvl, str);
return client;
}
private static WTelegramBotClient TypedTelegramBotClientFactory(IServiceProvider provider)
{
ILogger<WTelegramBotClient> logger = provider.GetRequiredService<ILogger<WTelegramBotClient>>();
WTelegramBotClient client = new WTelegramBotClient(provider.GetRequiredService<IOptions<WTelegramBotClientOptions>>().Value);
WTelegram.Helpers.Log = (lvl, str) => logger.Log((LogLevel)lvl, str);
return client;
}
#pragma warning restore CA2254
}
/// <summary>
@@ -1,9 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Telegrator;
public class WideReceiverOptions
{
}
@@ -8,7 +8,7 @@ using Telegrator.Core;
namespace Telegrator.Mediation;
/// <summary>
/// Service for receiving updates for Hosted telegram bots
/// Service for receiving updates for Hosted telegram botsand queuing them to router
/// </summary>
/// <param name="botClient"></param>
/// <param name="updateRouter"></param>
+17
View File
@@ -129,14 +129,28 @@ public static class HostBuilderExtensions
/// </summary>
public static class HostServicesCollectionExtensions
{
/// <summary>
/// Adds TelegramBotClientOptions to services
/// </summary>
/// <param name="services"></param>
/// <param name="options"></param>
/// <returns></returns>
public static IServiceCollection ConfigureTelegram(this IServiceCollection services, TelegramBotClientOptions options)
{
services.RemoveAll<IOptions<TelegramBotClientOptions>>();
services.AddSingleton(Options.Create(options));
return services;
}
/// <summary>
/// Adds ReceiverOptions to services
/// </summary>
/// <param name="services"></param>
/// <param name="options"></param>
/// <returns></returns>
public static IServiceCollection ConfigureReceiver(this IServiceCollection services, ReceiverOptions options)
{
services.RemoveAll<IOptions<ReceiverOptions>>();
services.AddSingleton(Options.Create(options));
return services;
}
@@ -176,6 +190,9 @@ public static class HostServicesCollectionExtensions
/// <returns></returns>
public static IServiceCollection AddTelegramReceiver(this IServiceCollection services)
{
services.RemoveAll<ITelegramBotClient>();
services.RemoveAll<HostedUpdateReceiver>();
services.AddHttpClient<ITelegramBotClient>("tgreceiver").RemoveAllLoggers().AddTypedClient(TypedTelegramBotClientFactory);
services.AddHostedService<HostedUpdateReceiver>();
return services;
+1
View File
@@ -10,3 +10,4 @@ using System.Diagnostics.CodeAnalysis;
[assembly: SuppressMessage("Style", "IDE0057")]
[assembly: SuppressMessage("Style", "IDE0270")]
[assembly: SuppressMessage("Roslynator", "RCS1037")]
[assembly: SuppressMessage("Roslynator", "RCS1227")]
+10 -1
View File
@@ -1,4 +1,5 @@
using Telegrator.Core;
using Telegram.Bot.Polling;
using Telegrator.Core;
namespace Telegrator;
@@ -12,4 +13,12 @@ public interface ITelegratorBot
/// Gets the update router for handling incoming updates.
/// </summary>
public IUpdateRouter UpdateRouter { get; }
/// <summary>
/// Initializes the update router and begins polling for updates asynchronously.
/// </summary>
/// <param name="receiverOptions">Optional receiver options for configuring update polling.</param>
/// <param name="cancellationToken">The cancellation token to stop receiving updates.</param>
/// <returns></returns>
Task StartReceivingAsync(ReceiverOptions? receiverOptions = null, CancellationToken cancellationToken = default);
}
+9 -16
View File
@@ -20,13 +20,13 @@ public class TelegratorClient : TelegramBotClient, ITelegratorBot, ICollectingPr
private IUpdateRouter? updateRouter = null;
/// <inheritdoc/>
public TelegratorOptions Options { get; private set; }
public TelegratorOptions Options { get; }
/// <inheritdoc/>
public IHandlersCollection Handlers { get; private set; }
public IHandlersCollection Handlers { get; }
/// <inheritdoc/>
public ITelegramBotInfo BotInfo { get; private set; }
public ITelegramBotInfo BotInfo { get; }
/// <inheritdoc/>
public IUpdateRouter UpdateRouter => updateRouter ?? throw new InvalidOperationException("Router's not created yet. Invoke `StartReceiving` to initialize this property.");
@@ -63,26 +63,19 @@ public class TelegratorClient : TelegramBotClient, ITelegratorBot, ICollectingPr
BotInfo = new TelegramBotInfo(this.GetMe(cancellationToken).Result);
}
/// <summary>
/// Starts receiving updates from Telegram.
/// Initializes the update router and begins polling for updates.
/// </summary>
/// <param name="receiverOptions">Optional receiver options for configuring update polling.</param>
/// <param name="globalCancellationToken">The cancellation token to stop receiving updates.</param>
public void StartReceiving(ReceiverOptions? receiverOptions = null, CancellationToken globalCancellationToken = default)
/// <inheritdoc/>
public async Task StartReceivingAsync(ReceiverOptions? receiverOptions = null, CancellationToken cancellationToken = default)
{
if (Options.GlobalCancellationToken == CancellationToken.None)
Options.GlobalCancellationToken = globalCancellationToken;
Options.GlobalCancellationToken = cancellationToken;
HandlersProvider handlerProvider = new HandlersProvider(Handlers, Options);
AwaitingProvider awaitingProvider = new AwaitingProvider(Options);
DefaultStateStorage stateStorage = new DefaultStateStorage();
updateRouter = new UpdateRouter(handlerProvider, awaitingProvider, stateStorage, Options, BotInfo);
// Log startup
TelegratorLogging.LogInformation($"Telegrator bot starting up - BotId: {BotInfo.User.Id}, Username: {BotInfo.User.Username}, MaxParallelHandlers: {Options.MaximumParallelWorkingHandlers ?? -1}");
StartReceivingInternal(receiverOptions, globalCancellationToken);
updateRouter = new UpdateRouter(handlerProvider, awaitingProvider, stateStorage, Options, BotInfo);
await StartReceivingInternal(receiverOptions, Options.GlobalCancellationToken);
}
/// <summary>
@@ -91,7 +84,7 @@ public class TelegratorClient : TelegramBotClient, ITelegratorBot, ICollectingPr
/// </summary>
/// <param name="receiverOptions">Optional receiver options for configuring update polling.</param>
/// <param name="cancellationToken">The cancellation token to stop receiving updates.</param>
private async void StartReceivingInternal(ReceiverOptions? receiverOptions, CancellationToken cancellationToken)
private async Task StartReceivingInternal(ReceiverOptions? receiverOptions, CancellationToken cancellationToken)
{
try
{
+19
View File
@@ -1,4 +1,5 @@
using System.Reflection;
using Telegram.Bot.Polling;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.Payments;
@@ -13,6 +14,24 @@ using Telegrator.States;
namespace Telegrator;
/// <summary>
/// Provides usefull helper methods for TelegratorBot
/// </summary>
public static class TelegratorBotExtensions
{
/// <summary>
/// Initializes the update router and begins polling for updates synchronously (Blocking calling thread).
/// </summary>
/// <param name="bot"></param>
/// <param name="receiverOptions">Optional receiver options for configuring update polling.</param>
/// <param name="cancellationToken">The cancellation token to stop receiving updates.</param>
public static void StartReceiving(this ITelegratorBot bot, ReceiverOptions? receiverOptions = null, CancellationToken cancellationToken = default)
{
bot.StartReceivingAsync(receiverOptions, cancellationToken)
.ConfigureAwait(false).GetAwaiter().GetResult();
}
}
/// <summary>
/// Provides usefull helper methods for messages
/// </summary>
@@ -0,0 +1,8 @@
// This file is used by Code Analysis to maintain SuppressMessage
// attributes that are applied to this project.
// Project-level suppressions either have no target or are given
// a specific target and scoped to a namespace, type, member, etc.
using System.Diagnostics.CodeAnalysis;
[assembly: SuppressMessage("Roslynator", "RCS1037")]
+1 -1
View File
@@ -32,7 +32,7 @@ internal static class Program
ApplicationName = "WBot example",
});
using DbConnection connection = new SqliteConnection(@"Data Source=wtgb.db");
using DbConnection connection = new SqliteConnection("Data Source=wtgb.db");
builder.Services.ConfigureWideTelegram(
new WTelegramBotClientOptions(token: "BOT_TOKEN", apiId: 123, apiHash: "API_HASH", dbConnection: connection));
+2 -4
View File
@@ -1,11 +1,10 @@
using Telegram.Bot.Types;
using Telegrator.Handlers;
namespace Telegrator.Tests
{
namespace Telegrator.Tests;
/// <summary>
/// Вспомогательный класс для тестирования абстрактного UpdateHandlerBase.
///
/// ПРИНЦИП: Создание тестовых двойников для абстрактных классов
/// </summary>
[MessageHandler]
@@ -20,4 +19,3 @@ namespace Telegrator.Tests
return Task.FromResult(Result.Ok());
}
}
}