Skip to content

Commit ea23bad

Browse files
committed
update webhook
1 parent 42e650a commit ea23bad

6 files changed

Lines changed: 125 additions & 3 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Matrix-Stack
2+
3+
```
4+
helm repo add code-tool https://code-tool.github.io/matrix-stack/
5+
```

charts/webhook/.helmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
docker/*

charts/webhook/Chart.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
---
22
name: webhook
3-
version: 0.0.4
3+
version: 0.0.5

charts/webhook/docker/Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FROM nim65s/matrix-webhook:latest
2+
3+
ADD diff.patch /tmp
4+
RUN patch -p1 < /tmp/diff.patch

charts/webhook/docker/diff.patch

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
diff --git a/matrix_webhook/app.py b/matrix_webhook/app.py
2+
index 46c91bb..b8d6005 100644
3+
--- a/matrix_webhook/app.py
4+
+++ b/matrix_webhook/app.py
5+
@@ -26,6 +26,8 @@ async def main(event):
6+
LOGGER.info(msg)
7+
utils.CLIENT.access_token = conf.MATRIX_TOKEN
8+
9+
+ invitation_watcher = utils.watch_for_invitation()
10+
+
11+
server = web.Server(handler.matrix_webhook)
12+
runner = web.ServerRunner(server)
13+
await runner.setup()
14+
@@ -44,6 +46,7 @@ async def main(event):
15+
await event.wait()
16+
17+
# Cleanup
18+
+ invitation_watcher.cancel()
19+
await runner.cleanup()
20+
await utils.CLIENT.close()
21+
22+
diff --git a/matrix_webhook/formatters.py b/matrix_webhook/formatters.py
23+
index 63040bf..e61f387 100644
24+
--- a/matrix_webhook/formatters.py
25+
+++ b/matrix_webhook/formatters.py
26+
@@ -1,6 +1,7 @@
27+
"""Formatters for matrix webhook."""
28+
29+
import re
30+
+from . import utils
31+
32+
33+
def grafana(data, headers):
34+
@@ -111,3 +112,32 @@ def grn(data, headers):
35+
)
36+
37+
return data
38+
+
39+
+
40+
+def slack(data, headers):
41+
+ """Pretty-print a slack notification."""
42+
+ text = ""
43+
+ if "text" in data:
44+
+ text = text + "<div>" + utils.format_url(data["text"]) + "</div>\n"
45+
+ if "attachments" in data and len(data["attachments"]):
46+
+ for attachment in data["attachments"]:
47+
+ text = text + "<div>"
48+
+ if "title_link" in attachment and "title" in attachment:
49+
+ text = text + "<h4><a href=\"" + attachment["title_link"] + "\">" + attachment["title"] + "</a></h4>\n"
50+
+ elif "title" in attachment:
51+
+ text = text + "<h4>" + utils.format_url(attachment["title"]) + "</h4>\n"
52+
+ if "text" in attachment:
53+
+ text = text + utils.format_url(attachment["text"]) + "\n"
54+
+
55+
+ if "fields" in attachment and len(attachment["fields"]):
56+
+ for field in attachment["fields"]:
57+
+ text = text + "<div>"
58+
+ if "title" in field:
59+
+ text = text + "<h5>" + utils.format_url(field["title"]) + "</h5>\n"
60+
+ if "value" in field:
61+
+ text = text + utils.format_url(str(field["value"])) + "\n"
62+
+ text = text + "</div>"
63+
+ text = text + "</div>"
64+
+ data["body"] = text
65+
+ return data
66+
+
67+
diff --git a/matrix_webhook/utils.py b/matrix_webhook/utils.py
68+
index 72172a7..940c7c1 100644
69+
--- a/matrix_webhook/utils.py
70+
+++ b/matrix_webhook/utils.py
71+
@@ -5,11 +5,13 @@ from collections import defaultdict
72+
from http import HTTPStatus
73+
74+
from aiohttp import web
75+
-from nio import AsyncClient
76+
+from asyncio import create_task
77+
+from nio import AsyncClient, InviteEvent, MatrixRoom
78+
from nio.exceptions import LocalProtocolError
79+
from nio.responses import JoinError, RoomSendError
80+
81+
from . import conf
82+
+from re import sub
83+
84+
ERROR_MAP = defaultdict(
85+
lambda: HTTPStatus.INTERNAL_SERVER_ERROR,
86+
@@ -21,6 +23,9 @@ ERROR_MAP = defaultdict(
87+
LOGGER = logging.getLogger("matrix_webhook.utils")
88+
CLIENT = AsyncClient(conf.MATRIX_URL, conf.MATRIX_ID)
89+
90+
+def format_url(data):
91+
+ data = sub(r"(<(https?://[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+)\|([\w\s]+)>)", r'<a href="\2">\3</a>', data)
92+
+ return sub(r"(<)(https?://[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+)(>)", r'<a href="\2">\2</a>', data)
93+
94+
def error_map(resp):
95+
"""Map response errors to HTTP status."""
96+
@@ -66,6 +71,16 @@ async def join_room(room_id):
97+
return create_json_response(HTTPStatus.GATEWAY_TIMEOUT, "Homeserver not responding")
98+
99+
100+
+async def accept_invitation(room: MatrixRoom, event: InviteEvent):
101+
+ LOGGER.info(f"Got invite to room {room.room_id=}")
102+
+ await CLIENT.join(room.room_id)
103+
+
104+
+
105+
+def watch_for_invitation():
106+
+ CLIENT.add_event_callback(accept_invitation, InviteEvent)
107+
+ return create_task(CLIENT.sync_forever())
108+
+
109+
+
110+
async def send_room_message(room_id, content):
111+
"""Send a message to a room."""
112+
msg = f"Sending room message in {room_id=}: {content=}"

charts/webhook/values.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
replicas: 1
22
image:
3-
repository: "zerodotfive/matrix-webhook"
4-
tag: "rawjson"
3+
repository: "ghcr.io/code-tool/matrix-stack/webhook"
4+
tag: "0.0.2"
55
pullPolicy: IfNotPresent
66
resources: {}
77
nodeSelector: {}

0 commit comments

Comments
 (0)