* Added integration addon wit WTelegramBot (WIP)
* Added some extensions methods * Refactored Result behaviour * Added missing exception messages * Removed telegrator-specific host builder (obsolete) * Code cleanup and bug fixes
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
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
|
||||
public class HostedWideBotUpdateReceiver(ILogger<HostedWideBotUpdateReceiver> logger, ITelegramBotClient botClient, IUpdateRouter updateRouter, IOptions<ReceiverOptions>? options) : BackgroundService
|
||||
{
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
if (botClient is not WTelegramBotClient wideBotClient)
|
||||
throw new Exception("Registered ITelegramBotClient was not a wide client (WTelegramBotClient)! Please, use `AddWideTelegrator` instead.");
|
||||
|
||||
if (options?.Value.DropPendingUpdates is true)
|
||||
await wideBotClient.DropPendingUpdates();
|
||||
|
||||
logger.LogInformation("Starting receiving updates via MTProto");
|
||||
|
||||
// UIP (understanding in progress)
|
||||
//_receiverOptions.AllowedUpdates = updateRouter.HandlersProvider.AllowedTypes.ToArray();
|
||||
|
||||
botClient.DeleteWebhook(options?.Value.DropPendingUpdates ?? false, cancellationToken: stoppingToken)
|
||||
.ConfigureAwait(false).GetAwaiter().GetResult();
|
||||
|
||||
WideUpdateReceiver updateReceiver = new WideUpdateReceiver(wideBotClient);
|
||||
await updateReceiver.ReceiveAsync(updateRouter, stoppingToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Polling;
|
||||
using Telegrator.Core;
|
||||
using WUpdate = WTelegram.Types.Update;
|
||||
|
||||
namespace Telegrator.Mediation;
|
||||
|
||||
public class WideUpdateReceiver(WTelegramBotClient client) : IUpdateReceiver
|
||||
{
|
||||
private readonly WTelegramBotClient _client = client;
|
||||
private IUpdateHandler? _updateHandler = null;
|
||||
private CancellationToken _cancellation = default;
|
||||
|
||||
public async Task ReceiveAsync(IUpdateHandler updateHandler, CancellationToken cancellationToken = default)
|
||||
{
|
||||
_updateHandler = updateHandler;
|
||||
_cancellation = cancellationToken;
|
||||
|
||||
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
await using CancellationTokenRegistration registration = cancellationToken.Register(() => tcs.TrySetResult(null!));
|
||||
|
||||
try
|
||||
{
|
||||
_client.OnUpdate += OnUpdate;
|
||||
await tcs.Task.ConfigureAwait(false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_client.OnUpdate -= OnUpdate;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task OnUpdate(WUpdate update)
|
||||
{
|
||||
if (_updateHandler == null)
|
||||
throw new Exception("Router not initialized (got null)");
|
||||
|
||||
try
|
||||
{
|
||||
await _updateHandler.HandleUpdateAsync(_client, update, _cancellation).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await _updateHandler.HandleErrorAsync(_client, ex, HandleErrorSource.HandleUpdateError, _cancellation).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user