Hide navigation sidebar
Hide table of contents sidebar
Skip to content
Back to top
View this page
Toggle table of contents sidebar

# PrefixHandler

class telegram.ext.PrefixHandler(prefix, command, callback, filters=None, block=True)

  Bases: `telegram.ext.BaseHandler`

  Handler class to handle custom prefix commands.

  This is an intermediate handler between `MessageHandler` and `CommandHandler`. It supports configurable commands with the same options as `CommandHandler`. It will respond to every combination of `prefix` and `command`. It will add a `list` to the `CallbackContext` named `CallbackContext.args`, containing a list of strings, which is the text following the command split on single or consecutive whitespace characters.

  Examples

  Single prefix and command:

      PrefixHandler("!", "test", callback)  # will respond to '!test'.

  Multiple prefixes, single command:

      PrefixHandler(["!", "#"], "test", callback)  # will respond to '!test' and '#test'.

  Multiple prefixes and commands:

      PrefixHandler(
          ["!", "#"], ["test", "help"], callback
      )  # will respond to '!test', '#test', '!help' and '#help'.

  By default, the handler listens to messages as well as edited messages. To change this behavior use `~filters.UpdateType.EDITED_MESSAGE`

  Note

  - `PrefixHandler` does not handle (edited) channel posts.

  Warning

  When setting `block` to `False`, you cannot rely on adding custom attributes to `telegram.ext.CallbackContext`. See its docs for more info.

  Available In

  `telegram.ext.Application.handlers`

  Changed in version 20.0:

  - `PrefixHandler` is no longer a subclass of `CommandHandler`.

  - Removed the attributes `command` and `prefix`. Instead, the new `commands` contains all commands that this handler listens to as a `frozenset`, which includes the prefixes.

  - Updating the prefixes and commands this handler listens to is no longer possible.

  Parameters:

    - prefix (`str` | Collection[`str`]) – The prefix(es) that will precede `command`.

    - command (`str` | Collection[`str`]) – The command or list of commands this handler should listen for. Case-insensitive.

    - 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 from `telegram.ext.filters.BaseFilter`. Standard filters can be found in `telegram.ext.filters`. Filters can be combined using bitwise operators (`&` for `and`, `|` for `or`, `~` for `not`)

    - block (`bool`, optional) –

      Determines whether the return value of the callback should be awaited before processing the next handler in `telegram.ext.Application.process_update()`. Defaults to `True`.

      See also

      Concurrency

  commands

    The commands that this handler will listen for, i.e. the combinations of `prefix` and `command`.

    Type:

      frozenset[`str`]

  callback

    The callback function for this handler.

    Type:

      coroutine function

  filters

    Optional. Only allow updates with these Filters.

    Type:

      `telegram.ext.filters.BaseFilter`

  block

    Determines whether the return value of the callback should be awaited before processing the next handler in `telegram.ext.Application.process_update()`.

    Type:

      `bool`

  check_update(update)

    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:

      `list`

  collect_additional_context(context, update, application, check_result)

    Add text after the command to `CallbackContext.args` as list, split on single whitespaces and add output of data filters to `CallbackContext` as well.