V
V
violpeople2021-06-01 19:51:48
Python
violpeople, 2021-06-01 19:51:48

How to change the value of a variable on a button click in a bot?

There is a tuple in which the data is:

items = [
    'Атомик 60|70|80 см', 
    'Болевар 60|70|80 см',
]

Below is a function for handling a request from a category:
current_item = items[0]
@dp.callback_query_handler(text= 'Ecuador')
async def ecuador(call:types.CallbackQuery):
    kb = InlineKeyboardMarkup(row_width=2)
    
    next = InlineKeyboardButton(text= 'Следующий',callback_data= 'next')

    kb.add(next)

    await bot.send_photo(
    chat_id,
    photo = current_photo,
    caption='Название: '+ current_item,
    reply_markup=kb)

Then the "Next" request is processed, in which the next item from the tuple must be called ( this should also be constantly repeated cyclically until the end of the tuple! )
@dp.callback_query_handler(text= 'next')
async def row(call:types.CallbackQuery):
    kb = InlineKeyboardMarkup(row_width=2)
    
    next =InlineKeyboardButton(text= 'Следующий',callback_data= 'next')

    kb.add(next)
    await bot.send_photo(
        chat_id,
        photo = current_photo[+1],
        caption ='Название: '+ current_item[+1],
        reply_markup= kb)

caption ='Title: '+ current_item[+1]
I thought about doing this, but it didn't work out. Please help me how to do this and loop this thing

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-06-01
@violpeople

Make an index and pass it to callback_data, this is the parameter for this and is needed.

@dp.callback_query_handler(text= 'Ecuador')
async def ecuador(call:types.CallbackQuery):
    kb = InlineKeyboardMarkup(row_width=2)
    
    next = InlineKeyboardButton(text= 'Следующий',callback_data= 'next_1')

    kb.add(next)

    await bot.send_photo(
    chat_id,
    photo = current_photo,
    caption='Название: '+ current_item,
    reply_markup=kb)


@dp.callback_query_handler(lambda c: c.data.startswith('next_'))
async def send_next_item(call: types.CallbackQuery):
    next_index = int(call.data.split('_')[-1])
    kb = InlineKeyboardMarkup(row_width=2)
    next = InlineKeyboardButton(text = 'Следующий',  callback_data = f'next_{next_index+1}')
    kb.add(next)
    await bot.send_photo(
        chat_id,
        photo = items[next_index],
        caption ='Название: '+ items[next_index],
        reply_markup= kb)

Plus, don't forget to check if the index matches the length of the list, but it's still a list, not a tuple.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question