Skip to content

Context

MessageT module-attribute

MessageT = TypeVar('MessageT', bound='ReceivedMessage')

Type variable for the kind of message a Context was created from, e.g. Context[DataMessage] in a handler that only fires for data messages.

Context

Context(bot: SignalBot, message: MessageT)

Bases: Generic[MessageT]

Context is a helper class that provides methods to reply, edit, react, etc. to a message. This is useful to avoid having to pass the recipient and other arguments to the bot's methods manually.

Method names mirror the underlying bot.<noun> action they call (e.g. react calls bot.reactions.react). The exception is when that name would be ambiguous as a bare call on Context, which bundles several actions into one flat namespace unlike bot.<noun> (e.g. update_contact/update_group both call an underlying .update(), so the noun is kept here to disambiguate).

Source code in src/signalbot/context/context.py
def __init__(self, bot: SignalBot, message: MessageT) -> None:
    self.bot = bot
    self.message = message
    self._logger = logging.getLogger(LOGGER_NAME)

create_poll async

create_poll(create_poll_request: CreatePoll) -> CreatedPoll

Same as signalbot.PollActions.create() but with the recipient set to the message's recipient.

Source code in src/signalbot/context/context.py
async def create_poll(self, create_poll_request: CreatePoll) -> CreatedPoll:
    """Same as
    [signalbot.PollActions.create()](actions.md#signalbot._actions.PollActions.create)
     but with the recipient set to the message's recipient."""
    received_message = cast("ReceivedMessage", self.message)
    return await self.bot.polls.create(
        create_poll_request, received_message.source_or_group_id()
    )

send async

send(message: SendMessage) -> SentMessage

Same as signalbot.MessageActions.send() but with the recipient set to the message's recipient.

Source code in src/signalbot/context/context.py
async def send(
    self,
    message: SendMessage,
) -> SentMessage:
    """Same as
     [signalbot.MessageActions.send()](actions.md#signalbot._actions.MessageActions.send)
    but with the recipient set to the message's recipient."""
    received_message = cast("ReceivedMessage", self.message)
    return await self.bot.messages.send(
        message, received_message.source_or_group_id()
    )

start_typing async

start_typing() -> None

Same as signalbot.MessageActions.start_typing() but with the recipient set to the message's recipient.

Source code in src/signalbot/context/context.py
async def start_typing(self) -> None:
    """Same as
    [signalbot.MessageActions.start_typing()](actions.md#signalbot._actions.MessageActions.start_typing)
     but with the recipient set to the message's recipient."""
    received_message = cast("ReceivedMessage", self.message)
    await self.bot.messages.start_typing(received_message.source_or_group_id())

stop_typing async

stop_typing() -> None

Same as signalbot.MessageActions.stop_typing() but with the recipient set to the message's recipient.

Source code in src/signalbot/context/context.py
async def stop_typing(self) -> None:
    """Same as
    [signalbot.MessageActions.stop_typing()](actions.md#signalbot._actions.MessageActions.stop_typing)
     but with the recipient set to the message's recipient."""
    received_message = cast("ReceivedMessage", self.message)
    await self.bot.messages.stop_typing(received_message.source_or_group_id())

update_contact async

update_contact(update_contact: UpdateContact) -> None

Same as signalbot.ContactActions.update() but with the recipient set to the message's recipient.

Source code in src/signalbot/context/context.py
async def update_contact(self, update_contact: UpdateContact) -> None:
    """Same as
    [signalbot.ContactActions.update()](actions.md#signalbot._actions.ContactActions.update)
     but with the recipient set to the message's recipient."""
    received_message = cast("ReceivedMessage", self.message)
    if received_message.is_group():
        error_msg = "Cannot update contact for a group message"
        raise ValueError(error_msg)
    await self.bot.contacts.update(
        update_contact, received_message.source_or_group_id()
    )

update_group async

update_group(update_group: UpdateGroup) -> None

Same as signalbot.GroupActions.update() but with the group id or name set to the message's recipient.

Source code in src/signalbot/context/context.py
async def update_group(
    self,
    update_group: UpdateGroup,
) -> None:
    """Same as
    [signalbot.GroupActions.update()](actions.md#signalbot._actions.GroupActions.update)
     but with the group id or name set to the message's recipient."""
    received_message = cast("ReceivedMessage", self.message)
    if received_message.is_private():
        error_msg = "Cannot update group for a private message"
        raise ValueError(error_msg)
    await self.bot.groups.actions.update(
        update_group, received_message.source_or_group_id()
    )

DataMessageContext

DataMessageContext(bot: SignalBot, message: MessageT)

Bases: Context[DataMessage | EditMessage]

Source code in src/signalbot/context/context.py
def __init__(self, bot: SignalBot, message: MessageT) -> None:
    self.bot = bot
    self.message = message
    self._logger = logging.getLogger(LOGGER_NAME)

delete_attachment async

delete_attachment(attachment: Attachment) -> None

Same as signalbot.AttachmentActions.delete().

Source code in src/signalbot/context/data_message_context.py
async def delete_attachment(self, attachment: Attachment) -> None:
    """Same as
    [signalbot.AttachmentActions.delete()](actions.md#signalbot._actions.AttachmentActions.delete)."""
    await self.bot.attachments.delete(attachment)

edit async

edit(
    new_message: SendMessage, original_message: SentMessage
) -> SentMessage

Same as signalbot.MessageActions.edit().

Source code in src/signalbot/context/data_message_context.py
async def edit(
    self, new_message: SendMessage, original_message: SentMessage
) -> SentMessage:
    """Same as
    [signalbot.MessageActions.edit()](actions.md#signalbot._actions.MessageActions.edit)."""
    return await self.bot.messages.edit(new_message, original_message)

react async

react(emoji: str) -> None

Same as signalbot.ReactionActions.react() but with the recipient set to the message's recipient.

Source code in src/signalbot/context/data_message_context.py
async def react(self, emoji: str) -> None:
    """Same as
     [signalbot.ReactionActions.react()](actions.md#signalbot._actions.ReactionActions.react)
    but with the recipient set to the message's recipient."""
    await self.bot.reactions.react(self.message, emoji)

remote_delete async

remote_delete(sent_message: SentMessage) -> int

Same as signalbot.MessageActions.remote_delete().

Source code in src/signalbot/context/data_message_context.py
async def remote_delete(self, sent_message: SentMessage) -> int:
    """Same as
    [signalbot.MessageActions.remote_delete()](actions.md#signalbot._actions.MessageActions.remote_delete)."""
    return await self.bot.messages.remote_delete(sent_message)

reply async

reply(message: SendMessage) -> SentMessage

Same as signalbot.MessageActions.send() but with the quote arguments set to the message's.

Source code in src/signalbot/context/data_message_context.py
async def reply(
    self,
    message: SendMessage,
) -> SentMessage:
    """Same as
     [signalbot.MessageActions.send()](actions.md#signalbot._actions.MessageActions.send)
    but with the quote arguments set to the message's."""
    send_mentions = self._convert_receive_mentions_into_send_mentions(
        self.message.mentions,
    )
    message = deepcopy(message)
    message.quote_mentions = send_mentions
    message.quote_author = self.message.source_uuid or self.message.source_number
    message.quote_text = self.message.text
    message.quote_timestamp = self.message.timestamp

    return await self.bot.messages.send(message, self.message.source_or_group_id())

send_receipt async

send_receipt(receipt_type: ReceiptType) -> None

Same as signalbot.ReceiptActions.send() but with the recipient set to the message's recipient.

Source code in src/signalbot/context/data_message_context.py
async def send_receipt(self, receipt_type: ReceiptType) -> None:
    """Same as
     [signalbot.ReceiptActions.send()](actions.md#signalbot._actions.ReceiptActions.send)
    but with the recipient set to the message's recipient."""
    await self.bot.receipts.send(self.message, receipt_type)

GroupUpdateContext

GroupUpdateContext(bot: SignalBot, message: MessageT)

Bases: Context[GroupUpdate]

Context passed to GroupUpdateHandler.handle_group_update.

Source code in src/signalbot/context/context.py
def __init__(self, bot: SignalBot, message: MessageT) -> None:
    self.bot = bot
    self.message = message
    self._logger = logging.getLogger(LOGGER_NAME)

ReactionContext

ReactionContext(bot: SignalBot, message: MessageT)

Bases: Context[Reaction]

Context passed to ReactionHandler.handle_reaction.

Source code in src/signalbot/context/context.py
def __init__(self, bot: SignalBot, message: MessageT) -> None:
    self.bot = bot
    self.message = message
    self._logger = logging.getLogger(LOGGER_NAME)

ReadyContext

ReadyContext(bot: SignalBot)

Context passed to ReadyHandler.handle_ready. Unlike the other contexts, there is no originating message, so this only gives access to the bot.

Source code in src/signalbot/context/ready_context.py
def __init__(self, bot: SignalBot) -> None:
    self.bot = bot

RemoteDeleteContext

RemoteDeleteContext(bot: SignalBot, message: MessageT)

Bases: Context[RemoteDelete]

Context passed to RemoteDeleteHandler.handle_remote_delete.

Source code in src/signalbot/context/context.py
def __init__(self, bot: SignalBot, message: MessageT) -> None:
    self.bot = bot
    self.message = message
    self._logger = logging.getLogger(LOGGER_NAME)

TypingContext

TypingContext(bot: SignalBot, message: MessageT)

Bases: Context[TypingMessage]

Context passed to TypingHandler.handle_typing.

Source code in src/signalbot/context/context.py
def __init__(self, bot: SignalBot, message: MessageT) -> None:
    self.bot = bot
    self.message = message
    self._logger = logging.getLogger(LOGGER_NAME)