You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用python-telegram-bot添加并修改Menu Button

问题原因与修复方案

你的代码没效果是因为把MenuButtonCommands类直接传给了MenuButton构造函数,这是用法错误——应该创建MenuButtonCommands的实例,而非直接传类本身。

正确代码示例

首先确认导入正确的类:

from telegram import MenuButtonCommands

然后修改初始化时的代码:

await context.bot.set_chat_menu_button(
    chat_id=update.effective_chat.id,
    menu_button=MenuButtonCommands()
)

额外提示

  • 如果想让菜单按钮点击后显示自定义命令,得先通过set_my_commands给机器人注册命令列表,不然菜单里会是空的
  • 要是想给所有用户统一设置菜单按钮,可以省略chat_id参数:
await context.bot.set_chat_menu_button(menu_button=MenuButtonCommands())

内容的提问来源于stack exchange,提问作者Juan David

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 15:02:40