Добавить docs/services/lxc208-manage/alert/matrix.py
This commit is contained in:
parent
104a7effe1
commit
c9ef477b3d
37
docs/services/lxc208-manage/alert/matrix.py
Normal file
37
docs/services/lxc208-manage/alert/matrix.py
Normal file
@ -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", "<br>"),
|
||||
"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")
|
||||
Loading…
Reference in New Issue
Block a user