Proper way to build menus with python-telegram-bot(使用 python-telegram-bot 构建菜单的正确方法)
问题描述
我使用 python-telegram-bot
并尝试像 BotFather bot 那样构建嵌套菜单系统.例如,您有一个通用的机器人菜单
I work with python-telegram-bot
and try to build a system of nested menus as BotFather bot does. For instance, you have a general bot menu
在这里你可以选择Edit Bot"并获得新的对应菜单
where you can choose "Edit Bot" and get the new corresponding menu
带有返回上一个菜单的选项.
with an option to get back to the previous menu.
我尝试用代码来实现:
# main menu
def start(bot, update):
menu_main = [[InlineKeyboardButton('Option 1', callback_data='m1')],
[InlineKeyboardButton('Option 2', callback_data='m2')],
[InlineKeyboardButton('Option 3', callback_data='m3')]]
reply_markup = InlineKeyboardMarkup(menu_main)
update.message.reply_text('Choose the option:', reply_markup=reply_markup)
# all other menus
def menu_actions(bot, update):
query = update.callback_query
if query.data == 'm1':
# first submenu
menu_1 = [[InlineKeyboardButton('Submenu 1-1', callback_data='m1_1')],
[InlineKeyboardButton('Submenu 1-2', callback_data='m1_2')]]
reply_markup = InlineKeyboardMarkup(menu_1)
bot.edit_message_text(chat_id=query.message.chat_id,
message_id=query.message.message_id,
text='Choose the option:',
reply_markup=reply_markup)
elif query.data == 'm2':
# second submenu
# first submenu
menu_2 = [[InlineKeyboardButton('Submenu 2-1', callback_data='m2_1')],
[InlineKeyboardButton('Submenu 2-2', callback_data='m2_2')]]
reply_markup = InlineKeyboardMarkup(menu_2)
bot.edit_message_text(chat_id=query.message.chat_id,
message_id=query.message.message_id,
text='Choose the option:',
reply_markup=reply_markup)
elif query.data == 'm1_1':
...
elif query.data == 'm1_2':
...
# and so on for every callback_data option
...
# handlers
dispatcher.add_handler(CommandHandler('start', start))
dispatcher.add_handler(CallbackQueryHandler(menu_actions))
这段代码有效,但我觉得构建一个长elif
树有点不合理.
This code works but I have a feeling that it is kind of irrational — to build a long elif
tree.
此外,我不知道如何给用户一个从二级菜单返回主菜单的选项(因为主菜单位于另一个处理程序中,我无法通过回调捕获它来自 CallbackQueryHandler
).
Moreover, I can't figure out how to give to the user an option to get back to the main menu from second level menus (since the main menu is located in another handler and I can't catch it with a callback from CallbackQueryHandler
).
那么问题是——构建这种菜单系统的最佳实践是什么?
So the question is — what is the best practice to build that kind of menu systems?
推荐答案
您应该在 CallbackQueryHandler
中使用参数 pattern
.为键盘和消息使用类或函数也是一件好事.
要返回主菜单,将返回按钮添加到具有特定回调模式的子菜单.
You should use an argument pattern
in CallbackQueryHandler
. Also is a good thing use a classes or functions for keyboards and messages.
To return to main menu add return button to submenu with specific callback pattern.
请注意:您在菜单中使用edit_message_text
.这意味着如果您从任何菜单中使用 reply_text
方法调用 start
函数,什么都不会发生.
Please note: you use edit_message_text
in menu. It's mean nothing will happen if you will call start
function with reply_text
method from any menu.
完整的函数示例:
#!/usr/bin/env python3.8
from telegram.ext import Updater
from telegram.ext import CommandHandler, CallbackQueryHandler
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
############################### Bot ############################################
def start(bot, update):
bot.message.reply_text(main_menu_message(),
reply_markup=main_menu_keyboard())
def main_menu(bot, update):
bot.callback_query.message.edit_text(main_menu_message(),
reply_markup=main_menu_keyboard())
def first_menu(bot, update):
bot.callback_query.message.edit_text(first_menu_message(),
reply_markup=first_menu_keyboard())
def second_menu(bot, update):
bot.callback_query.message.edit_text(second_menu_message(),
reply_markup=second_menu_keyboard())
def first_submenu(bot, update):
pass
def second_submenu(bot, update):
pass
def error(update, context):
print(f'Update {update} caused error {context.error}')
############################ Keyboards #########################################
def main_menu_keyboard():
keyboard = [[InlineKeyboardButton('Menu 1', callback_data='m1')],
[InlineKeyboardButton('Menu 2', callback_data='m2')],
[InlineKeyboardButton('Menu 3', callback_data='m3')]]
return InlineKeyboardMarkup(keyboard)
def first_menu_keyboard():
keyboard = [[InlineKeyboardButton('Submenu 1-1', callback_data='m1_1')],
[InlineKeyboardButton('Submenu 1-2', callback_data='m1_2')],
[InlineKeyboardButton('Main menu', callback_data='main')]]
return InlineKeyboardMarkup(keyboard)
def second_menu_keyboard():
keyboard = [[InlineKeyboardButton('Submenu 2-1', callback_data='m2_1')],
[InlineKeyboardButton('Submenu 2-2', callback_data='m2_2')],
[InlineKeyboardButton('Main menu', callback_data='main')]]
return InlineKeyboardMarkup(keyboard)
############################# Messages #########################################
def main_menu_message():
return 'Choose the option in main menu:'
def first_menu_message():
return 'Choose the submenu in first menu:'
def second_menu_message():
return 'Choose the submenu in second menu:'
############################# Handlers #########################################
updater = Updater('XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', use_context=True)
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CallbackQueryHandler(main_menu, pattern='main'))
updater.dispatcher.add_handler(CallbackQueryHandler(first_menu, pattern='m1'))
updater.dispatcher.add_handler(CallbackQueryHandler(second_menu, pattern='m2'))
updater.dispatcher.add_handler(CallbackQueryHandler(first_submenu, pattern='m1_1'))
updater.dispatcher.add_handler(CallbackQueryHandler(second_submenu, pattern='m2_1'))
updater.dispatcher.add_error_handler(error)
updater.start_polling()
################################################################################
抱歉,我在 tab 中有两个空格.:)
Sorry, i have two spaces in tab. :)
UPD:修复子菜单对象.
这篇关于使用 python-telegram-bot 构建菜单的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 python-telegram-bot 构建菜单的正确方法


基础教程推荐
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 筛选NumPy数组 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01