【问题标题】:How to set the inline keyboard in Telegram chatbot to other function in python-telegram-bot?如何将 Telegram 聊天机器人中的内联键盘设置为 python-telegram-bot 中的其他功能?
【发布时间】: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


    【解决方案1】:

    我不确定这是否是你想要的,但是;

    想象这是你的机器人:

    def bot(update: Update, context: CallbackContext) -> None:
    
    update.message.reply_text("Beginning of inline keyboard")
    
    keyboard = [
        [
            InlineKeyboardButton("Button 1", callback_data='1'),
            InlineKeyboardButton("Button 2", callback_data='2'),
        ]
    ]
    
    reply_markup = InlineKeyboardMarkup(keyboard)
    update.message.reply_text("Replying to text", reply_markup=reply_markup)
    

    您的按钮功能应如下所示:

    def button(update: Update, context: CallbackContext) -> None:
        query = update.callback_query
        query.answer()
        
        # This will define which button the user tapped on (from what you assigned to "callback_data". As I assigned them "1" and "2"):
        choice = query.data
        
        # Now u can define what choice ("callback_data") do what like this:
        if choose == '1':
            func1()
    
        if choose == '2':
            func2()
    

    在您启动机器人的主要功能中,您应该添加以下内容:

    updater.dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, bot))
    updater.dispatcher.add_handler(CallbackQueryHandler(button))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-18
      • 2016-10-20
      • 2017-06-02
      • 1970-01-01
      • 1970-01-01
      • 2021-06-20
      • 1970-01-01
      相关资源
      最近更新 更多