From c9ef477b3dda9e0b4d51a998883fdd566b43fb44 Mon Sep 17 00:00:00 2001 From: zailon Date: Tue, 23 Jun 2026 15:31:58 +0500 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20docs/services/lxc208-manage/alert/matrix.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/services/lxc208-manage/alert/matrix.py | 37 +++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 docs/services/lxc208-manage/alert/matrix.py diff --git a/docs/services/lxc208-manage/alert/matrix.py b/docs/services/lxc208-manage/alert/matrix.py new file mode 100644 index 0000000..8a82d11 --- /dev/null +++ b/docs/services/lxc208-manage/alert/matrix.py @@ -0,0 +1,37 @@ +import re +import logging +import uuid +import requests + +from alerts.rooms import HOMESERVER, ACCESS_TOKEN, get_room_id +from alerts.utils import strip_html + +logger = logging.getLogger(__name__) + +MAX_MATRIX_SIZE = 30000 + + +def send_matrix(text, channel="default"): + if len(text) > MAX_MATRIX_SIZE: + text = text[:MAX_MATRIX_SIZE] + "\n\n... (truncated)" + + room_id = get_room_id(channel) + txn = uuid.uuid4().hex + url = f"{HOMESERVER}/_matrix/client/v3/rooms/{room_id}/send/m.room.message/{txn}" + + payload = { + "msgtype": "m.text", + "format": "org.matrix.custom.html", + "formatted_body": text.replace("\n", "
"), + "body": strip_html(text) + } + headers = { + "Authorization": f"Bearer {ACCESS_TOKEN}", + "User-Agent": "grafana-matrix-relay/1.0" + } + try: + r = requests.put(url, json=payload, headers=headers, timeout=10) + if not r.ok: + logger.error("Matrix error: %s %s", r.status_code, r.text) + except Exception as e: + logger.exception("Error sending Matrix message") \ No newline at end of file