在我的代码中,我遇到callbackquery
处理程序的问题,当我点击/start
命令时出现下一步按钮,当我点击该按钮时它给我回复 hi ,直到此输出正确。然后,当我点击另一个命令/help
时,会出现帮助按钮,当我点击帮助按钮时,它会给我同样的答复,下一个按钮是喜
结论: 是否有办法杀死旧的callbackquery
处理程序。我找到的方法是从Conversationhandler.END
处理函数返回callbackquery
但是它限制了我的功能已经用Google搜索但没有找到预期的输出。
这是我的代码:
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()
This is code screenshot output for more detail
欢迎任何形式的帮助。
答案 0 :(得分:0)
您需要make your bot persistent(以免丢失状态),add error handler(以了解是否失败)和后备路由(如果没有路由匹配)。
在这种情况下,您会知道出了什么问题以及出在哪里。