我正在尝试创建一个菜单,用户可以在其中选择一个选项,并显示一些带有信息的文本。
我无法使用我的代码显示任何信息。有任何想法吗?
我尝试用callback_data
捕获用户"opciones"
,但是什么也没发生。
# ! python3
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackQueryHandler
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
import logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
############################### Bot ############################################
def start(update, context):
context.bot.send_message(chat_id=update.effective_chat.id, text="¡Hola!")
def info(update, context):
update.message.reply_text(main_menu_message(),
reply_markup=main_menu_keyboard())
def main_menu(update, context):
query = update.callback_query
query.answer()
query.edit_message_text(
text=main_menu_message(),
reply_markup=main_menu_keyboard())
def cs_menu(update, context):
query = update.callback_query
query.answer()
query.edit_message_text(
text=cms_menu_message(),
reply_markup=cs_menu_keyboard())
def omenu(update, context):
query = update.callback_query
query.answer()
query.edit_message_text(
text=omenu_message(),
reply_markup=omenu_keyboard())
def d_menu(update, context):
query = update.callback_query
query.answer()
query.edit_message_text(
text=dsl_menu_message(),
reply_markup=ds_menu_keyboard())
def opciones(update, context):
query = update.callback_query
query.answer()
if query.data == "cm11":
query.edit_message_text(text="Test 1")
elif query.data == "cm12":
query.edit_message_text(text="Test 2")
elif query.data == "cm13":
query.edit_message_text(text="Test 3")
def probando(update, context):
context.bot.send_message(chat_id=update.effective_chat.id, text="Probando las funciones.")
def unknown(update, context):
context.bot.send_message(chat_id=update.effective_chat.id, text="No entiendo ese comando.")
############################ Keyboards #########################################
def main_menu_keyboard():
keyboard = [[InlineKeyboardButton('CMs', callback_data='cm1')],
[InlineKeyboardButton('Os', callback_data='om2')],
[InlineKeyboardButton('Xs', callback_data='dm3')]]
return InlineKeyboardMarkup(keyboard)
def cs_menu_keyboard():
keyboard = [[InlineKeyboardButton('Option 1-1', callback_data='cm11')],
[InlineKeyboardButton('Option 1-2', callback_data='cm12')],
[InlineKeyboardButton('Option 1-3', callback_data='cm13')],
[InlineKeyboardButton('Option 1-4', callback_data='cm14')],
[InlineKeyboardButton('Option 1-5', callback_data='cm15')],
[InlineKeyboardButton('Menú principal', callback_data='main')]]
return InlineKeyboardMarkup(keyboard)
def omenu_keyboard():
keyboard = [[InlineKeyboardButton('Option 2-1', callback_data='om21')],
[InlineKeyboardButton('Option 2-2', callback_data='om22')],
[InlineKeyboardButton('Option 2-3', callback_data='om23')],
[InlineKeyboardButton('Option 2-4', callback_data='om24')],
[InlineKeyboardButton('Menú principal', callback_data='main')]]
return InlineKeyboardMarkup(keyboard)
def ds_menu_keyboard():
keyboard = [[InlineKeyboardButton('Option 3-1', callback_data='dm31')],
[InlineKeyboardButton('Option 3-2', callback_data='dm32')],
[InlineKeyboardButton('Option 3-3', callback_data='dm33')],
[InlineKeyboardButton('Option 3-4', callback_data='dm34')],
[InlineKeyboardButton('Menú principal', callback_data='main')]]
return InlineKeyboardMarkup(keyboard)
############################# Messages #########################################
def main_menu_message():
return 'First menu'
def cs_menu_message():
return 'Second menu'
def omenu_message():
return 'Third menu'
def ds_menu_message():
return 'Fourth menu'
############################# Handlers #########################################
if __name__ == "__main__":
TOKEN = 'TOKEN'
updater = Updater(token=TOKEN, use_context=True)
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler('start', start))
dispatcher.add_handler(CommandHandler('info', info))
dispatcher.add_handler(CommandHandler('probando', probando))
dispatcher.add_handler(MessageHandler(Filters.command, unknown))
updater.dispatcher.add_handler(CallbackQueryHandler(main_menu, pattern='main'))
updater.dispatcher.add_handler(CallbackQueryHandler(cs_menu, pattern='cm1'))
updater.dispatcher.add_handler(CallbackQueryHandler(omenu, pattern='om2'))
updater.dispatcher.add_handler(CallbackQueryHandler(d_menu, pattern='dm3'))
updater.dispatcher.add_handler(CallbackQueryHandler(opciones))
updater.start_polling()
# updater.start_polling(allowed_updates=[])
我还尝试将if部分添加到cs_menu
中,但是什么也没发生。
答案 0 :(得分:0)
对于像这样的复杂对话框,我建议使用{"response":"true","message":[{"label":"CAP TOPP D ","value":"CAP TOPP D "},{"label":"TAB FOLIC ACID 5","value":"TAB FOLIC ACID 5"},{"label":"TAB MTX 12.5","value":"TAB MTX 12.5"},{"label":"TAB PICSUL","value":"TAB PICSUL"},{"label":"TAB RELANAC PLUS","value":"TAB RELANAC PLUS"},{"label":"TAB SAAZ 1 GM","value":"TAB SAAZ 1 GM"},{"label":"TOPFENAC OIL","value":"TOPFENAC OIL"}]}
(请参阅official example库中的python-telegram-bot)。使用它,您将始终知道用户在哪里,要显示什么消息/键盘以及它如何对将来的输入做出反应。