@@ -34,11 +34,17 @@ public virtual void Init(IUpdateContext context, CancellationToken cancellationT
3434 Message = new MessageBuilder ( ) ;
3535 }
3636
37+ #region state management
3738 protected ValueTask State ( object state )
3839 {
3940 return Store ! . Set ( FromId , BotControllersFSMMiddleware . STATE_KEY , state ) ;
4041 }
4142
43+ protected ValueTask ClearState ( )
44+ {
45+ return Store ! . Remove ( FromId , BotControllersFSMMiddleware . STATE_KEY ) ;
46+ }
47+
4248 protected ValueTask AState < T > ( T state , string ? name = null ) where T : notnull
4349 {
4450 if ( name == null )
@@ -118,7 +124,9 @@ async ValueTask CallClear()
118124 }
119125 }
120126 }
127+ #endregion
121128
129+ #region misc
122130 public async Task Call < T > ( Func < T , Task > method ) where T : BotController
123131 {
124132 var controller = Context ! . Services . GetRequiredService < T > ( ) ;
@@ -160,6 +168,7 @@ public virtual async Task OnAfterCall()
160168 await SendOrUpdate ( ) ;
161169 }
162170 }
171+ #endregion
163172
164173 #region sending
165174 public async Task SendOrUpdate ( )
@@ -184,6 +193,7 @@ protected async Task Send(string text, ParseMode mode)
184193 replyMarkup : Message . Markup ,
185194 cancellationToken : CancelToken ,
186195 replyToMessageId : Message . ReplyToMessageId ) ;
196+ ClearMessage ( ) ;
187197 }
188198
189199 public async Task UpdateMarkup ( InlineKeyboardMarkup markup )
@@ -207,11 +217,7 @@ public async Task Update(InlineKeyboardMarkup? markup = null, string? text = nul
207217 replyMarkup : markup ?? Message . Markup as InlineKeyboardMarkup ,
208218 cancellationToken : CancelToken
209219 ) ;
210- }
211-
212- protected async Task SendHtml ( string text )
213- {
214- await Send ( text , ParseMode . Html ) ;
220+ ClearMessage ( ) ;
215221 }
216222
217223 protected async Task Send ( string text )
@@ -234,16 +240,6 @@ public async Task Send()
234240 await Send ( text ) ;
235241 }
236242 }
237-
238- public async Task SendHtml ( )
239- {
240- Message . SetParseMode ( ParseMode . Html ) ;
241- var text = Message . Message ;
242- if ( text != null )
243- {
244- await SendHtml ( text ) ;
245- }
246- }
247243 #endregion
248244
249245 #region formatting
@@ -334,6 +330,11 @@ public void Reply(int? messageId = default)
334330 Message . ReplyTo ( messageId . GetValueOrDefault ( ) ) ;
335331 }
336332 }
333+
334+ public void ClearMessage ( )
335+ {
336+ Message = new MessageBuilder ( ) ;
337+ }
337338 #endregion
338339
339340 #region querying
@@ -431,4 +432,44 @@ public string FPath(string controller, string action, params object[] args)
431432 return $ "{ hit . template } { splitter } { part2 } ". TrimEnd ( '/' ) . TrimEnd ( ) ;
432433 }
433434 #endregion
435+
436+ #region chaining
437+
438+ public async Task < IUpdateContext > AwaitNextUpdate ( )
439+ {
440+ var store = Context . Services . GetRequiredService < ChainStorage > ( ) ;
441+ var tcs = new TaskCompletionSource < IUpdateContext > ( ) ;
442+ store . Set ( ChatId , new ( tcs ) ) ;
443+ return await tcs . Task ;
444+ }
445+
446+ public async Task < string > AwaitText ( )
447+ {
448+ while ( true )
449+ {
450+ var update = await AwaitNextUpdate ( ) ;
451+ if ( update . Update . Type != UpdateType . Message )
452+ {
453+ continue ;
454+ }
455+
456+ return update . GetSafeTextPayload ( ) ! ;
457+ }
458+ }
459+
460+ public async Task < string > AwaitQuery ( )
461+ {
462+ while ( true )
463+ {
464+ var update = await AwaitNextUpdate ( ) ;
465+ if ( update . Update . Type != UpdateType . CallbackQuery )
466+ {
467+ continue ;
468+ }
469+
470+ return update . GetSafeTextPayload ( ) ! ;
471+ }
472+ }
473+
474+ #endregion
434475}
0 commit comments