Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions signalbot/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def __init__(
type: MessageType,
text: str,
base64_attachments: list = None,
attachments_local_filenames: Optional[list] = None,
group: str = None,
reaction: str = None,
mentions: list = None,
Expand All @@ -39,6 +40,10 @@ def __init__(
if self.base64_attachments is None:
self.base64_attachments = []

self.attachments_local_filenames = attachments_local_filenames
if self.attachments_local_filenames is None:
self.attachments_local_filenames = []

self.group = group

self.reaction = reaction
Expand Down Expand Up @@ -94,6 +99,7 @@ async def parse(cls, signal: SignalAPI, raw_message: str):
raw_message["envelope"]["syncMessage"]["sentMessage"]
)
base64_attachments = None
attachments_local_filenames = None

# Option 2: dataMessage
elif "dataMessage" in raw_message["envelope"]:
Expand All @@ -105,6 +111,9 @@ async def parse(cls, signal: SignalAPI, raw_message: str):
base64_attachments = await cls._parse_attachments(
signal, raw_message["envelope"]["dataMessage"]
)
attachments_local_filenames = cls._parse_attachments_local_filenames(
raw_message["envelope"]["dataMessage"]
)

else:
raise UnknownMessageFormatError
Expand All @@ -117,6 +126,7 @@ async def parse(cls, signal: SignalAPI, raw_message: str):
type,
text,
base64_attachments,
attachments_local_filenames,
group,
reaction,
mentions,
Expand All @@ -134,6 +144,15 @@ async def _parse_attachments(cls, signal: SignalAPI, data_message: dict) -> str:
for attachment in data_message["attachments"]
]

@classmethod
def _parse_attachments_local_filenames(cls, data_message: dict) -> list[str]:

if "attachments" not in data_message:
return []

# The ["id"] is the local filename and the ["filename"] is the remote filename
return [attachment["id"] for attachment in data_message["attachments"]]

@classmethod
def _parse_sync_message(cls, sync_message: dict) -> str:
try:
Expand Down
8 changes: 7 additions & 1 deletion tests/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ class TestMessage(unittest.IsolatedAsyncioTestCase):
raw_data_message = '{"envelope":{"source":"+490123456789","sourceNumber":"+490123456789","sourceUuid":"<uuid>","sourceName":"<name>","sourceDevice":1,"timestamp":1632576001632,"dataMessage":{"timestamp":1632576001632,"message":"Uhrzeit","expiresInSeconds":0,"viewOnce":false,"mentions":[],"attachments":[],"contacts":[],"groupInfo":{"groupId":"<groupid>","type":"DELIVER"}}}}' # noqa
raw_reaction_message = '{"envelope":{"source":"<source>","sourceNumber":"<source>","sourceUuid":"<uuid>","sourceName":"<name>","sourceDevice":1,"timestamp":1632576001632,"syncMessage":{"sentMessage":{"timestamp":1632576001632,"message":null,"expiresInSeconds":0,"viewOnce":false,"reaction":{"emoji":"👍","targetAuthor":"<target>","targetAuthorNumber":"<target>","targetAuthorUuid":"<uuid>","targetSentTimestamp":1632576001632,"isRemove":false},"mentions":[],"attachments":[],"contacts":[],"groupInfo":{"groupId":"<groupid>","type":"DELIVER"},"destination":null,"destinationNumber":null,"destinationUuid":null}}}}' # noqa
raw_user_chat_message = '{"envelope":{"source":"+490123456789","sourceNumber":"+490123456789","sourceUuid":"<uuid>","sourceName":"<name>","sourceDevice":1,"timestamp":1632576001632,"dataMessage":{"timestamp":1632576001632,"message":"Uhrzeit","expiresInSeconds":0,"viewOnce":false}},"account":"+49987654321","subscription":0}' # noqa
raw_attachment_message = '{"envelope":{"source":"+490123456789","sourceNumber":"+490123456789","sourceUuid":"<uuid>","sourceName":"<name>","sourceDevice":1,"timestamp":1632576001632,"dataMessage":{"timestamp":1632576001632,"message":"Uhrzeit","expiresInSeconds":0,"viewOnce":false, "attachments": [{"contentType": "image/png", "filename": "image.png", "id": "4296180834490578536","size": 12005}]}},"account":"+49987654321","subscription":0}' # noqa
raw_attachment_message = '{"envelope":{"source":"+490123456789","sourceNumber":"+490123456789","sourceUuid":"<uuid>","sourceName":"<name>","sourceDevice":1,"timestamp":1632576001632,"dataMessage":{"timestamp":1632576001632,"message":"Uhrzeit","expiresInSeconds":0,"viewOnce":false, "attachments": [{"contentType": "image/png", "filename": "image.png", "id": "1qeCjjWOOo9Gxv8pfdCw.png","size": 12005}]}},"account":"+49987654321","subscription":0}' # noqa

expected_source = "+490123456789"
expected_timestamp = 1632576001632
expected_text = "Uhrzeit"
expected_group = "<groupid>"
expected_local_filename = "1qeCjjWOOo9Gxv8pfdCw.png"

signal_service = "127.0.0.1:8080"
phone_number = "+49123456789"
Expand Down Expand Up @@ -95,6 +96,11 @@ async def test_attachments(self, mock_get):
)
self.assertEqual(message.base64_attachments, [expected_base64_str])

self.assertEqual(len(message.attachments_local_filenames), 1)
self.assertEqual(
message.attachments_local_filenames[0], TestMessage.expected_local_filename
)

# User Chats
async def test_parse_user_chat_message(self):
message = await Message.parse(
Expand Down
Loading