|
6 | 6 | using Telegram.Bot.Exceptions; |
7 | 7 | using Telegram.Bot.Types; |
8 | 8 | using Telegram.Bot.Types.Enums; |
| 9 | +using BotCommand = Telegram.Bot.Types.BotCommand; |
9 | 10 |
|
10 | 11 | namespace HuaJiBot.NET.Adapter.Telegram; |
11 | 12 |
|
@@ -61,6 +62,9 @@ public override async Task SetupServiceAsync() |
61 | 62 | return Task.CompletedTask; |
62 | 63 | }; |
63 | 64 |
|
| 65 | + // Subscribe to OnInitialized event to register commands after all plugins are loaded |
| 66 | + Events.OnInitialized += RegisterBotCommandsAsync; |
| 67 | + |
64 | 68 | Log("Telegram bot is receiving messages..."); |
65 | 69 | } |
66 | 70 | catch (Exception ex) |
@@ -412,4 +416,67 @@ private async Task HandleUpdateAsync(Update update) |
412 | 416 | LogError("Error handling Telegram update", ex); |
413 | 417 | } |
414 | 418 | } |
| 419 | + |
| 420 | + private async void RegisterBotCommandsAsync(object? sender, BotServiceBase service) |
| 421 | + { |
| 422 | + try |
| 423 | + { |
| 424 | + // Collect all commands from all loaded plugins |
| 425 | + var commands = new List<BotCommand>(); |
| 426 | + |
| 427 | + foreach (var (entryPoint, plugin) in Internal.Plugins) |
| 428 | + { |
| 429 | + if (!plugin.Enabled) |
| 430 | + continue; |
| 431 | + |
| 432 | + foreach (var commandInfo in plugin.GetAllCommands()) |
| 433 | + { |
| 434 | + // Telegram bot commands must be lowercase and can only contain letters, digits and underscores |
| 435 | + // Maximum length is 32 characters |
| 436 | + var commandName = commandInfo.Name.ToLowerInvariant(); |
| 437 | + |
| 438 | + // Check if command name contains only valid characters (letters, digits, underscores) |
| 439 | + if (!System.Text.RegularExpressions.Regex.IsMatch(commandName, @"^[a-z0-9_]+$")) |
| 440 | + { |
| 441 | + LogDebug($"Skipping command '{commandInfo.Name}' - contains invalid characters for Telegram (only a-z, 0-9, _ allowed)"); |
| 442 | + continue; |
| 443 | + } |
| 444 | + |
| 445 | + if (commandName.Length > 32) |
| 446 | + { |
| 447 | + LogDebug($"Skipping command '{commandInfo.Name}' - name too long for Telegram (max 32 chars)"); |
| 448 | + continue; |
| 449 | + } |
| 450 | + |
| 451 | + // Telegram command description max length is 256 characters |
| 452 | + var description = commandInfo.Description; |
| 453 | + if (description.Length > 256) |
| 454 | + { |
| 455 | + description = description.Substring(0, 253) + "..."; |
| 456 | + } |
| 457 | + |
| 458 | + commands.Add(new BotCommand |
| 459 | + { |
| 460 | + Command = commandName, |
| 461 | + Description = description |
| 462 | + }); |
| 463 | + } |
| 464 | + } |
| 465 | + |
| 466 | + if (commands.Count > 0) |
| 467 | + { |
| 468 | + // Register commands with Telegram Bot API |
| 469 | + await _botClient.SetMyCommands(commands, cancellationToken: _cancellationTokenSource.Token); |
| 470 | + Log($"Successfully registered {commands.Count} commands with Telegram Bot API"); |
| 471 | + } |
| 472 | + else |
| 473 | + { |
| 474 | + Log("No commands to register with Telegram Bot API"); |
| 475 | + } |
| 476 | + } |
| 477 | + catch (Exception ex) |
| 478 | + { |
| 479 | + LogError("Failed to register bot commands with Telegram", ex); |
| 480 | + } |
| 481 | + } |
415 | 482 | } |
0 commit comments