5320c9ec20
* Fixed wrong LeveldDebug method calls after moving logic from providers to router * Added independent "IndentFlags" property to inner debugger class * Fixed debug logging in few places * Removed "ICollectingOptions" and merged it with new options abstract "ITelegratorOptions" * Added WebHook version of hosting class
103 lines
4.5 KiB
C#
103 lines
4.5 KiB
C#
using Telegram.Bot;
|
|
using Telegram.Bot.Polling;
|
|
using Telegrator.Configuration;
|
|
using Telegrator.MadiatorCore;
|
|
using Telegrator.Polling;
|
|
using Telegrator.Providers;
|
|
|
|
namespace Telegrator
|
|
{
|
|
/// <summary>
|
|
/// Main client class for the Telegrator library.
|
|
/// Extends TelegramBotClient with reactive capabilities for handling updates.
|
|
/// </summary>
|
|
public class TelegratorClient : TelegramBotClient, ITelegratorBot, ICollectingProvider
|
|
{
|
|
/// <summary>
|
|
/// The update router for handling incoming updates.
|
|
/// </summary>
|
|
private IUpdateRouter? updateRouter = null;
|
|
|
|
/// <inheritdoc/>
|
|
public TelegratorOptions Options { get; private set; }
|
|
|
|
/// <inheritdoc/>
|
|
public IHandlersCollection Handlers { get; private set; }
|
|
|
|
/// <inheritdoc/>
|
|
public ITelegramBotInfo BotInfo { get; private set; }
|
|
|
|
/// <inheritdoc/>
|
|
public IUpdateRouter UpdateRouter { get => updateRouter ?? throw new Exception(); }
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="TelegratorClient"/> class with a bot token.
|
|
/// </summary>
|
|
/// <param name="token">The bot token from BotFather.</param>
|
|
/// <param name="httpClient">Optional HTTP client for making requests.</param>
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
public TelegratorClient(string token, HttpClient? httpClient = null, CancellationToken cancellationToken = default)
|
|
: this(new TelegramBotClientOptions(token), httpClient, cancellationToken) { }
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="TelegratorClient"/> class with bot options.
|
|
/// </summary>
|
|
/// <param name="options">The Telegram bot client options.</param>
|
|
/// <param name="httpClient">Optional HTTP client for making requests.</param>
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
public TelegratorClient(TelegramBotClientOptions options, HttpClient? httpClient = null, CancellationToken cancellationToken = default) : base(options, httpClient, cancellationToken)
|
|
{
|
|
Options = new TelegratorOptions();
|
|
Handlers = new HandlersCollection(default);
|
|
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="cancellationToken">The cancellation token to stop receiving updates.</param>
|
|
public void StartReceiving(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);
|
|
|
|
updateRouter = new UpdateRouter(handlerProvider, awaitingProvider, Options, BotInfo);
|
|
StartReceivingInternal(receiverOptions, cancellationToken);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Internal method that starts the update receiving process.
|
|
/// Handles the reactive update receiver and error handling.
|
|
/// </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)
|
|
{
|
|
try
|
|
{
|
|
try
|
|
{
|
|
await new ReactiveUpdateReceiver(this, receiverOptions)
|
|
.ReceiveAsync(UpdateRouter, cancellationToken)
|
|
.ConfigureAwait(false);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
await UpdateRouter
|
|
.HandleErrorAsync(this, exception, HandleErrorSource.FatalError, cancellationToken)
|
|
.ConfigureAwait(false);
|
|
}
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
// Cancelled
|
|
}
|
|
}
|
|
}
|
|
}
|