CommandHandler¶
- class telegram.ext.CommandHandler(command, callback, filters=None, block=True, has_args=None)[source]¶
Bases:
telegram.ext.BaseHandlerHandler class to handle Telegram commands.
Commands are Telegram messages that start with a
telegram.MessageEntity.BOT_COMMAND(so with/, optionally followed by an@and the bot’s name and/or some additional text). The handler will add alistto theCallbackContextnamedCallbackContext.args. It will contain a list of strings, which is the text following the command split on single or consecutive whitespace characters.By default, the handler listens to messages as well as edited messages. To change this behavior use
~filters.UpdateType.EDITED_MESSAGEin the filter argument.Note
CommandHandlerdoes not handle (edited) channel posts and does not handle commands that are part of a caption. Please useMessageHandlerwith a suitable combination of filters (e.g.telegram.ext.filters.UpdateType.CHANNEL_POSTS,telegram.ext.filters.CAPTIONandtelegram.ext.filters.Regex) to handle those messages.Note
If you want to support a different entity in the beginning, e.g. if a command message is wrapped in a
telegram.MessageEntity.CODE, use thetelegram.ext.PrefixHandler.Warning
When setting
blocktoFalse, you cannot rely on adding custom attributes totelegram.ext.CallbackContext. See its docs for more info.Examples
Use In
Available In
Changed in version 20.0:
- Parameters:
command (
str| Collection[str]) – The command or list of commands this handler should listen for. Case-insensitive. Limitations are the same as fortelegram.BotCommand.command.callback (coroutine function) –
The callback function for this handler. Will be called when
check_update()has determined that an update should be processed by this handler. Callback signature:async def callback(update: Update, context: CallbackContext)
The return value of the callback is usually ignored except for the special case of
telegram.ext.ConversationHandler.filters (
telegram.ext.filters.BaseFilter, optional) – A filter inheriting fromtelegram.ext.filters.BaseFilter. Standard filters can be found intelegram.ext.filters. Filters can be combined using bitwise operators (&forand,|foror,~fornot)Determines whether the return value of the callback should be awaited before processing the next handler in
telegram.ext.Application.process_update(). Defaults toTrue.See also
has_args (
bool|int, optional) –Determines whether the command handler should process the update or not. If
True, the handler will process any non-zero number of args. IfFalse, the handler will only process if there are no args. ifint, the handler will only process if there are exactly that many args. Defaults toNone, which means the handler will process any or no args.Added in version 20.5.
- Raises:
ValueError – When the command is too long or has illegal chars.
- block[source]¶
Determines whether the return value of the callback should be awaited before processing the next handler in
telegram.ext.Application.process_update().- Type:
- has_args[source]¶
Optional argument, otherwise all implementations of
CommandHandlerwill break. Defaults toNone, which means the handler will process any args or no args.Added in version 20.5.
- check_update(update)[source]¶
Determines whether an update should be passed to this handler’s
callback.- Parameters:
update (
telegram.Update|object) – Incoming update.- Returns:
The list of args for the handler.
- Return type:
- collect_additional_context(context, update, application, check_result)[source]¶
Add text after the command to
CallbackContext.argsas list, split on single whitespaces and add output of data filters toCallbackContextas well.