I
I
Ilkhomjon Matazimov2020-05-19 14:06:45
Python
Ilkhomjon Matazimov, 2020-05-19 14:06:45

Why does callback_query_handler only process the last line?

I use the Aiogram library for telegram bots.
My telegram bot processes only one inline line, and it seems to me that only the last one.

Here is the code:

keyboard = types.InlineKeyboardMarkup()
foods = ['Бургер', 'Картофель', 'Куринные ножки']
for food in foods:
    inline_btns = types.InlineKeyboardButton(food, callback_data=food)
    keyboard.add(inline_btns)

    @dp.callback_query_handler(lambda c: c.data == food)
    async def process_callback(call: types.CallbackQuery):
        await bot.edit_message_text(text=f"Нажата кнопка {food}", chat_id=call.message.chat.id,
                                    message_id=call.message.message_id)
await message.answer("Выберите блюдо:", reply_markup=keyboard)

Only the last row in the array fires: "Chicken legs".
If you put it in the last place in the array, for example "Burger", then Burger will work, etc.
What the hell? What could be causing this, and how can I fix it? I'm shocked, black magic is clearly involved.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Akakiy1880, 2020-12-02
@Akakiy1880

keyboard = types.InlineKeyboardMarkup()
foods = ['Burger', 'Potato', 'Chicken Legs']
for food in foods:
inline_btns = types.InlineKeyboardButton(food, callback_data=food)
keyboard.add(inline_btns)
@dp.callback_query_handler (lambda c: c.data == food)
async def process_callback(call: types.CallbackQuery):
await bot.edit_message_text(text=f"{food} button pressed", chat_id=call.message.chat.id,
message_id= call.message.message_id)
await message.answer("Select dish:", reply_markup=keyboard)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question