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

# StringRegexHandler

class telegram.ext.StringRegexHandler(pattern, callback, block=True)

  Bases: `telegram.ext.BaseHandler`

  Handler class to handle string updates based on a regex which checks the update content.

  Read the documentation of the `re` module for more information. The `re.match()` function is used to determine if an update should be handled by this handler.

  Note

  This handler is not used to handle Telegram `telegram.Update`, but strings manually put in the queue. For example to send messages with the bot using command line or API.

  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`

  Parameters:

    - pattern (`str` | `re.Pattern`) – The regex pattern.

    - 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: str, 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

  pattern

    The regex pattern.

    Type:

      `str` | `re.Pattern`

  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`

  check_update(update)

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

    Parameters:

      update (`object`) – The incoming update.

    Returns:

      `None` | `re.match`

  collect_additional_context(context, update, application, check_result)

    Add the result of `re.match(pattern, update)` to `CallbackContext.matches` as list with one element.