| import json, os
from http import HTTPStatus
from flask import Flask, request
from matrix_client.api import MatrixHttpApi
app = Flask(__name__)
matrix = MatrixHttpApi(os.environ["API_ENDPOINT"], token=os.environ["API_TOKEN"])
@app.route("/", methods=['POST'])
def webhook():
room_id = request.args["room_id"]
matrix.send_message(room_id, format_event(request.json))
return ('', HTTPStatus.NO_CONTENT)
def format_event(event):
message = ""
if "title" in event:
message += event["title"]
if "message" in event:
message += "\n" + event["message"]
if "evalMatches" in event:
matches = event["evalMatches"]
fmatches = []
for match in matches:
alias = (match.get("tags") or {}).get("alias")
value = match.get("value")
if alias and value:
fmatches.append(f"{alias}: {value}")
if len(fmatches) > 0:
message += "\n" + ", ".join(fmatches)
return message
|