A
A
AlmazKayum2018-03-06 23:25:28
Python
AlmazKayum, 2018-03-06 23:25:28

How to properly organize the work of callback buttons?

Hello!
I use the PyTelegramBotApi library
The database has a list of URLs, which is constantly updated, the number of addresses increases accordingly. It is necessary to make it convenient to view the URL in the telegram bot.
Task: it is
necessary to organize the work of callback buttons in such a way that when you click on the next_url button, the bot changes the content of the message in accordance with a specific URL.
5a9ef61bcf3a9478608563.jpeg
I hope I explained clearly.
That is, you press the next_url button -> the content of the message changes to the next URL, the url of the get_url button changes, etc.
There are ideas how to do this with a constantly changing number of URLs, because the number of callback_data also changes.
Here is a simple code for discussion:

@bot.message_handler(func=lambda message: message.text == 'message')
def idea(message):
    markup = types.InlineKeyboardMarkup()
    k1 = types.InlineKeyboardButton(text='get_url', url='http://url.ru')
    k2 = types.InlineKeyboardButton(text='prev_url', callback_data='1')
    k3 = types.InlineKeyboardButton(text='next_url', callback_data='2')
    markup.add(k1)
    markup.add(k2, k3)
    bot.send_message(text='URL Title', chat_id=message.chat.id, reply_markup=markup)

Please help, I've been sitting with red eyes for several hours now. Write please, who has already solved a similar problem. In what direction should we move?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
arudmin, 2018-03-09
@arudmin

Catch button presses with this handler
@bot.callback_query_handler(func=lambda message: hasattr(message, 'data'))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question