allow direct formatted_body

This was initially designed and implemented in #6

Co-authored-by: Gerhard Bräunlich <gerhard.braeunlich@id.ethz.ch>
This commit is contained in:
Guilhem Saurel 2021-07-31 12:03:26 +02:00
parent fa8f9b4a51
commit 3bebc88ee2
3 changed files with 34 additions and 1 deletions

View file

@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [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 - allow "key" to be passed as a parameter
- rename "text" to "body". - rename "text" to "body".

View file

@ -54,12 +54,17 @@ async def handler(request):
if data["key"] != conf.API_KEY: if data["key"] != conf.API_KEY:
return create_json_response(HTTPStatus.UNAUTHORIZED, "Invalid 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:] room_id = request.path[1:]
content = { content = {
"msgtype": "m.text", "msgtype": "m.text",
"body": data["body"], "body": data["body"],
"format": "org.matrix.custom.html", "format": "org.matrix.custom.html",
"formatted_body": markdown(str(data["body"]), extensions=["extra"]), "formatted_body": formatted_body,
} }
for _ in range(10): for _ in range(10):
try: try:

View file

@ -78,6 +78,32 @@ class BotTest(unittest.IsolatedAsyncioTestCase):
self.assertEqual(message.body, body) self.assertEqual(message.body, body)
self.assertEqual(message.formatted_body, "<h1>Hello</h1>") self.assertEqual(message.formatted_body, "<h1>Hello</h1>")
async def test_formatted_body(self):
"""Send a formatted message, and check the result."""
body = "Formatted message"
formatted_body = "<del>markdown</del><strong>Formatted</strong> 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): async def test_reconnect(self):
"""Check the reconnecting path.""" """Check the reconnecting path."""
client = nio.AsyncClient(MATRIX_URL, MATRIX_ID) client = nio.AsyncClient(MATRIX_URL, MATRIX_ID)