Y
Y
yurij1232021-05-15 08:46:07
Python
yurij123, 2021-05-15 08:46:07

How to remove aiogram inline keyboard?

I work with aiogram. I can not solve the problem with the inline keyboard. Please tell me how to remove inline buttons after clicking on one of them. Please tell me the easiest way.

from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor
from aiogram.types import ReplyKeyboardRemove, \
    ReplyKeyboardMarkup, KeyboardButton, \
    InlineKeyboardMarkup, InlineKeyboardButton

TOKEN = "****"

bot = Bot(token=TOKEN)

dp = Dispatcher(bot)

@dp.message_handler(commands=['start'])
async def answer(message: types.Message):
    btn1 = KeyboardButton('Привет')
    markup1 = ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True).add(btn1)
    await message.reply('Привет', reply_markup=markup1)


@dp.message_handler(content_types=['text'])
async def chat(message: types.Message):
    if message.text == 'Привет':
        btn3 = InlineKeyboardButton('Отлично!', callback_data='good')
        btn4 = InlineKeyboardButton('Не очень!', callback_data='bad')
        markup2 = InlineKeyboardMarkup(row_width=2, one_time_keyboard=True).add(btn3, btn4)
        await message.reply('Привет, как дела?', reply_markup=markup2)


    else:
        await message.reply('Извините, но я не понимаю!')





if __name__ == '__main__':
    executor.start_polling(dp)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dragonz1, 2021-07-15
@dragonz1

@dp.callback_query_handler(text="your callback_data name here")
async def call_main_menu(call: CallbackQuery):
await bot.delete_message(chat_id=call.from_user.id, message_id=call.message.message_id)

S
SashaN69, 2021-05-15
@SashaN69

You can just delete the message

import asyncio
from contextlib import suppress

from aiogram import types
from aiogram.utils.exceptions import (MessageToEditNotFound, MessageCantBeEdited, MessageCantBeDeleted,
                                      MessageToDeleteNotFound)

async def delete_message(message: types.Message, sleep_time: int = 0):
    await asyncio.sleep(sleep_time)
    with suppress(MessageCantBeDeleted, MessageToDeleteNotFound):
        await message.delete()

msg = await message.reply("Я удалюсь через 30 секунд")
    asyncio.create_task(delete_message(msg, 30)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question