Hide navigation sidebar
Hide table of contents sidebar
PTB has undergone significant changes in v20. Please read the documentation carefully and also check out the transition guide in the wiki.
Back to top
Toggle table of contents sidebar

# MessageHandler

class telegram.ext.MessageHandler(filters, callback, block=True)

  Bases: `telegram.ext.BaseHandler`

  Handler class to handle Telegram messages. They might contain text, media or status updates.

  Warning

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

  Parameters:

    - filters (`telegram.ext.filters.BaseFilter`) –

      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). Passing `None` is a shortcut to passing `telegram.ext.filters.ALL`.

      See also

      Advanced Filters

    - 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`.

    - 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

  filters

    Only allow updates with these Filters. See `telegram.ext.filters` for a full list of all available filters.

    Type:

      `telegram.ext.filters.BaseFilter`

  callback

    The callback function for this handler.

    Type:

      coroutine function

  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`

  Available In

  `telegram.ext.Application.handlers`

  check_update(update)

    Determines whether an update should be passed to this handler’s `callback`.

    Parameters:

      update (`telegram.Update` | `object`) – Incoming update.

    Returns:

      `bool`

  collect_additional_context(context, update, application, check_result)

    Adds possible output of data filters to the `CallbackContext`.