Сделай просто обновление текущего сообщения вместо отправки нового при нажатии на кнопки "влево" и "вправо", а также сохраняй и извлекай текущую страницу для пользователя.
Как-то так:
from aiogram.types import InlineKeyboardButton, InlineKeyboardMarkup
from aiogram import types
def create_pagination_buttons(current_page, max_page, chunks):
buttons = InlineKeyboardMarkup()
for a in chunks[current_page]:
buttons.add(InlineKeyboardButton(text=a['shortName'], callback_data=f"school-{a['id']}"))
buttons.add(InlineKeyboardButton(text='<', callback_data=f'to-left-{max_page}'),
InlineKeyboardButton(text=f'{current_page + 1}/{max_page}', callback_data='count'),
InlineKeyboardButton(text='>', callback_data=f'to-right-{max_page}'))
buttons.add(InlineKeyboardButton(text='Поменять город', callback_data='choose_city'))
return buttons
async def send_school_list(message: types.Message, r_json, current_page=0):
chunk_size = 15
chunks = [r_json[i:i + chunk_size] for i in range(0, len(r_json), chunk_size)]
max_page = len(chunks)
buttons = create_pagination_buttons(current_page, max_page, chunks)
await message.answer('Выберите школу:', reply_markup=buttons)
@dp.callback_query_handler(lambda c: re.search('to-', c.data))
async def to_page(call: types.CallbackQuery):
chat_id = call.message.chat.id
await call.answer()
direction = call.data.split("-")[1]
max_page = int(call.data.split("-")[2])
current_page = int(db.get_current_page(chat_id))
if direction == 'left':
current_page = (current_page - 1) % max_page
elif direction == 'right':
current_page = (current_page + 1) % max_page
db.set_current_page(chat_id, current_page)
r_json = reqtest.get_schools(449)
chunk_size = 15
chunks = [r_json[i:i + chunk_size] for i in range(0, len(r_json), chunk_size)]
buttons = create_pagination_buttons(current_page, max_page, chunks)
await call.message.edit_reply_markup(reply_markup=buttons)