From 54baf29d513aca87f79e969efc51064d0669f2ad Mon Sep 17 00:00:00 2001 From: Jochen Kupperschmidt Date: Sun, 27 Dec 2020 14:11:51 +0100 Subject: [PATCH] Extract function to send message Merges duplicated code. --- matrix_webhook.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/matrix_webhook.py b/matrix_webhook.py index 9e5fb44..903c4bb 100755 --- a/matrix_webhook.py +++ b/matrix_webhook.py @@ -51,20 +51,23 @@ async def handler(request): "formatted_body": markdown(data['text'], extensions=['extra']), } try: - await CLIENT.room_send(room_id=room_id, - message_type="m.room.message", - content=content) + await send_room_message(room_id, content) except LocalProtocolError: # Connection lost, try another login await CLIENT.login(MATRIX_PW) - await CLIENT.room_send(room_id=room_id, - message_type="m.room.message", - content=content) + await send_room_message(room_id, content) return web.Response(text='{"status": %i, "ret": "%s"}' % (status, ret), content_type='application/json', status=status) +async def send_room_message(room_id, content): + """Send a message to a room.""" + return await CLIENT.room_send(room_id=room_id, + message_type='m.room.message', + content=content) + + async def main(event): """ Launch main coroutine.