D
D
daxak2022-03-27 23:59:38
Python
daxak, 2022-03-27 23:59:38

How to make the bot react the same to all commands except for a certain list in aiogram?

I want the bot to say "For information about existing commands, type /help" when the command is not in the following list: ["weather", "dice"]

Answer the question

In order to leave comments, you need to log in

4 answer(s)
W
WolfInChains, 2022-03-28
@WolfInChains

if command not in ["weather", "dice"]:
    send_message("Для получения информации о существующих командах введите /help")

J
Jan, 2022-03-28
@Buchachalo

After you describe the reactions to all the necessary commands, create a handler with a regular expression that will catch all messages starting with "/" and that will send your message.

S
SHADRIN, 2022-03-28
@shadrin_ss

Create a handler

@dp.message_handler() # Он принимает все запросы без фильтров
    if message.text == 'тут твой текст':
        pass # ответ 
    else:
        send_message("Для получения информации о существующих командах введите /help")

L
LXSTVAYNE, 2022-03-28
@lxstvayne

Write your own filter:

from aiogram import types
from aiogram.dispatcher.filters import Filter, Command


class CommandNotInListFilter(Filter):
    def __init__(self, commands: Command):
        self.commands = commands

    def check(self, message: types.Message) -> bool:
        if not message.text.startswith('/'):
            return False
        return message.text[1:] in self.commands.commands

Next, you will need to create a handler that would catch unknown commands:
@dp.message_handler(
    CommandNotInListFilter(Command(['start', 'help']))
)
async def handle_unknown_commands(message: types.Message):
    await message.reply("Данной команды не существует!")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question