L
L
lous_qween2022-03-10 11:11:54
Python
lous_qween, 2022-03-10 11:11:54

TelegramBotAPI Urgent! The dictionary changes during pagination. What to do?

The code:

import telebot
import config

from telebot import types

from telegram_bot_pagination import InlineKeyboardPaginator

place1_pages = [ 
 {"id": "1", "path": "imagine/4.jpg"},
 {"id": '2', "path": "imagine/16.jpg"},
]

list_pages = [ 
 {"id": '23"', "path": "imagine/6.jpg"},
 {"id": '45', "path": "imagine/11.jpg"}, 
 {"id": '67', "path": "imagine/13.jpg"},
 {"id": '89', "path": "imagine/15.jpg"},
 {"id": '49', "path": "imagine/18.jpg"},
 {"id": '39', "path": "imagine/28.jpg"},
 {"id": '12', "path": "imagine/25.jpg"},
 {"id": '90', "path": "imagine/40.jpg"},
]

bot = telebot.TeleBot(config.TOKEN)
def send_list_page(message, page=0):
 paginator = InlineKeyboardPaginator( 
  len(list_pages),
  current_page=page, 
  data_pattern="page#{page}" 
 ) 
 
 if page != 0: 
  page -= 1 
 
 with open(list_pages[page]["path"], "rb") as file: 
  data = file.read() 
 
 bot.send_photo(message.chat.id, data, caption=list_pages[page]["id"], reply_markup=paginator.markup, parse_mode="Markdown") 

@bot.message_handler(commands=['list'])
def listik(message):
    send_list_page(message)

@bot.callback_query_handler(func=lambda call: True) 
def list_page_callback(call): 
 bot.delete_message( 
  call.message.chat.id, 
  call.message.message_id 
 ) 
 
 page_number = int(call.data.split("#")[1]) 
 send_list_page(call.message, page_number)

def send_place1_page(message, page=0): 
 paginator = InlineKeyboardPaginator( 
  len(place1_pages),
  current_page=page, 
  data_pattern="page#{page}" 
 ) 
 
 if page != 0: 
  page -= 1 
 
 with open(place1_pages[page]["path"], "rb") as file: 
  data = file.read() 
 
 bot.send_photo(message.chat.id, data, caption=place1_pages[page]["id"], reply_markup=paginator.markup, parse_mode="Markdown")

def place1(message):
    send_place1_page(message)
 
@bot.callback_query_handler(func=lambda call: True) 
def places_page_callback(call): 
 bot.delete_message( 
  call.message.chat.id, 
  call.message.message_id 
 ) 
 
 page_number = int(call.data.split("#")[1]) 
 send_place1_page(call.message, page_number)

@bot.message_handler(commands=['start']) 
def start (message):
    send_place1_page(message)

bot.polling()

A person sends a command and he receives a picture in response, which can be scrolled through inline with the buttons and thereby changed to another. The fact is that the list command works as it should, and the start command is crooked - at first the required number of pages and, accordingly, the necessary photos come out, but when the button is pressed, the dictionary is replaced with a dictionary for the start command. How to fix?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question