@Alpharius_Prog

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

Недавно прочел статью о пагинации. (https://habr.com/ru/post/501812/)
Пытаюсь реализовать код из поста, но не могу понять, что именно идет не так. Код прилагается:
spoiler
import telebot
from telebot import types
from telegram_bot_pagination import InlineKeyboardPaginator
token = 'должваодлполвопдлвоалдпдукгешердггщшугшнгн'
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)
        text = """Text about TOP\-7 questions"""
        bot.send_message(message.chat.id,text,parse_mode = 'MarkdownV2',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)

Предполагалось реализовать пагинацию для категорий "Science", "Arts" и других.

Вот код, куда планировалось вставить пагинацию:
spoiler
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)
        text = """Text about TOP\-7 questions"""
        bot.send_message(message.chat.id,text,parse_mode = 'MarkdownV2',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()

Ожидалось, что после включения телеграмм-бот будет переключать тексты по принципу "Одна страница-один текст" . Но после включения и переключения на следующую страницу вылезает следующая ошибка:
spoiler
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'


Как разобраться с этой ошибкой?
  • Вопрос задан
  • 174 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы