Search code examples
telegramtelegram-botpython-telegram-bot

Where does the inLineKeyboardButton respond when the user clicks on it


enter def start(update: Update, context: CallbackContext) -> None:
"""Sends a message with three inline buttons attached."""
keyboard = [
    [
        InlineKeyboardButton("Option 1", callback_data='1'),
        InlineKeyboardButton("Option 2", callback_data='2'),
    ],
    [InlineKeyboardButton("Option 3", callback_data='3')],
]

reply_markup = InlineKeyboardMarkup(keyboard)

update.message.reply_text('Please choose:', reply_markup=reply_markup) here

I know that 'Option 1' is the name that the button displays and callback_data is the data that is called back in, but · I don't know where this InlineKeyboardButton will respond to the pressed event,Or in which function will this callback_data be passed to be executed?


Solution

  • You need to use handler to get callback data

    updater = Updater(token, use_context=True)
    dp = updater.dispatcher
    dp.add_handler(CallbackQueryHandler(c_back_respons))
    

    then you can use function like this

    def c_back_respons(update: Update, context: CallbackContext):
        call_back_data = update.callback_query.data
        if call_back_data in ("1"):
            function(update)