B
B
balak_in2021-04-12 22:36:19
Python
balak_in, 2021-04-12 22:36:19

How to understand where the command came from in the telegram bot?

Hello everyone, I couldn't find the answer in Google, so I came here.

Bottom line: I'm trying to create a bot for an online store using tlebot. Screwed the keyboard to the product:

def last_choise():
    choise_key = types.InlineKeyboardMarkup(row_width=1)
    c1 = types.InlineKeyboardButton(text='Добавить в корзину', callback_data='add_in_basket')
    c2 = types.InlineKeyboardButton(text='Корзина', callback_data='basket')
    c3 = types.InlineKeyboardButton(text='Назад', callback_data='back1')

    choise_key.add(c1, c2, c3)
    return choise_key

Wrote the button handler " Add to cart: "
elif call.data == 'add_in_basket':
                bot.answer_callback_query(callback_query_id=call.id,
                                          text="Товар добавлен в корзину", show_alert=True)
                bot.delete_message(call.message.chat.id, call.message.message_id, timeout=10)

Kopka replies, everything is ok.

Question:
How can I understand from which product (the keyboard is screwed to each of the 10 products conditionally) this button was pressed to add the product to the basket?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
shurshur, 2021-04-13
@balak_in

Pass the necessary information to callback_data. For example, you can pass add: an article, which is parsed in the handler and determines what exactly to add.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question