From b673837f4e20a326f1013c2a41f658c6c642a534 Mon Sep 17 00:00:00 2001 From: privacyfr3ak <220089342+privacyfr3ak@users.noreply.github.com> Date: Sun, 27 Jul 2025 14:35:31 -0400 Subject: [PATCH] fix attachement with matrix v3 --- apprise/plugins/matrix.py | 131 +++++++++++++++++++------------------- 1 file changed, 64 insertions(+), 67 deletions(-) diff --git a/apprise/plugins/matrix.py b/apprise/plugins/matrix.py index 41f8b046..33b58b20 100644 --- a/apprise/plugins/matrix.py +++ b/apprise/plugins/matrix.py @@ -651,6 +651,9 @@ class NotifyMatrix(NotifyBase): # Get our room room = rooms.pop(0) + # Set method according to MatrixVersion + method = 'PUT' if self.version == MatrixVersion.V3 else 'POST' + # Get our room_id from our response room_id = self._room_join(room) if not room_id: @@ -677,39 +680,49 @@ class NotifyMatrix(NotifyBase): path = '/rooms/{}/send/m.room.message'.format( NotifyMatrix.quote(room_id)) - if self.version == MatrixVersion.V2: - # - # Attachments don't work beyond V2 at this time - # - if image_url: - # Define our payload - image_payload = { - 'msgtype': 'm.image', - 'url': image_url, - 'body': '{}'.format( - notify_type if not title else title), - } + if image_url: + # Define our payload + image_payload = { + 'msgtype': 'm.image', + 'url': image_url, + 'body': '{}'.format( + notify_type if not title else title), + } + + # Post our content + postokay, response = self._fetch( + path, payload=image_payload) + if not postokay: + # Mark our failure + has_error = True + continue + + if attachments: + for attachment in attachments: + attachment['room_id'] = room_id + attachment['type'] = 'm.room.message' - # Post our content postokay, response = self._fetch( - path, payload=image_payload) + path, payload=attachment, method=method) + + # Increment the transaction ID to avoid future messages + # being recognized as retransmissions and ignored + if self.version == MatrixVersion.V3 \ + and self.access_token != self.password: + self.transaction_id += 1 + self.store.set( + 'transaction_id', self.transaction_id, + expires=self.default_cache_expiry_sec) + path = '/rooms/{}/send/m.room.message/{}'.format( + NotifyMatrix.quote(room_id), + self.transaction_id, + ) + if not postokay: # Mark our failure has_error = True continue - if attachments: - for attachment in attachments: - attachment['room_id'] = room_id - attachment['type'] = 'm.room.message' - - postokay, response = self._fetch( - path, payload=attachment) - if not postokay: - # Mark our failure - has_error = True - continue - # Define our payload payload = { 'msgtype': 'm.{}'.format(self.msgtype), @@ -740,7 +753,6 @@ class NotifyMatrix(NotifyBase): }) # Post our content - method = 'PUT' if self.version == MatrixVersion.V3 else 'POST' postokay, response = self._fetch( path, payload=payload, method=method) @@ -770,10 +782,6 @@ class NotifyMatrix(NotifyBase): """ payloads = [] - if self.version != MatrixVersion.V2: - self.logger.warning( - 'Add ?v=2 to Apprise URL to support Attachments') - return next((False for a in attach if not a), []) for attachment in attach: if not attachment: @@ -795,38 +803,28 @@ class NotifyMatrix(NotifyBase): # "content_uri": "mxc://example.com/a-unique-key" # } - # FUTURE if self.version == MatrixVersion.V3: - # FUTURE # Prepare our payload - # FUTURE payloads.append({ - # FUTURE "body": attachment.name, - # FUTURE "info": { - # FUTURE "mimetype": attachment.mimetype, - # FUTURE "size": len(attachment), - # FUTURE }, - # FUTURE "msgtype": "m.image", - # FUTURE "url": response.get('content_uri'), - # FUTURE }) + if self.version == MatrixVersion.V3: + # Prepare our payload + payloads.append({ + "body": attachment.name, + "info": { + "mimetype": attachment.mimetype, + "size": len(attachment), + }, + "msgtype": "m.image", + "url": response.get('content_uri'), + }) - # FUTURE else: - # FUTURE # Prepare our payload - # FUTURE payloads.append({ - # FUTURE "info": { - # FUTURE "mimetype": attachment.mimetype, - # FUTURE }, - # FUTURE "msgtype": "m.image", - # FUTURE "body": "tta.webp", - # FUTURE "url": response.get('content_uri'), - # FUTURE }) - - # Prepare our payload - payloads.append({ - "info": { - "mimetype": attachment.mimetype, - }, - "msgtype": "m.image", - "body": "tta.webp", - "url": response.get('content_uri'), - }) + else: + # Prepare our payload + payloads.append({ + "info": { + "mimetype": attachment.mimetype, + }, + "msgtype": "m.image", + "body": "tta.webp", + "url": response.get('content_uri'), + }) return payloads @@ -1251,12 +1249,11 @@ class NotifyMatrix(NotifyBase): status_code = requests.codes.internal_server_error if path == '/upload': - # FUTURE if self.version == MatrixVersion.V3: - # FUTURE url += MATRIX_V3_MEDIA_PATH + path + if self.version == MatrixVersion.V3: + url += MATRIX_V3_MEDIA_PATH + path - # FUTURE else: - # FUTURE url += MATRIX_V2_MEDIA_PATH + path - url += MATRIX_V2_MEDIA_PATH + path + else: + url += MATRIX_V2_MEDIA_PATH + path params.update({'filename': attachment.name}) with open(attachment.path, 'rb') as fp: