Telegram bot inline keyboard callbacks not working properly

I’m having trouble with my Telegram bot. The inline keyboard buttons show up fine, but when users click them, the callback handlers don’t seem to work. I’ve set up the callback data correctly but the bot doesn’t respond to button clicks.

async def handle_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    """Handle inline keyboard button clicks"""
    query = update.callback_query
    await query.answer()
    
    if query.data == 'menu1':
        options = [
            [InlineKeyboardButton("How do I earn points?", callback_data="info1")],
            [InlineKeyboardButton("How to update my phone number?", callback_data="info2")],
            [InlineKeyboardButton("Card not working at terminal?", callback_data="info3")],
            [InlineKeyboardButton("Points not accumulating?", callback_data="info4")],
            [InlineKeyboardButton("Lost or damaged card?", callback_data="info5")]
        ]
        
        markup = InlineKeyboardMarkup(options)
        await query.edit_message_text('FAQ Section:', reply_markup=markup)
        
        if query.data == 'info1':
            print(query.data)
            back_btn = [[InlineKeyboardButton("Back to Menu", callback_data="home")]]
            markup = InlineKeyboardMarkup(back_btn)
            await query.edit_message_text('Points are earned with every purchase using your loyalty card. Each point equals 1 dollar discount when redeemed.', reply_markup=markup)
        
        if query.data == 'info2':
            print(query.data)
            back_btn = [[InlineKeyboardButton("Back to Menu", callback_data="home")]]
            markup = InlineKeyboardMarkup(back_btn)
            await query.edit_message_text('Please contact our support team at 1-800-SUPPORT to update your phone number.', reply_markup=markup)
    
    if query.data == 'menu2':
        print('menu2 selected')
        await query.edit_message_text('For all inquiries, please call our hotline at 1-800-HELP-NOW (toll-free)')

I expected the buttons for “How do I earn points?” and “How to update my phone number?” to trigger their respective handlers, but the code never enters the if query.data == 'info1': or if query.data == 'info2': blocks. Any help would be great!

yeah your nesting the if statements wrong dude. the info1/info2 checks are inside the menu1 block so they’ll never trigger since query.data is ‘menu1’ not ‘info1’. just move those if statements outside at the same indentation level as the menu1 check and it should work fine

Looking at your code, the problem stems from nested conditional statements that can never be reached. When a user clicks the ‘menu1’ button, the callback handler processes query.data == 'menu1', displays the FAQ keyboard, and then immediately looks for ‘info1’ or ‘info2’ within that same execution cycle. Since query.data is still ‘menu1’ at that moment, those nested conditions will never evaluate to true. The solution is to flatten your conditional structure so each callback data value has its own top-level condition. Remove the ‘info1’ and ‘info2’ checks from inside the ‘menu1’ block and place them at the root level of your function alongside the existing ‘menu1’ and ‘menu2’ conditions. This way, when users subsequently click the FAQ buttons, your handler will properly catch and process those specific callback values.

The issue is with your conditional logic structure. When query.data equals ‘menu1’, you’re creating the new keyboard and editing the message, but then immediately checking for ‘info1’ and ‘info2’ in the same callback execution. However, at that point query.data is still ‘menu1’, not ‘info1’ or ‘info2’. You need to restructure your handler to have separate top-level conditions for each callback data value. Move the ‘info1’ and ‘info2’ checks out of the ‘menu1’ block and make them independent conditions at the same level as your ‘menu1’ and ‘menu2’ checks. This way, when users click the FAQ buttons, the handler will properly match their callback data values and execute the correct response logic.