How to set the inline keyboard in Telegram chatbot to other function in python-telegram-bot?

Viewed 9987

I am building a Telegram chatbot with python-telegram-bot package with inline keyboard that to link to the searching parts. I am not familiar with inline keyboard. At the moment in some way I figure out I need to separate to a function to trigger the keyboard, a function to get the inline keyboard reply and a function to run the searching.

For the function to trigger the keyboard, I figure out as follow:

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)

For the function to get the reply from inline keyboard and lead the user to input keyword, I figure out as follow:

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: ")

I know I need to add a conversation hander with the states which is something like below (this is from the demo of the python-telegram-bot package) and I think I should put it in the main() with other dispatchers:

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)]
)

But I can't figure out how to set up the conv_handler so to point to the inline keyboard and link to the searching function? Just wonder if anyone has experience can guide me?

Many thanks!

1 Answers

I'm not sure if this is what u want but;

Imagine this is your bot:

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)

Your button function should look like this:

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()

And in your main function where you start your bot, you should add these:

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