'aiogram 2.20: How to remove a button in ReplyKeyboardMarkup after it has been called?

Python 3.10.4 | aiogram 2.20

Recently I encountered a need: to remove a button from the keyboard after it was called and this or that function worked on it.

I am NOT talking about the one_time_keyboard parameter. I need to remove this button after which this or that 'statement' worked from the existing set of buttons.

I tried to find out what ReplyKeyboardRemove() is, but most likely this is not it, since this function removes the entire keyboard.

Code:

from os import sep
from aiogram.utils.markdown import text, bold, italic
from aiogram.utils.emoji import emojize
from aiogram.types import ParseMode
from aiogram.dispatcher.filters import Text
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters.state import State, StatesGroup

from aiogram.types import KeyboardButton
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from aiogram.types import ReplyKeyboardMarkup, ReplyKeyboardRemove

import logging


from config import bot, dp
from aiogram import types, executor, Dispatcher


logging.basicConfig(level=logging.INFO)

async def shutdown(dispatcher: Dispatcher):
    await dispatcher.storage.close()
    await dispatcher.storage.wait_closed()

class States(StatesGroup):
    set_new_name_state = State()
    set_new_date_state = State()
    set_new_notification_state = State()

#start menu

mmenu_check_tasks = KeyboardButton('Check my tasks:')
mmenu_create_task = KeyboardButton('Create new task:')
mmenu_check_weather = KeyboardButton('Check weather:')
mmenu_settings = KeyboardButton('Settings:')

task_menu = ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True)
no_task_bttn = KeyboardButton('Here is no tasks.')

main_menu = ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True).insert(
    mmenu_check_tasks).insert(mmenu_create_task).insert(mmenu_check_weather).row(mmenu_settings)

#filling new task

new_name_button = KeyboardButton('Name:')
new_date_button = KeyboardButton('Date:')
new_notification_parametr = KeyboardButton('Install notification:')
confirm_setting = KeyboardButton('Confirm settings.')

parametr_set_to_create = ReplyKeyboardMarkup(resize_keyboard = True, one_time_keyboard = True).insert(new_name_button
                                            ).insert(new_date_button).insert(new_notification_parametr)

#main script

@dp.message_handler(commands = ['start'])
async def say_hello(msg: types.Message):
    await msg.answer(text(bold('Hello.\nI\'m your organizer bot, can I help you plan your affairs?')),
     ParseMode.MARKDOWN, reply_markup=main_menu)

@dp.message_handler(Text(equals = ['Check my tasks:'])) #what should the bot do after clicking the 'Check my tasks:' button
async def my_task_menu(msg: types.Message):
    if task_menu == ReplyKeyboardMarkup(resize_keyboard = True, one_time_keyboard = True):
        await msg.answer('Here is no tasks.', reply_markup = main_menu) #what to send to the bot if the list of tasks is empty
    else: 
        await bot.send_message(reply_markup = task_menu) #what to send to the bot if the list of tasks exists

@dp.message_handler(Text(equals = ['Create new task:'])) #what should the bot do after clicking the 'Create new task:' button
async def new_task_menu(msg: types.Message):
    await msg.answer('Fill in the required questions:', reply_markup=parametr_set_to_create)

@dp.message_handler(Text(equals = ['Name:'])) #what should the bot do after clicking the 'Name:' button
async def set_new_task_name(msg: types.Message, state: FSMContext):
    await state.set_state(States.set_new_name_state)

@dp.message_handler(state = States.set_new_name_state) #what should the bot do after clicking the 'Name:' button
async def say_wroten_name(msg: types.Message, state: FSMContext):
    await state.reset_state()
    global name
    name = msg.text #this action exists just to know that everything is working.
    await msg.answer(text(bold(name)), ParseMode.MARKDOWN, reply_markup = ReplyKeyboardRemove())

@dp.message_handler(Text(equals = ['Confirm settings.'])) #insert a user-written task name into the button
async def to_confirm_new_setting(msg: types.Message):
    task_menu.insert(name)
    await msg.answer('The settings saved succesfully, the task saved.')



if __name__ == '__main__':
    executor.start_polling(dp, skip_updates = True, on_shutdown = shutdown)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source