L
L
lifiij2021-11-30 13:32:15
Python
lifiij, 2021-11-30 13:32:15

How to pass a list of buttons in the aiogram library to the add function?

@dp.message_handler( filters.CommandStart() )
async def start( message: types.Message ):
    await message.answer(
        'hi', 
        reply_markup = 
            InlineKeyboardMarkup(row_width=1).add(
                [InlineKeyboardButton(text = "Apples", callback_data='1'),
                InlineKeyboardButton(text = "Oranges", callback_data='2'),
                InlineKeyboardButton(text = "Beans", callback_data='3')]
            )
    )


TypeError: aiogram.types.inline_keyboard.InlineKeyboardButton object at 0x000001A3CB035610 is not JSON serializable

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
LXSTVAYNE, 2021-11-30
@lxstvayne

You need to unpack your list, or simply pass it there separated by commas.

@dp.message_handler( filters.CommandStart() )
async def start( message: types.Message ):
    await message.answer(
        'hi', 
        reply_markup = 
            InlineKeyboardMarkup(row_width=1).add(
                *[InlineKeyboardButton(text = "Apples", callback_='1'),
                InlineKeyboardButton(text = "Oranges", callback_data='2'),
                InlineKeyboardButton(text = "Beans", callback_data='3')]
            )
    )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question