From 3bebc88ee21438838d43828c4b6c47c2bed96dfc Mon Sep 17 00:00:00 2001 From: Guilhem Saurel Date: Sat, 31 Jul 2021 12:03:26 +0200 Subject: [PATCH] allow direct formatted_body MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This was initially designed and implemented in #6 Co-authored-by: Gerhard Bräunlich --- CHANGELOG.md | 2 ++ matrix_webhook/__main__.py | 7 ++++++- tests/tests.py | 26 ++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 35d0863..baff78f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- add formatted_body to bypass markdown with direct + [matrix-custom-HTML](https://matrix.org/docs/spec/client_server/r0.6.1#m-room-message-msgtypes) - allow "key" to be passed as a parameter - rename "text" to "body". diff --git a/matrix_webhook/__main__.py b/matrix_webhook/__main__.py index 17305cc..005a3eb 100644 --- a/matrix_webhook/__main__.py +++ b/matrix_webhook/__main__.py @@ -54,12 +54,17 @@ async def handler(request): if data["key"] != conf.API_KEY: 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"]) + room_id = request.path[1:] content = { "msgtype": "m.text", "body": data["body"], "format": "org.matrix.custom.html", - "formatted_body": markdown(str(data["body"]), extensions=["extra"]), + "formatted_body": formatted_body, } for _ in range(10): try: diff --git a/tests/tests.py b/tests/tests.py index 2bc97e3..9a82cc9 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -78,6 +78,32 @@ class BotTest(unittest.IsolatedAsyncioTestCase): self.assertEqual(message.body, body) self.assertEqual(message.formatted_body, "

Hello

") + async def test_formatted_body(self): + """Send a formatted message, and check the result.""" + body = "Formatted message" + formatted_body = "markdownFormatted message" + messages = [] + client = nio.AsyncClient(MATRIX_URL, MATRIX_ID) + + await client.login(MATRIX_PW) + room = await client.room_create() + + self.assertEqual( + bot_req( + {"body": body, "formatted_body": formatted_body}, KEY, room.room_id + ), + {"status": 200, "ret": "OK"}, + ) + + sync = await client.sync() + messages = await client.room_messages(room.room_id, sync.next_batch) + await client.close() + + message = messages.chunk[0] + self.assertEqual(message.sender, FULL_ID) + self.assertEqual(message.body, body) + self.assertEqual(message.formatted_body, formatted_body) + async def test_reconnect(self): """Check the reconnecting path.""" client = nio.AsyncClient(MATRIX_URL, MATRIX_ID)