L
L
Loknut2022-02-23 19:43:58
Python
Loknut, 2022-02-23 19:43:58

How to make scrolling in telegram bot?

How to make scrolling in python telegram bot, TelegramBotAPI library?
That is, the text is displayed to the user, and in order to display the next one, the user must press the arrow on the inline keyboard. You can also return to the previous text. That is the usual turning of pages. And if possible, an example code.

I paint through if.

def baby(message):
    if message.chat.type == 'private':
            if message.text == 'Культурный отдых \N{Dancer}':
                bot.send_photo(message.chat.id, open('imagine/41.jpg', 'rb'), caption='\N{Potted Plant} Забайкальский ботанический сад.')

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Musickk, 2022-02-23
@Musickk

this is not the same at all, inline buttons are written completely wrong, I advise you to read the pytelegrambotAPI documentation (because the answer on Habré cannot be compared with the official telebot documentation)

S
soremix, 2022-02-24
@SoreMix

Determine the size of the text in one message, let it be 10 characters. Suppose the text is in a variable text. Strings support slices , and we use them. In the first message, the first 10 characters are sent:
text[:10]. Two inline buttons cling to the message, well, for the first page, you can just one. You can use any convenient value in callback_data, the main thing is to make it clear which slice to take. Maybe just numbers. For the first page, the callback_data will have 2. In the button click handler, call the Message editing method, pass a new slice as text, where the cut from is the value chunk * digit of the date callback, the slice is BEFORE chunk + 1 * digit, the keyboard is also edited to the message , where the number in the button of the previous page becomes equal to the current one, and in the next one +1. In short, just cuts and that's it.

@bot.callback_query_handler(func=lambda call: True)
def callback_query(call):

    page = int(call.data)

    f = (page-1)*chunk
    s = page*chunk

    kb = types.InlineKeyboardMarkup()
    kb.add(types.InlineKeyboardButton('<', callback_data=page), types.InlineKeyboardButton('>', callback_data=page+1))


    bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.id, text=text[f:s], reply_markup=kb)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question