2019-02-10 19:29:55 -05:00
|
|
|
#!/usr/bin/env python3
|
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
|
2020-02-16 07:47:00 -05:00
|
|
|
v1: matrix-client & http.server
|
2020-03-14 11:57:43 -04:00
|
|
|
v2: matrix-nio & aiohttp & markdown
|
2019-02-10 16:22:09 -05:00
|
|
|
"""
|
|
|
|
|
2020-02-14 13:18:51 -05:00
|
|
|
import asyncio
|
2019-02-10 19:00:53 -05:00
|
|
|
import json
|
2019-02-10 18:38:08 -05:00
|
|
|
import os
|
2020-03-11 05:08:13 -04:00
|
|
|
from http import HTTPStatus
|
2020-03-08 10:54:50 -04:00
|
|
|
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
|
2020-04-20 13:18:53 -04:00
|
|
|
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
|
2019-02-10 18:38:08 -05:00
|
|
|
|
2021-05-30 16:14:42 -04:00
|
|
|
SERVER_ADDRESS = (os.environ.get('HOST', ''), int(os.environ.get('PORT', 4785)))
|
2019-02-10 19:51:35 -05:00
|
|
|
MATRIX_URL = os.environ.get('MATRIX_URL', 'https://matrix.org')
|
2020-02-16 07:47:00 -05:00
|
|
|
MATRIX_ID = os.environ.get('MATRIX_ID', '@wwm:matrix.org')
|
2019-02-10 19:51:35 -05:00
|
|
|
MATRIX_PW = os.environ['MATRIX_PW']
|
|
|
|
API_KEY = os.environ['API_KEY']
|
2020-02-16 07:47:00 -05:00
|
|
|
CLIENT = AsyncClient(MATRIX_URL, MATRIX_ID)
|
2019-02-10 18:38:08 -05:00
|
|
|
|
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
|
|
|
"""
|
2020-02-16 07:47:00 -05:00
|
|
|
data = await request.read()
|
2020-12-27 08:50:52 -05:00
|
|
|
|
2020-06-08 03:59:51 -04:00
|
|
|
try:
|
|
|
|
data = json.loads(data.decode())
|
|
|
|
except json.decoder.JSONDecodeError:
|
2020-12-27 08:50:52 -05:00
|
|
|
return create_json_response(HTTPStatus.BAD_REQUEST, 'Invalid JSON')
|
|
|
|
|
|
|
|
if not all(key in data for key in ['text', 'key']):
|
2021-06-16 19:23:25 -04:00
|
|
|
return create_json_response(HTTPStatus.BAD_REQUEST, 'Missing text and/or API key property')
|
2020-12-27 08:50:52 -05:00
|
|
|
|
|
|
|
if data['key'] != API_KEY:
|
|
|
|
return create_json_response(HTTPStatus.UNAUTHORIZED, 'Invalid API key')
|
|
|
|
|
2021-05-30 10:40:01 -04:00
|
|
|
room_id = request.path[1:]
|
2020-12-27 08:50:52 -05:00
|
|
|
content = {
|
2020-12-27 08:57:14 -05:00
|
|
|
'msgtype': 'm.text',
|
|
|
|
'body': data['text'],
|
|
|
|
'format': 'org.matrix.custom.html',
|
2021-06-16 19:23:25 -04:00
|
|
|
'formatted_body': markdown(str(data['text']), extensions=['extra']),
|
2020-12-27 08:50:52 -05:00
|
|
|
}
|
|
|
|
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')
|
2020-12-27 08:37:54 -05:00
|
|
|
|
|
|
|
|
|
|
|
def create_json_response(status, ret):
|
|
|
|
"""Create a JSON response."""
|
|
|
|
response_data = {'status': status, 'ret': ret}
|
|
|
|
return web.json_response(response_data, status=status)
|
2019-02-10 16:22:09 -05:00
|
|
|
|
|
|
|
|
2020-12-27 08:11:51 -05:00
|
|
|
async def send_room_message(room_id, content):
|
|
|
|
"""Send a message to a room."""
|
2021-06-16 19:23:25 -04:00
|
|
|
return await CLIENT.room_send(room_id=room_id, message_type='m.room.message', content=content)
|
2020-12-27 08:11:51 -05:00
|
|
|
|
|
|
|
|
2020-03-08 10:54:50 -04:00
|
|
|
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
|
|
|
|
"""
|
|
|
|
await CLIENT.login(MATRIX_PW)
|
|
|
|
|
2020-02-14 13:18:51 -05:00
|
|
|
server = web.Server(handler)
|
|
|
|
runner = web.ServerRunner(server)
|
|
|
|
await runner.setup()
|
|
|
|
site = web.TCPSite(runner, *SERVER_ADDRESS)
|
|
|
|
await site.start()
|
|
|
|
|
2020-03-08 10:54:50 -04:00
|
|
|
# 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."""
|
2020-03-08 10:54:50 -04:00
|
|
|
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."""
|
2020-02-14 13:18:51 -05:00
|
|
|
loop = asyncio.get_event_loop()
|
2020-03-08 10:54:50 -04:00
|
|
|
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
|
|
|
|
|
|
|
loop.close()
|
2020-03-11 05:06:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
run()
|