如何使用aiogram为列表添加团队说明及配置Telegram Bot命令菜单
aiogram 配置 Telegram 斜杠命令菜单教程
1. 正确调用 types.BotCommand 实现命令菜单
你只需要按以下步骤配置即可,配置完成后用户输入 / 就会自动弹出对应命令菜单:
- 首先导入需要的模块:
from aiogram import Bot from aiogram.types import BotCommand, BotCommandScopeDefault
- 编写设置命令的异步函数,按照你的需求构造命令列表,每个
BotCommand接收两个参数:第一个是命令名称(不需要加/前缀),第二个是命令说明:
async def set_bot_commands(bot: Bot): # 你需要的对应列表里的命令都可以按下面的格式依次添加 commands = [ BotCommand(command="start", description="启动机器人,初始化用户信息"), BotCommand(command="help", description="查看所有可用功能说明"), BotCommand(command="feedback", description="提交使用反馈给运营团队"), ] # 全局生效配置 await bot.set_my_commands( commands=commands, scope=BotCommandScopeDefault() )
- 在机器人启动逻辑中注册这个函数,让机器人启动时自动加载命令配置:
if __name__ == "__main__": # 初始化 Bot、Dispatcher 逻辑省略 dp.startup.register(set_bot_commands) dp.run_polling(bot)
2. 为命令列表添加团队说明
你可以通过两种方式实现:
- 如果要把团队说明入口直接放在命令菜单的描述里,直接修改对应
BotCommand的description参数即可,比如:
BotCommand(command="about", description="查看机器人所属团队介绍")
- 如果要展示更详细的团队说明,给对应命令编写回复逻辑即可,示例:
from aiogram.types import Message from aiogram.filters import Command @dp.message(Command("about")) async def send_team_info(message: Message): await message.answer(""" *团队相关说明*: - 本机器人由XX技术团队开发维护 - 所有功能为团队专属业务场景定制 - 问题对接人:@XXX 处理时效:工作日24小时内 """)
如果需要给特定用户组(比如管理员、特定群)显示专属命令列表,调整set_my_commands的scope参数即可,比如用BotCommandScopeChatAdministrators可以让管理命令仅对群管理员可见。
内容的提问来源于stack exchange,提问作者Beeming
相关产品推荐
相关产品推荐

