Как реализовать пагинацию для телеграм-бота?

На днях появилась задача - реализовать переключение карточек с текстами в телеграмм-боте (по принципу "Одна страничка-один текст). Наткнулся на статью с Хабра, где был задействована библиотека "telegram-bot-pagination". Здесь приведен код, где я пытался реализовать этот подход:

    import telebot
from telebot import types
from telegram_bot_pagination import InlineKeyboardPaginator
token = '5дпрллвлдпщшыопщпвщагпщшвщапващпщвапвлапооварповадопровар'
bot = telebot.TeleBot(token)
@bot.message_handler(commands = ['start'])
def start(message):
    markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
    item1 = types.KeyboardButton("EN")
    item2 = types.KeyboardButton("RU")
    item3 = types.KeyboardButton("DE")
    item4 = types.KeyboardButton("UA")
    markup.add(item1,item2, item3,item4)
    bot.send_message(message.chat.id,text="start",reply_markup=markup)
@bot.message_handler(content_types=['text'])
def EN(message):
    if (message.text == "EN"):
        markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
        item11 = types.KeyboardButton("Choose category")
        item12 = types.KeyboardButton("FAQ")
        item13 = types.KeyboardButton("About")
        item14 = types.KeyboardButton("Payment instructions")
        item15 = types.KeyboardButton("Back")
        markup.add(item11,item12,item13,item14,item15)
        bot.send_message(message.chat.id,text = "EN", reply_markup=markup)
    elif (message.text == "Choose category"):
        markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
        item111 = types.KeyboardButton("History")
        item112 = types.KeyboardButton("Science")
        item113 = types.KeyboardButton("Arts")
        item114 = types.KeyboardButton("Back")
        markup.add(item111,item112,item113,item114)
        bot.send_message(message.chat.id,text = "Choose category", reply_markup=markup)
    elif (message.text == "FAQ"):
        markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
        item122 = types.KeyboardButton("Back")
        markup.add(item122) 
        a = open("F.A.Q.txt")
        text = a.read()
        bot.send_message(message.chat.id,text,reply_markup=markup)
    elif (message.text == "Payment instructions"):
        markup = types.InlineKeyboardMarkup()
        item141 = types.InlineKeyboardButton("Binance", url = 'https://www.binance.com/en/support/faq/c-2?navId=2')
        item142 = types.InlineKeyboardButton("BlockChain",url = 'https://support.blockchain.com/hc/en-us/sections/4517566823060-Deposits-and-Withdrawals')
        item143 = types.InlineKeyboardButton("TrustWallet",url = 'https://community.trustwallet.com/t/how-to-create-a-payment-request/23332')
        markup.add(item141,item142,item143)
        bot.send_message(message.chat.id,text = "Choose your Wallet", reply_markup=markup)
    elif (message.text == "Back"):
        markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
        item11 = types.KeyboardButton("Choose category")
        item12 = types.KeyboardButton("FAQ")
        item13 = types.KeyboardButton("About")
        item14 = types.KeyboardButton("Payment instructions")
        item15 = types.KeyboardButton("Back")
        markup.add(item11,item12,item13,item14,item15)
        bot.send_message(message.chat.id,text = "You returned to the menu", reply_markup=markup)
    else:
        RU(message)

Вот код, который был написан для проверки работы библиотеки:

import telebot
from telebot import types
from telegram_bot_pagination import InlineKeyboardPaginator
from texts import arr
token = 'дваопщвгашщпвапвдапукзщелудрпшв'
bot = telebot.TeleBot(token)
@bot.message_handler(func=lambda message:True)
def get_text(message):
    send_text_page(message)
@bot.callback_query_handler(func=lambda call: call.texts.split('#')[0]=='text')
def text_page_callback(call):
    page = int(call.texts.split('#')[1])
    bot.delete_message(call.message.chat.id,call.message.message_id)
    send_text_page(call.message,page)
def send_text_page(message,page=1):
    paginator = InlineKeyboardPaginator(len(arr),current_page=page,data_pattern='text#{page}')
    paginator.add_before(types.InlineKeyboardButton('Like',callback_data='like#{}'.format(page)),types.InlineKeyboardButton('Dislike',callback_data='dislike#{}'.format(page)))
    paginator.add_after(types.InlineKeyboardButton('Go Back',callback_data='back'))
    bot.send_message(message.chat.id,arr[page-1],reply_markup=paginator.markup)
bot.polling()

После запуска кода в телеграмм-боте в консоли интерпретатор выдал следующую ошибку:

Traceback (most recent call last): File "c:/Users/admin/Desktop/Python/bot.py", line 5, in import cards_en File "c:\Users\admin\Desktop\Python\cards_en.py", line 20, in bot.polling() File "C:\Program Files\Python38-32\lib\site-packages\telebot_init_.py", line 658, in polling self._threaded_polling(non_stop, interval, timeout, long_polling_timeout, allowed_updates) File "C:\Program Files\Python38-32\lib\site-packages\telebot_init.py", line 720, in _threaded_polling raise e File "C:\Program Files\Python38-32\lib\site-packages\telebot_init.py", line 679, in _threaded_polling polling_thread.raise_exceptions() File "C:\Program Files\Python38-32\lib\site-packages\telebot\util.py", line 105, in raise_exceptions raise self.exception_info File "C:\Program Files\Python38-32\lib\site-packages\telebot\util.py", line 87, in run task(*args, **kwargs) File "C:\Program Files\Python38-32\lib\site-packages\telebot_init.py", line 410, in retrieve_updates self.process_new_updates(updates) File "C:\Program Files\Python38-32\lib\site-packages\telebot_init.py", line 502, in process_new_updates self.process_new_callback_query(new_callback_queries) File "C:\Program Files\Python38-32\lib\site-packages\telebot_init.py", line 540, in process_new_callback_query self.notify_command_handlers(self.callback_query_handlers, new_callback_querys) File "C:\Program Files\Python38-32\lib\site-packages\telebot_init.py", line 3450, in _notify_command_handlers if self.test_message_handler(message_handler, message): File "C:\Program Files\Python38-32\lib\site-packages\telebot_init.py", line 3392, in _test_message_handler if not self.test_filter(message_filter, filter_value, message): File "C:\Program Files\Python38-32\lib\site-packages\telebot_init.py", line 3421, in _test_filter return filter_value(message) File "c:\Users\admin\Desktop\Python\cards_en.py", line 10, in @bot.callback_query_handler(func=lambda call: call.texts.split('#')[0]=='text') AttributeError: 'CallbackQuery' object has no attribute 'texts'

Вопрос: как реализовать пагинацию для кода, приведенного в начале?


Ответы (0 шт):