@@ -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