Просто выбор элементов по индексу
import telebot
from telebot import types
import re
items = ['A el', 'B el', 'C el', 'D el']
def generate_markup(pages_count, current_page=0):
markup = types.InlineKeyboardMarkup()
for i in range(pages_count):
markup.add(types.InlineKeyboardButton(i+1, callback_data='page_{}'.format(i)))
btn_back = types.InlineKeyboardButton('Назад', callback_data='page_{}'.format(current_page-1))
btn_forward = types.InlineKeyboardButton('Вперед', callback_data='page_{}'.format(current_page+1))
if current_page == pages_count-1:
markup.add(btn_back)
elif current_page == 0:
markup.add(btn_forward)
else:
markup.row(btn_back, btn_forward)
return markup
@bot.message_handler(commands=['start'])
def send_start(message):
bot.send_message(message.chat.id, items[0], reply_markup=generate_markup(len(items)))
@bot.callback_query_handler(func = lambda call: re.match(r'page_\d+$', call.data))
def callback_query(call):
page = int(re.match(r'page_(\d+)$', call.data).group(1))
bot.edit_message_text(items[page], call.message.chat.id, call.message.message_id, reply_markup=generate_markup(len(items), page))