Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 17, 2025

概述 / Overview

实现了Telegram适配器自动注册机器人指令的功能。当插件加载完成后,适配器会自动收集所有插件的命令并通过Telegram Bot API注册,使命令在Telegram客户端的命令菜单中可见。

This PR implements automatic registration of bot commands in the Telegram adapter. After plugins are loaded, the adapter automatically collects all commands from plugins and registers them with the Telegram Bot API, making commands visible in Telegram's command menu.

实现细节 / Implementation Details

核心变更 / Core Changes

TelegramAdapter.cs 中添加了 RegisterBotCommandsAsync 方法,该方法:

  1. 订阅初始化事件: 通过 Events.OnInitialized 事件在所有插件加载完成后触发
  2. 收集命令: 遍历 Internal.Plugins 获取所有已启用插件的命令
  3. 验证命令名称: 确保符合Telegram要求(小写字母、数字、下划线,最大32字符)
  4. 注册到API: 使用 _botClient.SetMyCommands() 将命令注册到Telegram

Added RegisterBotCommandsAsync method to TelegramAdapter.cs that:

  1. Subscribes to initialization event: Triggered via Events.OnInitialized after all plugins are loaded
  2. Collects commands: Iterates through Internal.Plugins to get commands from all enabled plugins
  3. Validates command names: Ensures compliance with Telegram requirements (lowercase, alphanumeric + underscore, max 32 chars)
  4. Registers with API: Uses _botClient.SetMyCommands() to register commands with Telegram

命令验证规则 / Command Validation Rules

  • ✅ 命令名称只能包含小写字母(a-z)、数字(0-9)和下划线() / Command names must contain only lowercase letters (a-z), digits (0-9), and underscores ()
  • ✅ 命令名称最大长度32字符 / Maximum command name length: 32 characters
  • ✅ 命令描述最大长度256字符(超出会自动截断) / Maximum description length: 256 characters (truncated if exceeded)
  • ⚠️ 包含中文或其他非ASCII字符的命令会被跳过并记录调试日志 / Commands with Chinese or other non-ASCII characters are skipped with debug logging

示例 / Example

// 插件中定义的命令 / Commands defined in plugins
[Command("help", "显示帮助信息")]
public void HelpCommand(GroupMessageEventArgs e) { ... }

[Command("query", "Query server status")]
public void QueryCommand(GroupMessageEventArgs e) { ... }

这些命令会自动注册到Telegram,用户可以在聊天输入框中看到命令提示。

These commands will be automatically registered with Telegram, and users will see command suggestions in the chat input field.

影响范围 / Impact

  • 仅影响Telegram适配器 / Only affects Telegram adapter
  • 向后兼容 / Backward compatible
  • 无破坏性变更 / No breaking changes
  • 纯功能增强 / Pure feature enhancement

测试 / Testing

由于需要真实的Telegram Bot Token,自动化测试受限。建议手动测试:

  1. 启动带有Telegram适配器的机器人
  2. 检查日志确认命令注册成功
  3. 在Telegram客户端中输入 / 查看命令列表

Automated testing is limited due to the need for a real Telegram Bot Token. Manual testing recommended:

  1. Start bot with Telegram adapter
  2. Check logs to confirm successful command registration
  3. Type / in Telegram client to view command list

文档更新 / Documentation

更新了 README.md 文档,说明了自动命令注册功能和相关限制。

Updated README.md with details about automatic command registration feature and limitations.


Fixes #[issue_number]

Original prompt

在Telegram适配器自动注册tg机器人指令


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 2 commits October 17, 2025 09:26
Co-authored-by: LazuliKao <46601807+LazuliKao@users.noreply.github.com>
Co-authored-by: LazuliKao <46601807+LazuliKao@users.noreply.github.com>
Copilot AI changed the title [WIP] Add auto registration for Telegram bot commands 自动注册Telegram机器人指令 / Automatically register Telegram bot commands Oct 17, 2025
Copilot AI requested a review from LazuliKao October 17, 2025 09:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants