P
P
Python Newbie2021-10-25 08:58:33
Python
Python Newbie, 2021-10-25 08:58:33

How to get text on inline button in pytelegrambotapi?

Hello, how do I get the text written on an inline button in pytelegrambotapi?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jerwright, 2021-10-25
@Levman5

Everything depends on the task. If you need the text of the button that the user clicked on, then the code will be as follows (in this case, the button must have the callback_data parameter , otherwise we will not be able to get the unique name of the button):

@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
    if call.message:
        for row in call.message.json['reply_markup']['inline_keyboard']:
            if call.data==row[0]['callback_data']:
                print(f'Текст на нажатой кнопке: {row[0]["text"]}')

If you need the text of each message button, then the condition with call.data does not make sense:
@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
    if call.message:
        for row in call.message.json['reply_markup']['inline_keyboard']:
            print(row[0]['text'])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question