|
16 | 16 | using Newtonsoft.Json.Linq; |
17 | 17 | using TelegramBot.Attributes; |
18 | 18 | using TelegramBot.Services; |
| 19 | +using TelegramBot.Builders; |
19 | 20 |
|
20 | 21 | namespace TelegramBot |
21 | 22 | { |
@@ -122,8 +123,21 @@ public async Task StartAsync(CancellationToken cancellationToken = default) |
122 | 123 | await hostedService.StartAsync(mergedToken); |
123 | 124 | _logger.LogInformation("Started '{hostedService}'.", hostedService.GetType().Name); |
124 | 125 | } |
125 | | - var hostApplicationLifetime = _serviceProvider.GetRequiredService<IHostApplicationLifetime>() |
| 126 | + var hostApplicationLifetime = _serviceProvider.GetService<IHostApplicationLifetime>() |
126 | 127 | as HostApplicationLifetime ?? throw new InvalidOperationException("Host application lifetime is not registered."); |
| 128 | + var commandRegistrationBuilders = _serviceProvider.GetServices<CommandRegistrationBuilder>(); |
| 129 | + if (commandRegistrationBuilders != null && commandRegistrationBuilders.Any()) |
| 130 | + { |
| 131 | + foreach (var builder in commandRegistrationBuilders) |
| 132 | + { |
| 133 | + var commands = builder.Build(); |
| 134 | + await _client.SetMyCommandsAsync(commands, |
| 135 | + languageCode: builder.Language, |
| 136 | + cancellationToken: mergedToken); |
| 137 | + _logger.LogInformation("Registered {count} commands for language '{language}'.", |
| 138 | + commands.Count(), builder.Language); |
| 139 | + } |
| 140 | + } |
127 | 141 | hostApplicationLifetime.NotifyStarted(); |
128 | 142 | } |
129 | 143 |
|
@@ -225,7 +239,7 @@ private async Task HandleRequestAsync(ITelegramUpdateHandler handler, Update upd |
225 | 239 | _logger.LogWarning("Method not found for message: {Text}.", update.Message?.Text); |
226 | 240 | return; |
227 | 241 | } |
228 | | - if (method.GetCustomAttribute<AuthorizeAttribute>() != null |
| 242 | + if (method.GetCustomAttribute<AuthorizeAttribute>() != null |
229 | 243 | || method.DeclaringType?.GetCustomAttribute<AuthorizeAttribute>() != null) |
230 | 244 | { |
231 | 245 | if (_serviceProvider.GetService<IBotAuthorizationHandler>() is IBotAuthorizationHandler authorizationHandler) |
@@ -268,6 +282,25 @@ await authorizationHandler |
268 | 282 | { |
269 | 283 | throw new InvalidOperationException("Invalid result type: " + result.GetType().Name); |
270 | 284 | } |
| 285 | + |
| 286 | + if (controller is IAsyncDisposable asyncDisposable) |
| 287 | + { |
| 288 | + await asyncDisposable.DisposeAsync(); |
| 289 | + } |
| 290 | + else if (controller is IDisposable disposable) |
| 291 | + { |
| 292 | + disposable.Dispose(); |
| 293 | + } |
| 294 | + |
| 295 | + if (result is IAsyncDisposable asyncDisposableResult) |
| 296 | + { |
| 297 | + await asyncDisposableResult.DisposeAsync(); |
| 298 | + } |
| 299 | + else if (result is IDisposable disposableResult) |
| 300 | + { |
| 301 | + disposableResult.Dispose(); |
| 302 | + } |
| 303 | + |
271 | 304 | } |
272 | 305 |
|
273 | 306 | private void CheckDisposed() |
|
0 commit comments