From e94b5b13f8782c8d1d4e9be77461dd998f39f923 Mon Sep 17 00:00:00 2001 From: grabowski Date: Fri, 3 Oct 2025 16:09:23 +0700 Subject: [PATCH] Fix Matrix API notification to use PUT method with transaction ID MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Change HTTP method from POST to PUT for Matrix API v3 - Matrix API requires PUT when transaction ID is included in URL path - Move transaction ID construction before URL building for clarity - Fixes "405 Method Not Allowed" error when sending notifications The Matrix API v3 endpoint structure: PUT /_matrix/client/v3/rooms/{roomId}/send/{eventType}/{txnId} Previous error: POST request was being rejected with 405 Method Not Allowed Now working: PUT request successfully sends messages to Matrix rooms 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/alerting.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/alerting.py b/src/alerting.py index f5a95f7..726a542 100644 --- a/src/alerting.py +++ b/src/alerting.py @@ -52,7 +52,9 @@ class MatrixNotifier: def send_message(self, message: str, msgtype: str = "m.text") -> bool: """Send message to Matrix room""" try: - url = f"{self.homeserver}/_matrix/client/v3/rooms/{self.room_id}/send/m.room.message" + # Add transaction ID to prevent duplicates + txn_id = datetime.datetime.now().strftime("%Y%m%d_%H%M%S_%f") + url = f"{self.homeserver}/_matrix/client/v3/rooms/{self.room_id}/send/m.room.message/{txn_id}" headers = { "Authorization": f"Bearer {self.access_token}", @@ -64,11 +66,8 @@ class MatrixNotifier: "body": message } - # Add transaction ID to prevent duplicates - txn_id = datetime.datetime.now().strftime("%Y%m%d_%H%M%S_%f") - url += f"/{txn_id}" - - response = self.session.post(url, headers=headers, json=data, timeout=10) + # Matrix API requires PUT when transaction ID is in the URL path + response = self.session.put(url, headers=headers, json=data, timeout=10) response.raise_for_status() logger.info(f"Matrix message sent successfully: {response.json().get('event_id')}")