I
I
i_ikigai2020-08-05 00:53:26
Python
i_ikigai, 2020-08-05 00:53:26

There is a list of films, how to display a button in telegram every 10, 20, 30, etc. films, aiogram?

When you click on the "all" button, buttons come out, like 1 will be the first 10 films, 2 20 films, etc.

@dp.callback_query_handler(lambda c: c.data == 'все')
async def callback_inline(callback_query: types.CallbackQuery):
    await bot.answer_callback_query(callback_query.id)
    key1 = InlineKeyboardMarkup() # тут будут кнопки в зависимости от колва фильмов
    for key in handlers.user_films_all():
        rele1 = InlineKeyboardButton(text=f"{key}", callback_data=f"{key}")
        key1.row(rele1)
    await bot.edit_message_text(chat_id=callback_query.from_user.id, message_id=callback_query.message.message_id,
                                text=f'На цифраx будут фильмы', reply_markup=key1)

Here I create a dictionary with a "number" and a value - a list of movies
def user_films_all():
    db = sql.connect('TGusers_films.db')
    cur = db.cursor()
    array = list(cur.execute('SELECT name_film, year_films FROM user_films'))
    result = list(chunks(3, array))# для примера тут по 3
    ord_dict = OrderedDict()
    for i in enumerate(result, 1):
        ord_dict[f'{i[0]}'] = i[1]

    return ord_dict

This is how you need to write code so that it picks up numbers from callback_data and displays a list of movies and not write for numbers manually?
@dp.callback_query_handler(lambda c: c.data == '1') # потом 2, 3, 4 и тд
async def process_callback_button1(callback_query: types.CallbackQuery):
    number = callback_query.data
    await bot.answer_callback_query(callback_query.id)
    keys = InlineKeyboardMarkup(resize_keyboard=True)
    for key in handlers.user_films_all().keys():
        rele1 = InlineKeyboardButton(text=f"{key}", callback_data=f"{key}")
        keys.row(rele1)
    await bot.edit_message_text(chat_id=callback_query.from_user.id, message_id=callback_query.message.message_id,
                                text=f'{handlers.nubmer_exit(number)}',
                                reply_markup=keys)

that is, so that it would not be necessary to write a piece of code for each callback?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
i_ikigai, 2020-08-11
@i_ikigai

I don't know if it's the right way to write it, but it works

amount_keys = handlers.col()  # количество кнопок


@dp.callback_query_handler(lambda c: f"{amount_keys}")
async def process_callback_button1(callback_query: types.CallbackQuery):
    number = callback_query.data
    await bot.answer_callback_query(callback_query.id)
    keys = InlineKeyboardMarkup(resize_keyboard=True)
    for key in handlers.user_films_all().keys():
        number_button = InlineKeyboardButton(text=f"{key}", callback_data=f"{key}")
        keys.insert(number_button)
    await bot.edit_message_text(chat_id=callback_query.from_user.id, message_id=callback_query.message.message_id,
                                text=f'{handlers.nubmer_exit(number)}',
                                reply_markup=keys)

Function for number of buttons
def col():
    s = len(user_films_all())
    o = 'or'
    res = ["c.data == '1'"]


    for i in range(2, s+1):
        if i == s:
            res.append(o)
            res.append(f'c.data = "{i}"')
        else:
            res.append(o)
            res.append(f'c.data = "{i}"')
    r = " ".join(res)
    return r

N
Nick, 2020-08-05
@c00re

You can put movies in the list, after pressing the button you get a callback and make a cut. 5f2abfcae54f7435040725.png
This is a screenshot from my bot. If a person clicks on 10, then I do and get a list of the first 10 films
listOfFilms = [:10] #Пример

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question