Skip to content

Context

For each command invoked, the first argument is always a Context instance, which holds a lot of metadata, and a few utility functions to help you write commands.

A lot of the time, these are the three main attributes you'll be using:

  • Context.room (nio.MatrixRoom) - the room the command was invoked in.
  • Context.message (nio.RoomMessageText) - the message that invoked this command.
  • Context.respond - a utility class to help you respond to the command.

Command Context

Event-based context for a command callback

room property

room: MatrixRoom

The room that the event was dispatched in

client property

client: 'NioBot'

The current instance of the client

command property

command: 'Command'

The current command being invoked

args property

args: list[str]

Each argument given to this command

message property

message: RoomMessageText

The current message

original_response property

original_response: Optional[RoomSendResponse]

The result of Context.reply(), if it exists.

latency property

latency: float

Returns the current event's latency in milliseconds.

respond async

respond(
    content: Optional[str] = None,
    file: Optional[BaseAttachment] = None,
    reply_to: Optional[Union[RoomMessageText, str]] = None,
    message_type: Optional[str] = None,
    *,
    content_type: Literal[
        "plain", "markdown", "html", "html.raw"
    ] = "markdown",
    override: Optional[dict] = None,
    mentions: Union["Mentions", Literal[False], None] = None
) -> ContextualResponse

Responds to the current event.

See niobot.NioBot.send_message for more information.

Contextual Response

Context class for managing replies.

Usage of this function is not required, however it is a useful utility.

original_event async

original_event() -> Optional[RoomMessage]

Fetches the current event for this response

reply async

reply(
    content: Optional[str] = None,
    file: Optional[BaseAttachment] = None,
    message_type: Optional[str] = None,
    *,
    content_type: Literal[
        "plain", "markdown", "html", "html.raw"
    ] = "markdown",
    override: Optional[dict] = None
) -> "ContextualResponse"

Replies to the current response.

This does NOT reply to the original invoking message.

See niobot.NioBot.send_message for more information.

edit async

edit(
    content: str,
    *,
    message_type: Optional[str] = None,
    content_type: Literal[
        "plain", "markdown", "html", "html.raw"
    ] = "markdown",
    override: Optional[dict] = None
) -> "ContextualResponse"

Edits the current response.

See niobot.NioBot.edit_message for more information.

delete async

delete(reason: Optional[str] = None) -> None

Redacts the current response.

Parameters:

Name Type Description Default
reason Optional[str]

An optional reason for the redaction

None

Returns:

Type Description
None

None, as there will be no more response.