在我的代码中,我遇到了callbackquery处理程序的问题,当我点击/启动命令时出现“下一步”按钮,当我点击该按钮时,它会给我回复“嗨“,直到这里输出正确。然后,当我点击另一个命令“/ 帮助”时,会出现“帮助”按钮,当我点击该帮助按钮时,它会给我之前的回复“hi”,其中输出应该是“帮助”
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, ConversationHandler
TELEGRAM_HTTP_API_TOKEN = 'token'
FIRST, SECOND, HELP = range(3)
def start(bot, update):
keyboard = [
[InlineKeyboardButton(u"Next", callback_data=str(FIRST))]
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.message.reply_text(
u"Start handler, Press next",
reply_markup=reply_markup
)
return FIRST
def first(bot, update):
query = update.callback_query
#reply_markup = InlineKeyboardMarkup(keyboard)
bot.send_message(chat_id=query.message.chat_id,
text='hi')
def help(bot,update):
keyboard = [
[InlineKeyboardButton(u"HELP", callback_data=str(HELP))]
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.message.reply_text(
u"Help handler, Press button",
reply_markup=reply_markup
)
return HELP
def myhelp(bot,update):
query = update.callback_query
bot.send_message(chat_id=query.message.chat_id,
text='help')
updater = Updater(TELEGRAM_HTTP_API_TOKEN)
conv_handler = ConversationHandler(
entry_points=[CommandHandler('start', start)],
states={
FIRST: [CallbackQueryHandler(first)]
},
fallbacks=[CommandHandler('start', start)]
)
conv_handler1=ConversationHandler(
entry_points=[CommandHandler('help',help)],
states={
HELP: [CallbackQueryHandler(myhelp)]
},
fallbacks=[CommandHandler('help',help)]
)
updater.dispatcher.add_handler(conv_handler)
updater.dispatcher.add_handler(conv_handler1)
updater.start_polling()
updater.idle()
答案 0 :(得分:1)
你是第一个sessionhandler仍处于状态FIRST
en因此仍在等待回调查询。
因为它是第一个添加的处理程序,它们在同一个组中,第一个将响应,而第二个不响应。
您可以查看pattern
的{{1}}参数来解决您的问题。
答案 1 :(得分:0)
似乎使用 return ConversationHandler.END
结束对话也可以,因为它会结束第一个对话。
请参阅此处的示例:https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/conversationbot.py