diff --git a/matrix_webhook.py b/matrix_webhook.py index 12f47d2..6a88346 100755 --- a/matrix_webhook.py +++ b/matrix_webhook.py @@ -33,30 +33,33 @@ async def handler(request): This one handles a POST, checks its content, and forwards it to the matrix room. """ data = await request.read() + try: data = json.loads(data.decode()) - status, ret = HTTPStatus.BAD_REQUEST, 'I need a json dict with text & key' except json.decoder.JSONDecodeError: - data = {} - status, ret = HTTPStatus.BAD_REQUEST, 'This was not valid JSON' - if all(key in data for key in ['text', 'key']): - status, ret = HTTPStatus.UNAUTHORIZED, 'I need the good "key"' - if data['key'] == API_KEY: - status, ret = HTTPStatus.OK, 'OK' - room_id = str(request.rel_url)[1:] - content = { - "msgtype": "m.text", - "body": data['text'], - "format": "org.matrix.custom.html", - "formatted_body": markdown(data['text'], extensions=['extra']), - } - try: - await send_room_message(room_id, content) - except LocalProtocolError: # Connection lost, try another login - await CLIENT.login(MATRIX_PW) - await send_room_message(room_id, content) + return create_json_response(HTTPStatus.BAD_REQUEST, 'Invalid JSON') - return create_json_response(status, ret) + if not all(key in data for key in ['text', 'key']): + return create_json_response(HTTPStatus.BAD_REQUEST, + 'Missing text and/or API key property') + + if data['key'] != API_KEY: + return create_json_response(HTTPStatus.UNAUTHORIZED, 'Invalid API key') + + room_id = str(request.rel_url)[1:] + content = { + "msgtype": "m.text", + "body": data['text'], + "format": "org.matrix.custom.html", + "formatted_body": markdown(data['text'], extensions=['extra']), + } + try: + await send_room_message(room_id, content) + except LocalProtocolError: # Connection lost, try another login + await CLIENT.login(MATRIX_PW) + await send_room_message(room_id, content) + + return create_json_response(HTTPStatus.OK, 'OK') def create_json_response(status, ret):