Fix Matrix API notification to use PUT method with transaction ID
- 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 <noreply@anthropic.com>
This commit is contained in:
@@ -52,7 +52,9 @@ class MatrixNotifier:
|
|||||||
def send_message(self, message: str, msgtype: str = "m.text") -> bool:
|
def send_message(self, message: str, msgtype: str = "m.text") -> bool:
|
||||||
"""Send message to Matrix room"""
|
"""Send message to Matrix room"""
|
||||||
try:
|
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 = {
|
headers = {
|
||||||
"Authorization": f"Bearer {self.access_token}",
|
"Authorization": f"Bearer {self.access_token}",
|
||||||
@@ -64,11 +66,8 @@ class MatrixNotifier:
|
|||||||
"body": message
|
"body": message
|
||||||
}
|
}
|
||||||
|
|
||||||
# Add transaction ID to prevent duplicates
|
# Matrix API requires PUT when transaction ID is in the URL path
|
||||||
txn_id = datetime.datetime.now().strftime("%Y%m%d_%H%M%S_%f")
|
response = self.session.put(url, headers=headers, json=data, timeout=10)
|
||||||
url += f"/{txn_id}"
|
|
||||||
|
|
||||||
response = self.session.post(url, headers=headers, json=data, timeout=10)
|
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
|
|
||||||
logger.info(f"Matrix message sent successfully: {response.json().get('event_id')}")
|
logger.info(f"Matrix message sent successfully: {response.json().get('event_id')}")
|
||||||
|
Reference in New Issue
Block a user