A
A
artgin2021-09-27 10:42:52
Telegram
artgin, 2021-09-27 10:42:52

Python (aiogram): How to make multiple scrolling pages of inline buttons?

there are more than 100 inline buttons. For a better perception, I want to make something like pages of inline buttons with the addition of forward (Next) and back (Back) buttons to each, respectively, so that you can scroll through these pages forward and backward. I couldn't find the answer anywhere and where I could read the guide and so on...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
Fallerwood, 2021-09-27
@Fallerwood

On aiogram I will not suggest a specific solution. But how would I do on the example of pytelegrambotapi.
I think the structure will be clear, and you can do it on aiogram.
Dictionary containing user_id : Page on which the
user_page = dict
list of inline buttons is located
buttons = list
For convenience, a separate function for the keyboard, where we pass user_id

def inline_keyboard(user_id):
    markup = InlineKeyboardMarkup()
    # Достаем страницу. Будет идти с 0
    page = user_page[user_id]
    # Кол-во кнопок которые будем менять, по сути индекс. 
    step = 2
    for i in range(page, page+2):
        markup.add(buttons[i+]
    btn_next = InlineKeyboardButton(text='>', callback_data = 'next')
    btn_back = InlineKeyboardButton(text='<', callback_data = 'back')
    return markup

Accordingly, in the processing of callbacks on next, we add step to the page, and subtract it on back.
After, we edit an already existing message.
We write a check so that the user cannot go beyond the array.
We call where it is necessary
reply_markup = inline_keyboard(message.user_id)
Everything should be simple. Good luck!

J
Jan, 2021-10-07
@Buchachalo

Two ways.
1) Through fsm. And through the handlers we catch the desired state. You can fasten the button generator and the context to send callbacks that will hang on the buttons and the stage.
2) Well, if the keyboards have already been created, then give each keyboard a unique identifier and, again, pass this identifier in the callback. Write a function that catches all callbacks through the handler, looks at the identifier inside and calls the desired function by identifier.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question