matrix-webhook/matrix_webhook/__main__.py

171 lines
4.9 KiB
Python
Raw Normal View History

2019-02-10 16:22:09 -05:00
"""
2020-03-08 17:10:32 -04:00
Matrix Webhook.
2019-02-17 05:46:00 -05:00
Post a message to a matrix room with a simple HTTP POST
2019-02-10 16:22:09 -05:00
"""
2020-02-14 13:18:51 -05:00
import asyncio
import json
2021-07-14 11:12:55 -04:00
import logging
2020-03-11 05:08:13 -04:00
from http import HTTPStatus
from signal import SIGINT, SIGTERM
2019-02-10 16:22:09 -05:00
2020-02-14 13:18:51 -05:00
from aiohttp import web
from markdown import markdown
2020-02-16 07:47:00 -05:00
from nio import AsyncClient
2020-07-28 15:56:05 -04:00
from nio.exceptions import LocalProtocolError
2021-07-14 17:25:24 -04:00
from nio.responses import RoomSendError
2019-02-10 18:38:08 -05:00
from . import conf, formatters
2021-07-18 10:52:39 -04:00
2021-07-14 17:25:24 -04:00
ERROR_MAP = {"M_FORBIDDEN": HTTPStatus.FORBIDDEN}
2019-02-10 18:38:08 -05:00
2021-07-18 10:52:39 -04:00
CLIENT = AsyncClient(conf.MATRIX_URL, conf.MATRIX_ID)
LOGGER = logging.getLogger("matrix-webhook")
2020-02-14 13:18:51 -05:00
async def handler(request):
2019-02-10 18:38:08 -05:00
"""
2020-02-14 13:18:51 -05:00
Coroutine given to the server, st. it knows what to do with an HTTP request.
2020-03-08 17:10:32 -04:00
2020-02-14 13:18:51 -05:00
This one handles a POST, checks its content, and forwards it to the matrix room.
2019-02-10 18:38:08 -05:00
"""
2021-07-14 14:00:52 -04:00
LOGGER.debug(f"Handling {request=}")
2020-02-16 07:47:00 -05:00
data = await request.read()
2020-06-08 03:59:51 -04:00
try:
data = json.loads(data.decode())
except json.decoder.JSONDecodeError:
2021-07-13 05:28:13 -04:00
return create_json_response(HTTPStatus.BAD_REQUEST, "Invalid JSON")
# legacy naming
2021-07-31 05:21:29 -04:00
if "text" in data and "body" not in data:
data["body"] = data["text"]
# allow key to be passed as a parameter
if "key" in request.rel_url.query and "key" not in data:
data["key"] = request.rel_url.query["key"]
if "formatter" in request.rel_url.query:
try:
data = getattr(formatters, request.rel_url.query["formatter"])(data)
except AttributeError:
return create_json_response(HTTPStatus.BAD_REQUEST, "Unknown formatter")
if "room_id" in request.rel_url.query and "room_id" not in data:
data["room_id"] = request.rel_url.query["room_id"]
if "room_id" not in data:
data["room_id"] = request.path.lstrip("/")
missing = []
for key in ["body", "key", "room_id"]:
if key not in data or not data[key]:
missing.append(key)
if missing:
2021-07-13 05:28:13 -04:00
return create_json_response(
HTTPStatus.BAD_REQUEST, f"Missing {', '.join(missing)}"
2021-07-13 05:28:13 -04:00
)
2021-07-18 10:52:39 -04:00
if data["key"] != conf.API_KEY:
2021-07-13 05:28:13 -04:00
return create_json_response(HTTPStatus.UNAUTHORIZED, "Invalid API key")
if "formatted_body" in data:
formatted_body = data["formatted_body"]
else:
formatted_body = markdown(str(data["body"]), extensions=["extra"])
content = {
2021-07-13 05:28:13 -04:00
"msgtype": "m.text",
2021-07-31 05:21:29 -04:00
"body": data["body"],
2021-07-13 05:28:13 -04:00
"format": "org.matrix.custom.html",
"formatted_body": formatted_body,
}
2021-07-14 17:25:24 -04:00
for _ in range(10):
try:
resp = await send_room_message(data["room_id"], content)
2021-07-14 17:25:24 -04:00
if isinstance(resp, RoomSendError):
if resp.status_code == "M_UNKNOWN_TOKEN":
LOGGER.warning("Reconnecting")
2021-07-18 10:52:39 -04:00
await CLIENT.login(conf.MATRIX_PW)
2021-07-14 17:25:24 -04:00
else:
return create_json_response(
ERROR_MAP[resp.status_code], resp.message
)
else:
break
except LocalProtocolError as e:
LOGGER.error(f"Send error: {e}")
LOGGER.warning("Trying again")
else:
return create_json_response(
HTTPStatus.GATEWAY_TIMEOUT, "Homeserver not responding"
)
2021-07-13 05:28:13 -04:00
return create_json_response(HTTPStatus.OK, "OK")
def create_json_response(status, ret):
"""Create a JSON response."""
2021-07-14 14:00:52 -04:00
LOGGER.debug(f"Creating json response: {status=}, {ret=}")
2021-07-13 05:28:13 -04:00
response_data = {"status": status, "ret": ret}
return web.json_response(response_data, status=status)
2019-02-10 16:22:09 -05:00
async def send_room_message(room_id, content):
"""Send a message to a room."""
2021-07-14 14:00:52 -04:00
LOGGER.debug(f"Sending room message in {room_id=}: {content=}")
2021-07-13 05:28:13 -04:00
return await CLIENT.room_send(
room_id=room_id, message_type="m.room.message", content=content
)
async def main(event):
2020-02-16 07:47:00 -05:00
"""
2020-03-08 17:10:32 -04:00
Launch main coroutine.
2020-02-16 07:47:00 -05:00
matrix client login & start web server
"""
2021-07-18 10:52:39 -04:00
LOGGER.info(f"Log in {conf.MATRIX_ID=} on {conf.MATRIX_URL=}")
await CLIENT.login(conf.MATRIX_PW)
2020-02-16 07:47:00 -05:00
2020-02-14 13:18:51 -05:00
server = web.Server(handler)
runner = web.ServerRunner(server)
await runner.setup()
2021-07-18 10:52:39 -04:00
LOGGER.info(f"Binding on {conf.SERVER_ADDRESS=}")
site = web.TCPSite(runner, *conf.SERVER_ADDRESS)
2020-02-14 13:18:51 -05:00
await site.start()
# Run until we get a shutdown request
await event.wait()
# Cleanup
await runner.cleanup()
await CLIENT.close()
def terminate(event, signal):
2020-03-08 17:10:32 -04:00
"""Close handling stuff."""
event.set()
2020-03-11 05:06:53 -04:00
asyncio.get_event_loop().remove_signal_handler(signal)
2019-02-10 16:22:09 -05:00
2020-03-11 05:06:53 -04:00
def run():
"""Launch everything."""
2021-07-14 14:00:52 -04:00
LOGGER.info("Starting...")
2020-02-14 13:18:51 -05:00
loop = asyncio.get_event_loop()
event = asyncio.Event()
for sig in (SIGINT, SIGTERM):
loop.add_signal_handler(sig, terminate, event, sig)
loop.run_until_complete(main(event))
2020-02-14 13:18:51 -05:00
2021-07-14 14:00:52 -04:00
LOGGER.info("Closing...")
2020-02-14 13:18:51 -05:00
loop.close()
2020-03-11 05:06:53 -04:00
2021-07-13 05:28:13 -04:00
if __name__ == "__main__":
2021-07-14 14:00:52 -04:00
log_format = "%(asctime)s - %(name)s - %(lineno)d - %(levelname)s - %(message)s"
2021-07-18 10:52:39 -04:00
logging.basicConfig(level=50 - 10 * conf.VERBOSE, format=log_format)
2020-03-11 05:06:53 -04:00
run()