【发布时间】:2020-10-02 03:25:59
【问题描述】:
我正在构建一个带有 python-telegram-bot 包的 Telegram 聊天机器人,该包带有内联键盘,用于链接到搜索部分。我不熟悉内联键盘。目前以某种方式我发现我需要分离一个触发键盘的函数、一个获取内联键盘回复的函数和一个运行搜索的函数。
对于触发键盘的功能,我想出如下:
def refSearch_handler(update, context):
reply_markup = InlineKeyboardMarkup([[
InlineKeyboardButton("Search by name", callback_data="search_name"),
InlineKeyboardButton("Search by function", callback_data="search_function")]])
bot.send_message(update.message.chat.id, "Please select:", reply_to_message_id = update.message.message_id,
reply_markup = reply_markup)
对于从内联键盘获取回复并引导用户输入关键字的功能,我计算如下:
def getClickButtonData(update, context):
if update.callback_query.data == "search_name":
update.callback_query.edit_message_text("Please input the name: ")
if update.callback_query.data == "search_function":
update.callback_query.edit_message_text("Please input the keyword of the function: ")
我知道我需要添加一个对话处理程序,其状态如下所示(来自 python-telegram-bot 包的demo),我认为我应该将它放在 main() 中其他调度员:
conv_handler = ConversationHandler(
entry_points=[CommandHandler('start', start)],
states={
CHOOSING: [MessageHandler(Filters.regex('^(Age|Favourite colour|Number of siblings)$'),
regular_choice),
MessageHandler(Filters.regex('^Something else...$'),
custom_choice)
],
TYPING_CHOICE: [MessageHandler(Filters.text,
regular_choice)
],
TYPING_REPLY: [MessageHandler(Filters.text,
received_information),
],
},
fallbacks=[MessageHandler(Filters.regex('^Done$'), done)]
)
但我不知道如何设置 conv_handler 以指向内联键盘并链接到搜索功能?不知道有没有人有经验可以指导一下?
非常感谢!
【问题讨论】:
标签: python telegram telegram-bot python-telegram-bot