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

# StringCommandHandler

class telegram.ext.StringCommandHandler(command, callback, block=True)

  Bases: `telegram.ext.BaseHandler`

  Handler class to handle string commands. Commands are string updates that start with `/`. The handler will add a `list` to the `CallbackContext` named `CallbackContext.args`. It will contain a list of strings, which is the text following the command split on single whitespace characters.

  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.

  Parameters:

    - command (`str`) – The command this handler should listen for.

    - 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

  command

    The command this handler should listen for.

    Type:

      `str`

  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 (`object`) – The incoming update.

    Returns:

      List containing the text command split on whitespace.

    Return type:

      List[`str`]

  collect_additional_context(context, update, application, check_result)

    Add text after the command to `CallbackContext.args` as list, split on single whitespaces.