gpt4 book ai didi

python-telegram-bot - 多个回调查询处理程序?

转载 作者:行者123 更新时间:2023-12-04 07:33:46 25 4
gpt4 key购买 nike

python-telegram-bot包装器是否可以使用多个回调查询处理程序?

我希望有多个唯一的处理程序,但据我所知,只能有一个。这意味着我必须基于在启动消息文本上显示的嵌入式键盘。

有什么我想念的吗?

最佳答案

您可以为此使用ConversationHandler包装器。检查代码如下:

from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, ConversationHandler

TELEGRAM_HTTP_API_TOKEN = 'PASTE_TELEGRAM_HTTP_API_TOKEN'

FIRST, SECOND = range(2)

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
keyboard = [
[InlineKeyboardButton(u"Next", callback_data=str(SECOND))]
]
reply_markup = InlineKeyboardMarkup(keyboard)
bot.edit_message_text(
chat_id=query.message.chat_id,
message_id=query.message.message_id,
text=u"First CallbackQueryHandler, Press next"
)

reply_markup = InlineKeyboardMarkup(keyboard)

bot.edit_message_reply_markup(
chat_id=query.message.chat_id,
message_id=query.message.message_id,
reply_markup=reply_markup
)
return SECOND

def second(bot, update):
query = update.callback_query
bot.edit_message_text(
chat_id=query.message.chat_id,
message_id=query.message.message_id,
text=u"Second CallbackQueryHandler"
)
return

updater = Updater(TELEGRAM_HTTP_API_TOKEN)

conv_handler = ConversationHandler(
entry_points=[CommandHandler('start', start)],
states={
FIRST: [CallbackQueryHandler(first)],
SECOND: [CallbackQueryHandler(second)]
},
fallbacks=[CommandHandler('start', start)]
)

updater.dispatcher.add_handler(conv_handler)

updater.start_polling()

updater.idle()

关于python-telegram-bot - 多个回调查询处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41195822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com