skip to Main Content

I am developing a telegram bot in the Python programming language using the telebot library (pyTelegramBotApi). At the moment when I’m making buttons, I decided to use InlineKeyboardMarkup. The following questions arise, how to get the name of this button by clicking on the button. I need to get the index of an item in the list by the name I need to get from the button name.

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

def callback_query(call):

    if call.data == 'SUBCATEGORIES_BTN':

        sub_id = names.index(???) # Here I have to insert the name from the button name

2

Answers


  1. Chosen as BEST ANSWER

    @bot.message_handler(content_types=['text'])

    def text(message):

    if message.text = 'SUBCATEGORIES_BTN':
    
         sub_id = names.index(f'{message.text}')
    

  2. There is no such thing as a button ‘name’.

    The only value that is returned by Telgram is the data attribute (called callback_data) that you’ve provided on the button. The text the user sees on the button is not given.


    The data is available as call.data as you’re already using in your code.


    For a complete exaxmple on how to use the InlineKeyboard, consider taking a look at the
    pyTelegramBotApi Inline Keyboard Example

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search