如何处理 python 电报机器人中的回调查询
How to handle callbackquery in python telegram bot
在我的代码中,我遇到了回调查询处理程序的问题,当我点击 /start 命令时,会出现“Next”按钮,当我点击该按钮时,它会给我提示回复为“hi”,直到此处输出正确。然后当我点击另一个命令“/help”然后出现“help”按钮时,当我点击那个帮助按钮时它会给我之前的回复是 "hi",输出应该是“help”
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()
您是第一个会话处理程序,仍处于 FIRST
状态,因此仍在等待回调查询。
因为它是第一个添加的处理程序并且它们在同一组中,所以第一个会响应,而第二个不会。
您可以查看 CallbackQueryHandler
的 pattern
参数来解决您的问题。
似乎使用 return ConversationHandler.END
结束对话也可以,因为它会结束第一个对话。
请参阅此处的示例:https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/conversationbot.py
在我的代码中,我遇到了回调查询处理程序的问题,当我点击 /start 命令时,会出现“Next”按钮,当我点击该按钮时,它会给我提示回复为“hi”,直到此处输出正确。然后当我点击另一个命令“/help”然后出现“help”按钮时,当我点击那个帮助按钮时它会给我之前的回复是 "hi",输出应该是“help”
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()
您是第一个会话处理程序,仍处于 FIRST
状态,因此仍在等待回调查询。
因为它是第一个添加的处理程序并且它们在同一组中,所以第一个会响应,而第二个不会。
您可以查看 CallbackQueryHandler
的 pattern
参数来解决您的问题。
似乎使用 return ConversationHandler.END
结束对话也可以,因为它会结束第一个对话。
请参阅此处的示例:https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/conversationbot.py