diff --git a/.gitignore b/.gitignore index 0f2687a..87c8525 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +config.yml + # Created by https://www.gitignore.io/api/python,pycharm,virtualenv # Edit at https://www.gitignore.io/?templates=python,pycharm,virtualenv diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4272430 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +PyYAML>=5.1,<6 +Flask>=1.1.1,<2 +matrix-client>=0.3.2,<0.4 \ No newline at end of file diff --git a/wmn.py b/wmn.py new file mode 100644 index 0000000..bf73785 --- /dev/null +++ b/wmn.py @@ -0,0 +1,31 @@ +import yaml +from flask import Flask, request, abort +from matrix_client.client import MatrixClient + +app = Flask(__name__) + +with open("config.yml", 'r') as ymlfile: + cfg = yaml.safe_load(ymlfile) + + +@app.route('/matrix') +def notify(): + channel = request.args.get('channel') + if channel is None or len(channel) == 0: + abort(401) + print(f"[DEBUG] Channel: {channel}") + gitlab_token = request.headers.get('X-Gitlab-Token') + if gitlab_token is None or len(gitlab_token) == 0 or gitlab_token != cfg['secret']: + abort(403) + print("[DEBUG] Correct secret") + + server = cfg["matrix"]["server"] + client = MatrixClient(server) + client.login(username=cfg["matrix"]["username"], password=cfg["matrix"]["password"]) + print(f"[DEBUG] Connected to matrix server {server}") + + room = client.join_room(room_id_or_alias=channel) + room.send_text("Hello!") + print(f"[DEBUG] Sent text to channel {room}") + + return ""