From 2a2638782efc4a5d1433603a4d51fe37db667421 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 17 Oct 2025 09:19:57 +0000 Subject: [PATCH 1/3] Initial plan From 74f99fb5cbb8e4c13049b8dfa7a493d0fa8c8e25 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 17 Oct 2025 09:26:25 +0000 Subject: [PATCH 2/3] Implement automatic Telegram bot command registration Co-authored-by: LazuliKao <46601807+LazuliKao@users.noreply.github.com> --- .../TelegramAdapter.cs | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/src/HuaJiBot.NET.Adapter.Telegram/TelegramAdapter.cs b/src/HuaJiBot.NET.Adapter.Telegram/TelegramAdapter.cs index c5e9040..d3352fb 100644 --- a/src/HuaJiBot.NET.Adapter.Telegram/TelegramAdapter.cs +++ b/src/HuaJiBot.NET.Adapter.Telegram/TelegramAdapter.cs @@ -6,6 +6,7 @@ using Telegram.Bot.Exceptions; using Telegram.Bot.Types; using Telegram.Bot.Types.Enums; +using BotCommand = Telegram.Bot.Types.BotCommand; namespace HuaJiBot.NET.Adapter.Telegram; @@ -61,6 +62,9 @@ public override async Task SetupServiceAsync() return Task.CompletedTask; }; + // Subscribe to OnInitialized event to register commands after all plugins are loaded + Events.OnInitialized += RegisterBotCommandsAsync; + Log("Telegram bot is receiving messages..."); } catch (Exception ex) @@ -412,4 +416,67 @@ private async Task HandleUpdateAsync(Update update) LogError("Error handling Telegram update", ex); } } + + private async void RegisterBotCommandsAsync(object? sender, BotServiceBase service) + { + try + { + // Collect all commands from all loaded plugins + var commands = new List(); + + foreach (var (entryPoint, plugin) in Internal.Plugins) + { + if (!plugin.Enabled) + continue; + + foreach (var commandInfo in plugin.GetAllCommands()) + { + // Telegram bot commands must be lowercase and can only contain letters, digits and underscores + // Maximum length is 32 characters + var commandName = commandInfo.Name.ToLowerInvariant(); + + // Check if command name contains only valid characters (letters, digits, underscores) + if (!System.Text.RegularExpressions.Regex.IsMatch(commandName, @"^[a-z0-9_]+$")) + { + LogDebug($"Skipping command '{commandInfo.Name}' - contains invalid characters for Telegram (only a-z, 0-9, _ allowed)"); + continue; + } + + if (commandName.Length > 32) + { + LogDebug($"Skipping command '{commandInfo.Name}' - name too long for Telegram (max 32 chars)"); + continue; + } + + // Telegram command description max length is 256 characters + var description = commandInfo.Description; + if (description.Length > 256) + { + description = description.Substring(0, 253) + "..."; + } + + commands.Add(new BotCommand + { + Command = commandName, + Description = description + }); + } + } + + if (commands.Count > 0) + { + // Register commands with Telegram Bot API + await _botClient.SetMyCommands(commands, cancellationToken: _cancellationTokenSource.Token); + Log($"Successfully registered {commands.Count} commands with Telegram Bot API"); + } + else + { + Log("No commands to register with Telegram Bot API"); + } + } + catch (Exception ex) + { + LogError("Failed to register bot commands with Telegram", ex); + } + } } From e605a68c20dfd34f92927c49e65767fd0fb12ebc Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 17 Oct 2025 09:27:39 +0000 Subject: [PATCH 3/3] Update README with command registration documentation Co-authored-by: LazuliKao <46601807+LazuliKao@users.noreply.github.com> --- src/HuaJiBot.NET.Adapter.Telegram/README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/HuaJiBot.NET.Adapter.Telegram/README.md b/src/HuaJiBot.NET.Adapter.Telegram/README.md index 5f85090..21ee14c 100644 --- a/src/HuaJiBot.NET.Adapter.Telegram/README.md +++ b/src/HuaJiBot.NET.Adapter.Telegram/README.md @@ -14,6 +14,7 @@ This adapter allows HuaJiBot.NET to connect to Telegram using the official Teleg - Set group chat titles - Get user member permissions - HTML formatting support for rich text +- **Automatic bot command registration** - Commands from plugins are automatically registered with Telegram Bot API ## Setup @@ -38,6 +39,18 @@ await telegramAdapter.SetupServiceAsync(); The TelegramAdapter requires only a bot token to function. The bot token is obtained from [@BotFather](https://t.me/BotFather) when you create a new bot. +### Automatic Command Registration + +When plugins are loaded, the adapter automatically collects all commands and registers them with the Telegram Bot API using the `setMyCommands` method. This makes commands visible to users in the Telegram UI. + +**Command Requirements:** +- Command names must contain only lowercase letters (a-z), digits (0-9), and underscores (_) +- Maximum length: 32 characters +- Commands with Chinese or other non-ASCII characters in names will be skipped +- Command descriptions are limited to 256 characters + +The adapter will log debug messages for any commands that are skipped due to invalid names. + ## Message Types - **TextMessage**: Sent as regular text with HTML formatting support