Answer the question
In order to leave comments, you need to log in
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 см',
]
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)
@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
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)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question