GitHub

chat_message

API reference for chat_message.

Messages read from a chat platform (Effect's effect/chat/ChatMessage).

A ChatMessage has the same shape on every read path: inbound events, Chat.fetch_message and Chat.history. Its text is the platform's own content string, verbatim, and its mentions are data with offsets into that text. text is a different format from outbound Markdown and cannot be posted back as is.

Types

TypeDefinitionDescription
MentionKindtype MentionKind = Literal['user', 'role', 'channel', 'broadcast']

Classes

Functions

mentions_self

mentions_self(message) -> bool

Whether the message mentions the bot. A reply to the bot is not a mention (see replies_to_self).

ParameterTypeDescription
messageChatMessage

Returns bool

def mentions_self(message: ChatMessage) -> bool:
    """Whether the message mentions the bot. A reply to the bot is not a
    mention (see ``replies_to_self``)."""
    return any(mention.is_self for mention in message.mentions)

replies_to_self

replies_to_self(message) -> bool

Whether the message replies to one of the bot's own messages. False when the platform does not say who wrote the message replied to; a Slack thread reply carries no reply_to.

ParameterTypeDescription
messageChatMessage

Returns bool

def replies_to_self(message: ChatMessage) -> bool:
    """Whether the message replies to one of the bot's own messages. False
    when the platform does not say who wrote the message replied to; a Slack
    thread reply carries no ``reply_to``."""
    return message.reply_to is not None and message.reply_to.is_self is True

utf16_slice

utf16_slice(text, start, end) -> str

text[start:end] with start and end in UTF-16 code units, the unit of Mention offsets.

ParameterTypeDescription
textstr
startint
endint

Returns str

def utf16_slice(text: str, start: int, end: int) -> str:
    """``text[start:end]`` with ``start`` and ``end`` in UTF-16 code units,
    the unit of ``Mention`` offsets."""
    encoded = text.encode("utf-16-le", "surrogatepass")
    return encoded[start * 2 : end * 2].decode("utf-16-le", "surrogatepass")

utf16_length

utf16_length(text) -> int

How many UTF-16 code units text takes (JavaScript's length).

ParameterTypeDescription
textstr

Returns int

def utf16_length(text: str) -> int:
    """How many UTF-16 code units ``text`` takes (JavaScript's ``length``)."""
    return len(text) + sum(1 for c in text if ord(c) > 0xFFFF)