Skip to content

Commit 782e7ea

Browse files
committed
🐗 Migrate away from deprecated QueuedUpdateReceiver
1 parent b44b9fd commit 782e7ea

File tree

2 files changed

+47
-26
lines changed

2 files changed

+47
-26
lines changed

CubicBot.Telegram/BotService.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Microsoft.Extensions.Logging;
33
using Telegram.Bot;
44
using Telegram.Bot.Exceptions;
5-
using Telegram.Bot.Polling;
65
using Telegram.Bot.Types;
76

87
namespace CubicBot.Telegram;
@@ -85,28 +84,27 @@ private async Task RunBotAsync(CancellationToken cancellationToken = default)
8584

8685
logger.LogInformation("Started Telegram bot: @{BotUsername} ({BotId})", me.Username, me.Id);
8786

88-
var updateReceiver = new QueuedUpdateReceiver(bot, null, updateHandler.HandleErrorAsync);
8987
var saveDataTask = SaveDataHourlyAsync(data, cancellationToken);
9088

9189
try
9290
{
93-
await updateHandler.HandleUpdateStreamAsync(bot, updateReceiver, cancellationToken);
91+
await updateHandler.RunAsync(bot, cancellationToken);
9492
}
9593
finally
9694
{
9795
await saveDataTask;
9896
}
9997
}
10098

99+
private static readonly TimeSpan s_saveDataInterval = TimeSpan.FromHours(1);
100+
101101
private async Task SaveDataHourlyAsync(Data data, CancellationToken cancellationToken = default)
102102
{
103-
TimeSpan interval = TimeSpan.FromHours(1);
104-
105103
while (!cancellationToken.IsCancellationRequested)
106104
{
107105
try
108106
{
109-
await Task.Delay(interval, cancellationToken);
107+
await Task.Delay(s_saveDataInterval, cancellationToken);
110108
}
111109
catch (TaskCanceledException)
112110
{

CubicBot.Telegram/UpdateHandler.cs

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,40 +50,63 @@ public async Task RegisterCommandsAsync(ITelegramBotClient botClient, Cancellati
5050
[LoggerMessage(Level = LogLevel.Information, Message = "Registered {CommandCount} bot commands")]
5151
private partial void LogRegisteredCommands(int commandCount);
5252

53-
public async Task HandleUpdateStreamAsync(ITelegramBotClient botClient, IAsyncEnumerable<Update> updates, CancellationToken cancellationToken = default)
53+
public async Task RunAsync(ITelegramBotClient botClient, CancellationToken cancellationToken = default)
5454
{
55-
await foreach (var update in updates.WithCancellation(cancellationToken))
55+
while (!cancellationToken.IsCancellationRequested)
5656
{
57-
LogReceivedUpdate(update.Id, update.Type);
57+
try
58+
{
59+
Update[] updates = await botClient.GetUpdates(allowedUpdates: [UpdateType.Message], cancellationToken: cancellationToken);
60+
foreach (Update update in updates)
61+
{
62+
LogReceivedUpdate(update.Id, update.Type);
63+
64+
if (update.Type == UpdateType.Message && update.Message is Message message)
65+
{
66+
var messageContext = new MessageContext(botClient, message, _data);
5867

59-
if (update.Type == UpdateType.Message && update.Message is not null)
68+
foreach (var dispatch in _dispatches)
69+
{
70+
_ = dispatch.HandleAsync(messageContext, cancellationToken)
71+
.ContinueWith(t =>
72+
{
73+
if (t?.Exception?.InnerException is not null)
74+
{
75+
LogFailedToHandleUpdate(t.Exception.InnerException);
76+
}
77+
}, TaskContinuationOptions.OnlyOnFaulted);
78+
}
79+
}
80+
}
81+
}
82+
catch (OperationCanceledException)
83+
{
84+
return;
85+
}
86+
catch (Exception ex)
6087
{
61-
var messageContext = new MessageContext(botClient, update.Message, _data);
88+
LogFailedToReceiveUpdates(ex);
6289

63-
foreach (var dispatch in _dispatches)
90+
try
91+
{
92+
await Task.Delay(s_delayOnError, cancellationToken);
93+
}
94+
catch (TaskCanceledException)
6495
{
65-
_ = dispatch.HandleAsync(messageContext, cancellationToken)
66-
.ContinueWith(t =>
67-
{
68-
if (t?.Exception?.InnerException is not null)
69-
{
70-
HandleError(t.Exception.InnerException);
71-
}
72-
}, TaskContinuationOptions.OnlyOnFaulted);
96+
return;
7397
}
7498
}
7599
}
76100
}
77101

102+
private static readonly TimeSpan s_delayOnError = TimeSpan.FromSeconds(5);
103+
78104
[LoggerMessage(Level = LogLevel.Trace, Message = "Received update with ID {UpdateId} and type {UpdateType}")]
79105
private partial void LogReceivedUpdate(int updateId, UpdateType updateType);
80106

81-
public Task HandleErrorAsync(Exception ex, CancellationToken _ = default)
82-
{
83-
HandleError(ex);
84-
return Task.CompletedTask;
85-
}
107+
[LoggerMessage(Level = LogLevel.Warning, Message = "Failed to receive updates")]
108+
private partial void LogFailedToReceiveUpdates(Exception ex);
86109

87110
[LoggerMessage(Level = LogLevel.Warning, Message = "Failed to handle update")]
88-
private partial void HandleError(Exception ex);
111+
private partial void LogFailedToHandleUpdate(Exception ex);
89112
}

0 commit comments

Comments
 (0)