S
S
silmar132020-09-26 11:48:49
Python
silmar13, 2020-09-26 11:48:49

InlineKeyboard. How to catch and pass the value of the button when clicked?

Please do not scold too much. I make the first bot and got stuck in one place.
There is a dictionary, the key (city) should be a button. Depending on which button (key) was pressed, the bot should send a value from the dictionary. But I can't figure out how exactly to get the value of the selected button.
Actually the question is: how to get the value of the selected button (name_project_btn) and pass it to the next handler?

data = {'Москва': {'1546949': 211.0}}

# Создание кнопок
@bot.message_handler(commands=['start'])
def start_programm(message):
    keyboard = telebot.types.InlineKeyboardMarkup()
    for name_project_btn in data:
        key = telebot.types.InlineKeyboardButton(
            text=name_project_btn, callback_data='get_mark')
        keyboard.add(key)

    bot.send_message(message.from_user.id, text='Выберите проект',
                     reply_markup=keyboard)

# Получение значения копки(ключа словаря)
@bot.callback_query_handler(func=lambda call: True)
def callback_btn(call):
    if call.data == 'get_mark':
        bot.answer_callback_query(call.id, text="Проект выбран")


# Получение ответа(баллов)
@bot.message_handler(content_types=['text'])
def get_id(id_msg):
    ids = int(id_msg.text)
    ans = f"Количество баллов - {data['Москва'][ids]}"
    bot.send_message(id_msg.chat.id, ans)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kirillinyakin, 2020-09-26
@kirillinyakin

@bot.callback_query_handler(func=lambda call: call.data == 'get_mark') 
def callback_btn(call): 
    answer = str(data[call.text])
    bot.answer_callback_query(call.id, text=answer)

I hope I understood you correctly

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question