Skip to content

Message

Message dataclass

Message(
    source: str,
    source_number: str | None,
    source_uuid: str,
    timestamp: int,
    type: MessageType,
    text: str,
    base64_attachments: list[str] = list(),
    attachments_local_filenames: list[str] = list(),
    view_once: bool = False,
    link_previews: list[LinkPreview] = list(),
    group: str | None = None,
    reaction: str | None = None,
    mentions: list[str] = list(),
    quote: Quote | None = None,
    read_messages: list[dict] | None = None,
    target_sent_timestamp: int | None = None,
    remote_delete_timestamp: int | None = None,
    updated_group_id: str | None = None,
    raw_message: str | None = None,
)

Class representing a Signal message.

Attributes:

Name Type Description
source str

The phone number or UUID of the sender of the message.

source_number str | None

The phone number of the sender of the message. This is None for signal contacts where the phone number was not exchanged.

source_uuid str

The UUID of the sender of the message.

timestamp int

The timestamp of when the message was sent.

type MessageType

The type of the message.

text str

The text content of the message.

base64_attachments list[str]

A list of attachments in the message, encoded as base64 strings.

attachments_local_filenames list[str]

A list of local filenames for the attachments in the message.

view_once bool

A boolean indicating whether the message is a view-once message.

link_previews list[LinkPreview]

A list of LinkPreview objects representing the link previews in the message.

group str | None

The UUID of the group chat the message was sent in, or None if the message was sent in a user chat.

reaction str | None

The reaction emoji if the message is a reaction.

mentions list[str]

A list of UUIDs of users mentioned in the message.

quote Quote | None

An object representing the quoted message if the message is a quote.

read_messages list[dict] | None

A list of dictionaries representing the messages that have been read if the message is a MessageType.READ_MESSAGE

target_sent_timestamp int | None

The timestamp of the original message that was edited, if the message is a MessageType.EDIT_MESSAGE.

remote_delete_timestamp int | None

The timestamp of the original message that was deleted, the message is a MessageType.DELETE_MESSAGE.

updated_group_id str | None

The UUID of the group that was updated, if the message is a MessageType.GROUP_UPDATE_MESSAGE.

raw_message str | None

The raw JSON string of the message as received from the Signal API.

is_group

is_group() -> bool

Check if the message is a group message.

Returns:

Type Description
bool

True if the message is a group message, False otherwise.

Source code in src/signalbot/message.py
116
117
118
119
120
121
122
def is_group(self) -> bool:
    """Check if the message is a group message.

    Returns:
        True if the message is a group message, False otherwise.
    """
    return bool(self.group)

is_private

is_private() -> bool

Check if the message is a private (one-on-one) message.

Returns:

Type Description
bool

True if the message is a private (one-on-one) message, False otherwise.

Source code in src/signalbot/message.py
107
108
109
110
111
112
113
114
def is_private(self) -> bool:
    """Check if the message is a private (one-on-one) message.


    Returns:
        True if the message is a private (one-on-one) message, False otherwise.
    """
    return not bool(self.group)

parse async classmethod

parse(signal: SignalAPI, raw_message_str: str) -> Message

Parse a raw JSON message string from the Signal API into a Message object.

Parameters:

Name Type Description Default
signal SignalAPI

An instance of the SignalAPI class, used to fetch attachments and link previews if necessary.

required
raw_message_str str

The raw JSON string of the message as received from the Signal API.

required

Returns:

Type Description
Message

A Message object representing the parsed message.

Raises:

Type Description
UnknownMessageFormatError

If the message format is unrecognized or if required fields are missing.

Source code in src/signalbot/message.py
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
@classmethod
async def parse(cls, signal: SignalAPI, raw_message_str: str) -> Message:
    """Parse a raw JSON message string from the Signal API into a Message object.

    Args:
        signal: An instance of the `SignalAPI` class, used to fetch attachments and
            link previews if necessary.
        raw_message_str: The raw JSON string of the message as received from the
            Signal API.

    Returns:
        A `Message` object representing the parsed message.

    Raises:
        UnknownMessageFormatError: If the message format is unrecognized or if
            required fields are missing.
    """
    try:
        raw_message = json.loads(raw_message_str)
    except Exception as exc:
        raise UnknownMessageFormatError from exc

    envelope = raw_message["envelope"]
    # General attributes
    try:
        source = envelope["source"]
        source_uuid = envelope["sourceUuid"]
        timestamp = envelope["timestamp"]
    except Exception as exc:
        raise UnknownMessageFormatError from exc

    source_number = envelope.get("sourceNumber")

    (
        message_type,
        data_message,
        target_sent_timestamp,
        remote_delete_timestamp,
        updated_group_id,
    ) = cls._extract_message_data(envelope)

    text = cls._parse_data_message(data_message)
    group = cls._parse_group_information(data_message)
    reaction = cls._parse_reaction(data_message)
    mentions = cls._parse_mentions(data_message)
    quote = cls._parse_quote(data_message)
    read_messages = data_message.get("readMessages")

    base64_attachments, attachments_local_filenames, link_previews = [], [], []
    view_once = False
    if signal.download_attachments:
        base64_attachments = await cls._parse_attachments(signal, data_message)
        attachments_local_filenames = cls._parse_attachments_local_filenames(
            data_message,
        )
        link_previews = await cls._parse_previews(signal, data_message)
        view_once = data_message.get("viewOnce", False)

    return cls(
        source,
        source_number,
        source_uuid,
        timestamp,
        message_type,
        text,
        base64_attachments=base64_attachments,
        attachments_local_filenames=attachments_local_filenames,
        view_once=view_once,
        link_previews=link_previews,
        group=group,
        reaction=reaction,
        mentions=mentions,
        quote=quote,
        read_messages=read_messages,
        target_sent_timestamp=target_sent_timestamp,
        remote_delete_timestamp=remote_delete_timestamp,
        updated_group_id=updated_group_id,
        raw_message=raw_message_str,
    )

recipient

recipient() -> str

Get the recipient of the message, which is either the group ID for group chats or the source ID for user chats.

Returns:

Type Description
str

The recipient ID of the message.

Source code in src/signalbot/message.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
def recipient(self) -> str:
    """Get the recipient of the message, which is either the group ID for group
        chats or the source ID for user chats.

    Returns:
        The recipient ID of the message.
    """
    # Case 1: Group chat
    if self.group:
        return self.group  # internal ID

    # Case 2: User chat
    return self.source

MessageType

Bases: Enum

Enum representing the type of a Signal message.

Attributes:

Name Type Description
SYNC_MESSAGE

Message received in a linked device

DATA_MESSAGE

Message received in a primary device

EDIT_MESSAGE

Message received is an edit of a previous message

DELETE_MESSAGE

Message received is a remote delete of a previous message

READ_MESSAGE

User read some messages

GROUP_UPDATE_MESSAGE

An update has been made to a group

CONTACT_SYNC_MESSAGE

Message received is a contact sync

UnknownMessageFormatError

Bases: Exception

Exception raised when a message with an unknown format is encountered.