I
I
Igor Mironov2021-11-17 00:31:43
Python
Igor Mironov, 2021-11-17 00:31:43

How to delete the last message of the bot by clicking on the inline button and also by clicking on the "Back" button, the bot returned its last answer?

The problem is that I can't make a good bot menu without the function of going back to the previous selection presented by inline buttons. Well, when you click on the inline button in more detail to one of the options, the previous message with the choice of actions through the inline button should be deleted.
Here is a piece of my code with all buttons inline:

@bot.callback_query_handler(lambda first: True)
def callback_inline(call):
    try:
        if call.message:
            if call.message and call.data == 'pyaterochka':
                markup1 = types.InlineKeyboardMarkup(row_width=1)
                item5 = types.InlineKeyboardButton("Штрих-коды", callback_data='shtrih1')
                item6 = types.InlineKeyboardButton(" Назад", callback_data='back')
                markup1.add(item5, item6)
                bot.send_message(call.message.chat.id, 'Выберите подкатегорию.', reply_markup=markup1)

            elif call.data == 'magnit':
                bot.send_message(call.message.chat.id, 'Выберите подкатегорию.')

            elif call.data == 'mac':
                bot.send_message(call.message.chat.id, 'Выберите подкатегорию.')

            elif call.data == 'kfc':
                bot.send_message(call.message.chat.id, 'Выберите подкатегорию.')

            elif call.data == 'shtrih1':
                markup2 = types.InlineKeyboardMarkup(row_width=1)
                item7 = types.InlineKeyboardButton("500₽ — 150.0₽", callback_data='cena1-500')
                item8 = types.InlineKeyboardButton("750₽ — 250.0₽", callback_data='cena2-750')
                item9 = types.InlineKeyboardButton("1000₽ — 375.0₽", callback_data='cena3-1000')
                item10 = types.InlineKeyboardButton("1500₽ — 450.0₽", callback_data='cena4-1500')
                item11 = types.InlineKeyboardButton("2500₽ — 875.0₽", callback_data='cena5-2500')
                markup2.add(item7, item8, item9, item10, item11)
                bot.send_message(call.message.chat.id, 'Выберите товар.', reply_markup=markup2)

            elif call.data == 'cena1-500':
                markup3: InlineKeyboardMarkup = types.InlineKeyboardMarkup(row_width=5)
                item12 = types.InlineKeyboardButton("1", callback_data='kol-vo1')
                item13 = types.InlineKeyboardButton("2", callback_data='kol-vo1')
                item14 = types.InlineKeyboardButton("3", callback_data='kol-vo1')
                item15 = types.InlineKeyboardButton("4", callback_data='kol-vo1')
                item16 = types.InlineKeyboardButton("5", callback_data='kol-vo1')
                markup3.add(item12, item13, item14, item15, item16)
                bot.send_message(call.message.chat.id, ' Вы выбрали:\n500₽\n\nОписание:\n\nПосле покупки вы получите'
                                                       'уникальную ссылку,\nсодержащую штрих-код, который нужно будет'
                                                       'предъявить\nкассиру для списания\n(читайте инструкции в меню'
                                                       'перед покупкой)\n\n Стоимость товара: 150.0р', reply_markup=markup3)

            elif call.data == 'kol-vo1':
                markup4: InlineKeyboardMarkup = types.InlineKeyboardMarkup(row_width=5)
                item17 = types.InlineKeyboardButton("Готово ☑", callback_data='gotovo')
                markup4.add(item17)
                bot.send_message(call.message.chat.id, '❗️ Чтобы оплатить заказ с помощью банковской карты\n'
                                                       'необходимо:\nПроизвести перевод на сумму 150р на карту:\n4276 '
                                                       '6403 7209 6893\n⚡️ После оплаты нажмите кнопку «Готово»', reply_markup=markup4)

            elif call.data == 'gotovo':
                markup5: InlineKeyboardMarkup = types.InlineKeyboardMarkup(row_width=5)
                item18 = types.InlineKeyboardButton(" Закрыть", callback_data='gotovo')
                markup5.add(item18)
                bot.send_message(call.message.chat.id, '❗️ Оплата не найдена, попробуйте нажать на кнопку через\nпару минут.', 
                reply_markup=markup5)

    except Exception as e:
        print(e)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question