Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude the /command from message_handler in the dispatcher of Aiogram?

I've a message handler that respond to a command in a Bot application written in python3 with the library Aiogram.

I can receive from the application the message I send to the BOT, but I would like to exclude the command (via config possibly, not parsing the string). Maybe it is in the documentation, but currently I could not find the answer.

To be clear with an example, if I type to my TelegramBot

/my_command text

where this is my code in the python3 application

@dispatcher.message_handler(commands=['my_command'])
async def echo(message: types.Message):

I would like to receive from the message_handler in the message types.Message variable the string text and not /my_command text.

Thanks a lot

like image 820
axel Avatar asked Nov 02 '25 17:11

axel


1 Answers

The function to extract only the arguments of a command is get_args():

@dispatcher.message_handler(commands=['my_command'])
async def echo(message: types.Message):
    arguments = message.get_args()
    await message.reply(arguments)
like image 112
axel Avatar answered Nov 05 '25 07:11

axel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!