Package: telebot-inline-pagination
Pageable inline keyboard forpyTelegramBotAPI(telebot)A library for pyTelegramBotAPI that allows you to display data in the format of a pageable inline keyboard.InstallationYou can usepipto install this library.pip install telebot-inline-paginationUsageStep 1. Import necessary libraries and connect Telegram tokenfromtelebotimportTeleBotbot=TeleBot('TOKEN',parse_mode=None)# Use your Telegram tokenfromtelebot.typesimportCallbackQueryfromtelebot_inline_paginationimportKeyboardStep 2. Define the parametersRequired parametersdata: Dataset in "list of tuples" format, without headers: [(a1, b1), (a2, b2), (a3, b3)].text_message: Message displayed above the keyboard.data=[('Hartsfield-Jackson Atlanta International Airport','ATL/KATL','Atlanta, Georgia, United States'),("O'Hare International Airport",'ORD/KORD','Chicago, Illinois, United States'),('Dallas/Fort Worth International Airport','DFW/KDFW','Coppell, Euless, Grapevine, and Irving, Texas, United States'),('Denver International Airport','DEN/KDEN','Denver, Colorado, United States'),('Charlotte Douglas International Airport','CLT/KCLT','Charlotte, North Carolina, United States'),('Los Angeles International Airport','LAX/KLAX','Los Angeles, California, United States'),('Harry Reid International Airport','LAS/KLAS','Paradise, Nevada, United States'),('Phoenix International Airport','PHX/KPHX','Phoenix, Arizona, United States'),('Miami International Airport','MIA/KMIA','Miami-Dade County, Florida, United States'),('George Bush Intercontinental Airport','IAH/KIAH','Houston, Texas, United States')]text_message='Demo'Optional parametersrows_per_page: The number of rows of buttons on one page, excluding the navigation bar (by default - 5).text_index: Column index - data source for button titles (by default - 0).callback_index: Column index - data source for callback-function (by default - 0).button_text_mode: The number of the mode responsible for the format of the displayed text on the buttons:0 or None (by default) - The button displays data and has callback data from the column index 01 - The button displays only data from thetext_indexcolumn2 - The button displays data from thetext_indexcolumn, and in brackets - data from thecallback_indexcolumn3 - The button displays data from thecallback_indexcolumn, and in brackets - data from thetext_indexcolumnnext_page: Content of the button to go to the next page (by default - '--->').previous_index: Content of the button to go to the previous page (by default - '<---').TEXT_INDEX=0# in this example, full airport nameCALLBACK_INDEX=1# in this example, IATA/ICAO airport codeBUTTON_TEXT_MODE=2# in this example, "full airport name (IATA/ICAO airport code)"ROWS_PER_PAGE=3NEXT_PAGE='>'PREVIOUS_PAGE='<'Step 3. Creating the pageable keyboard instanceThe standard pageable keyboard instance looks like this:Keyboard(chat_id=message.chat.id,data=data,rows_per_page=ROWS_PER_PAGE,button_text_mode=BUTTON_TEXT_MODE,text_index=TEXT_INDEX,callback_index=CALLBACK_INDEX,next_page=NEXT_PAGE,previous_page=PREVIOUS_PAGE)In this example, the instance is written to the variablekeyboardsto ensure multithreading:keyboards=[]...json={"id":message.chat.id,"object":Keyboard(chat_id=message.chat.id,data=data,rows_per_page=rows_per_page,button_text_mode=button_text_mode,text_index=text_index,callback_index=callback_index)}keyboards.append(json)Step 4. Creating the message handler for pageable keyboardThe standard message handler for pageable keyboard looks like this:bot.send_message(message.from_user.id,text_message,reply_markup=Keyboard.send_keyboard()In this example, the keyboard calls using the/startcommand. Also,
first, when using the/startcommand, all previously created instances are deleted. Second, a new instance is added to the message handler.@bot.message_handler(commands=['start'])defdemo_pagination(message):fori,jinenumerate(keyboards):ifj["id"]==message.chat.id:delkeyboards[i]json={"id":message.chat.id,"object":Keyboard(chat_id=message.chat.id,data=data,rows_per_page=ROWS_PER_PAGE,button_text_mode=BUTTON_TEXT_MODE,text_index=TEXT_INDEX,callback_index=CALLBACK_INDEX,next_page=NEXT_PAGE,previous_page=PREVIOUS_PAGE)}keyboards.append(json)foriinkeyboards:ifi["id"]==message.chat.id:bot.send_message(message.from_user.id,text_message,reply_markup=i["object"].send_keyboard())Step 5. Creating the callback query handler for pageable keyboardThe standard callback query handler for pageable keyboard looks like this:bot.edit_message_text(text_message,reply_markup=Keyboard.edit_keyboard(call),chat_id=call.message.chat.id,message_id=call.message.message_id)In this example, we use the current chat id to find the required instance in thekeyboardslist and launch the callback query handler. Also, we use thesend_messagecommand to receive a message with information when the result button is clicked.@bot.callback_query_handler(func=lambdacall:True)defdemo_pagination_handler(call:CallbackQuery):ifcall.datain('previous_page','next_page'):foriinkeyboards:ifi["id"]==call.message.chat.id:bot.edit_message_text(text_message,reply_markup=i["object"].edit_keyboard(call),chat_id=call.message.chat.id,message_id=call.message.message_id)foriindata:ifcall.data==i[CALLBACK_INDEX]:bot.send_message(call.message.chat.id,'Airport: '+i[0]+' ('+i[1]+')'+'\n'+'Address: '+i[2])Full exampleIn this example, the code implements multithreading: the bot can be used by several people at the same time without interfering with each other.fromtelebotimportTeleBotbot=TeleBot('TOKEN',parse_mode=None)# Use your Telegram tokenfromtelebot.typesimportCallbackQueryfromtelebot_inline_pagination.keyboardimportKeyboarddata=[('Hartsfield-Jackson Atlanta International Airport','ATL/KATL','Atlanta, Georgia, United States'),("O'Hare International Airport",'ORD/KORD','Chicago, Illinois, United States'),('Dallas/Fort Worth International Airport','DFW/KDFW','Coppell, Euless, Grapevine, and Irving, Texas, United States'),('Denver International Airport','DEN/KDEN','Denver, Colorado, United States'),('Charlotte Douglas International Airport','CLT/KCLT','Charlotte, North Carolina, United States'),('Los Angeles International Airport','LAX/KLAX','Los Angeles, California, United States'),('Harry Reid International Airport','LAS/KLAS','Paradise, Nevada, United States'),('Phoenix International Airport','PHX/KPHX','Phoenix, Arizona, United States'),('Miami International Airport','MIA/KMIA','Miami-Dade County, Florida, United States'),('George Bush Intercontinental Airport','IAH/KIAH','Houston, Texas, United States')]text_message='Demo'BUTTON_TEXT_MODE=2TEXT_INDEX=0CALLBACK_INDEX=1ROWS_PER_PAGE=3NEXT_PAGE='>'PREVIOUS_PAGE='<'keyboards=[]@bot.message_handler(commands=['start'])defdemo_pagination(message):fori,jinenumerate(keyboards):ifj["id"]==message.chat.id:delkeyboards[i]json={"id":message.chat.id,"object":Keyboard(chat_id=message.chat.id,data=data,rows_per_page=ROWS_PER_PAGE,button_text_mode=BUTTON_TEXT_MODE,text_index=TEXT_INDEX,callback_index=CALLBACK_INDEX,next_page=NEXT_PAGE,previous_page=PREVIOUS_PAGE)}keyboards.append(json)foriinkeyboards:ifi["id"]==message.chat.id:bot.send_message(message.from_user.id,text_message,reply_markup=i["object"].send_keyboard())@bot.callback_query_handler(func=lambdacall:True)defdemo_pagination_handler(call:CallbackQuery):ifcall.datain('previous_page','next_page'):foriinkeyboards:ifi["id"]==call.message.chat.id:bot.edit_message_text(text_message,reply_markup=i["object"].edit_keyboard(call),chat_id=call.message.chat.id,message_id=call.message.message_id)foriindata:ifcall.data==i[CALLBACK_INDEX]:bot.send_message(call.message.chat.id,'Airport: '+i[0]+' ('+i[1]+')'+'\n'+'Address: '+i[2])bot.infinity_polling()ResultMIT License