@@ -23,8 +23,8 @@ BotEngineConfig botEngineConfig
2323{
2424 private IEnumerable < Type > BotCommands => GetCommands ( ) ;
2525 private List < MethodInfo > BotMethods => GetMethods ( ) ;
26-
27- #region Invocation
26+
27+ #region Type Handling
2828
2929 public async Task < object ? > HandleUpdate ( ITelegramBotClient botClient , Update update , CancellationToken token )
3030 {
@@ -63,6 +63,7 @@ private async Task TrackUpdate(ITelegramBotClient botClient, CancellationToken t
6363 {
6464 try
6565 {
66+ logger . LogTrace ( "Finding command for update type: {UpdateType}" , update . Type ) ;
6667 var method = update . Type switch
6768 {
6869 UpdateType . InlineQuery => GetMethod ( update . InlineQuery ! ) ,
@@ -76,6 +77,8 @@ private async Task TrackUpdate(ITelegramBotClient botClient, CancellationToken t
7677 return null ;
7778 }
7879
80+ logger . LogTrace ( "Found command for update type: {UpdateType}" , update . Type ) ;
81+
7982 method . Update = update ;
8083 var invokeMethod = await InvokeMethod ( botClient , method ) ;
8184
@@ -94,6 +97,7 @@ private async Task TrackUpdate(ITelegramBotClient botClient, CancellationToken t
9497 try
9598 {
9699 var message = update . Message ?? update . EditedMessage ;
100+ logger . LogTrace ( "Finding command for message: {MessageText}" , message ? . Text ) ;
97101 var method = GetMethod ( message ! ) ;
98102
99103 if ( method == null )
@@ -102,6 +106,8 @@ private async Task TrackUpdate(ITelegramBotClient botClient, CancellationToken t
102106 return null ;
103107 }
104108
109+ logger . LogTrace ( "Found command for message: {MessageText}" , message ? . Text ) ;
110+
105111 method . Update = update ;
106112
107113 var invokeMethod = await InvokeMethod ( botClient , method ) ;
@@ -116,6 +122,10 @@ private async Task TrackUpdate(ITelegramBotClient botClient, CancellationToken t
116122 return null ;
117123 }
118124
125+ #endregion
126+
127+ #region Invocation
128+
119129 private async Task < object ? > InvokeMethod ( ITelegramBotClient client , BotCommandInfo ? botCommandInfo )
120130 {
121131 if ( botCommandInfo is null )
@@ -140,15 +150,38 @@ private async Task TrackUpdate(ITelegramBotClient botClient, CancellationToken t
140150 ] ;
141151 }
142152
143- #region Before Command Middleware
153+ var middlewarePassed = await ExecuteBeforeMiddlewareAsync ( commandContext ) ;
154+ if ( ! middlewarePassed )
155+ {
156+ return null ;
157+ }
144158
159+ #region Invoke Command
160+
161+ var controller = ( BotCommandController ) ActivatorUtilities . CreateInstance ( provider , botCommandInfo . ControllerType ) ;
162+ controller . Context = commandContext ;
163+
164+ logger . LogTrace ( "Invoking command: {Controller}/{Method}" , botCommandInfo . ControllerType . Name , botCommandInfo . Method . Name ) ;
165+ var invokeResult = await MethodHelper . InvokeMethod ( botCommandInfo . Method , paramList , controller ) ;
166+ logger . LogDebug ( "Successfully handled UpdateId: {UpdateId} for {UpdateType} " , botCommandInfo . Update ! . Id , botCommandInfo . Update ! . Type ) ;
167+
168+ #endregion
169+
170+ await ExecuteAfterMiddlewareAsync ( commandContext ) ;
171+
172+ return invokeResult ;
173+ }
174+
175+ private async Task < bool > ExecuteBeforeMiddlewareAsync ( CommandContext commandContext )
176+ {
145177 var beforeCommands = provider . GetServices < IBeforeCommand > ( )
146178 . Where ( x => x . GetType ( ) . GetCustomAttribute < DisabledMiddlewareAttribute > ( ) == null )
147179 . Where ( x => botEngineConfig . DisabledMiddleware ? . Contains ( x . GetType ( ) . Name ) == false )
148180 . ToList ( ) ;
149181
150182 var passedMiddlewareCount = 0 ;
151183
184+ logger . LogTrace ( "Found {Count} BeforeMiddlewares" , beforeCommands . Count ) ;
152185 foreach ( var command in beforeCommands )
153186 {
154187 var middlewareName = command . GetType ( ) . Name ;
@@ -167,27 +200,20 @@ await command.ExecuteAsync(commandContext, data =>
167200 {
168201 logger . LogDebug ( "Handler stops because middleware is not passed" ) ;
169202
170- return null ;
203+ return false ;
171204 }
172205
173- #endregion
174-
175- #region Invoke Command
176-
177- var controller = ( BotCommandController ) ActivatorUtilities . CreateInstance ( provider , botCommandInfo . ControllerType ) ;
178- controller . Context = commandContext ;
179-
180- var invokeResult = await MethodHelper . InvokeMethod ( botCommandInfo . Method , paramList , controller ) ;
181- logger . LogDebug ( "Successfully handled UpdateId: {UpdateId} for {UpdateType} " , botCommandInfo . Update ! . Id , botCommandInfo . Update ! . Type ) ;
182-
183- #endregion
184-
185- #region After Command Middleware
206+ logger . LogTrace ( "All BeforeMiddlewares passed" ) ;
207+ return true ;
208+ }
186209
210+ private async Task ExecuteAfterMiddlewareAsync ( CommandContext commandContext )
211+ {
187212 var afterCommands = provider . GetServices < IAfterCommand > ( )
188213 . Where ( x => x . GetType ( ) . GetCustomAttribute < DisabledMiddlewareAttribute > ( ) == null )
189214 . ToList ( ) ;
190215
216+ logger . LogTrace ( "Found {Count} AfterMiddlewares" , afterCommands . Count ) ;
191217 foreach ( var command in afterCommands )
192218 {
193219 var middlewareName = command . GetType ( ) . Name ;
@@ -196,14 +222,10 @@ await command.ExecuteAsync(commandContext, data =>
196222 await command . ExecuteAsync ( commandContext ) ;
197223 logger . LogDebug ( "AfterMiddleware - Complete: {Middleware}" , middlewareName ) ;
198224 }
199-
200- #endregion
201-
202- return invokeResult ;
203225 }
204226
205227 #endregion
206-
228+
207229 #region Command
208230
209231 private BotCommandInfo ? GetMethod ( Update update )
0 commit comments