update "text" key to "body"
This commit is contained in:
parent
8b32c972b8
commit
292d77274d
5 changed files with 39 additions and 11 deletions
|
@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
- update "text" key to "body".
|
||||||
|
|
||||||
## [3.1.0] - 2021-07-18
|
## [3.1.0] - 2021-07-18
|
||||||
|
|
||||||
- Publish on PyPI & Docker Hub with Github Actions
|
- Publish on PyPI & Docker Hub with Github Actions
|
||||||
|
|
|
@ -74,7 +74,7 @@ docker-compose up -d
|
||||||
## Test / Usage
|
## Test / Usage
|
||||||
|
|
||||||
```
|
```
|
||||||
curl -d '{"text":"new contrib from toto: [44](http://radio.localhost/map/#44)", "key": "secret"}' \
|
curl -d '{"body":"new contrib from toto: [44](http://radio.localhost/map/#44)", "key": "secret"}' \
|
||||||
'http://matrixwebhook.localhost/!DPrUlnwOhBEfYwsDLh:matrix.org'
|
'http://matrixwebhook.localhost/!DPrUlnwOhBEfYwsDLh:matrix.org'
|
||||||
```
|
```
|
||||||
(or localhost:4785 without docker)
|
(or localhost:4785 without docker)
|
||||||
|
|
|
@ -38,9 +38,13 @@ async def handler(request):
|
||||||
except json.decoder.JSONDecodeError:
|
except json.decoder.JSONDecodeError:
|
||||||
return create_json_response(HTTPStatus.BAD_REQUEST, "Invalid JSON")
|
return create_json_response(HTTPStatus.BAD_REQUEST, "Invalid JSON")
|
||||||
|
|
||||||
if not all(key in data for key in ["text", "key"]):
|
# legacy naming:
|
||||||
|
if "text" in data and "body" not in data:
|
||||||
|
data["body"] = data["text"]
|
||||||
|
|
||||||
|
if not all(key in data for key in ["body", "key"]):
|
||||||
return create_json_response(
|
return create_json_response(
|
||||||
HTTPStatus.BAD_REQUEST, "Missing text and/or API key property"
|
HTTPStatus.BAD_REQUEST, "Missing body and/or API key property"
|
||||||
)
|
)
|
||||||
|
|
||||||
if data["key"] != conf.API_KEY:
|
if data["key"] != conf.API_KEY:
|
||||||
|
@ -49,9 +53,9 @@ async def handler(request):
|
||||||
room_id = request.path[1:]
|
room_id = request.path[1:]
|
||||||
content = {
|
content = {
|
||||||
"msgtype": "m.text",
|
"msgtype": "m.text",
|
||||||
"body": data["text"],
|
"body": data["body"],
|
||||||
"format": "org.matrix.custom.html",
|
"format": "org.matrix.custom.html",
|
||||||
"formatted_body": markdown(str(data["text"]), extensions=["extra"]),
|
"formatted_body": markdown(str(data["body"]), extensions=["extra"]),
|
||||||
}
|
}
|
||||||
for _ in range(10):
|
for _ in range(10):
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -88,7 +88,7 @@ def run_and_test():
|
||||||
srv.terminate()
|
srv.terminate()
|
||||||
|
|
||||||
# TODO Check what the bot says when the server is offline
|
# TODO Check what the bot says when the server is offline
|
||||||
# print(bot_req({'text': 'bye'}, KEY), {'status': 200, 'ret': 'OK'})
|
# print(bot_req({'data': 'bye'}, KEY), {'status': 200, 'ret': 'OK'})
|
||||||
|
|
||||||
LOGGER.info("Stopping the bot")
|
LOGGER.info("Stopping the bot")
|
||||||
bot.terminate()
|
bot.terminate()
|
||||||
|
|
|
@ -15,19 +15,19 @@ class BotTest(unittest.IsolatedAsyncioTestCase):
|
||||||
self.assertEqual(bot_req(), {"status": 400, "ret": "Invalid JSON"})
|
self.assertEqual(bot_req(), {"status": 400, "ret": "Invalid JSON"})
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
bot_req({"toto": 3}),
|
bot_req({"toto": 3}),
|
||||||
{"status": 400, "ret": "Missing text and/or API key property"},
|
{"status": 400, "ret": "Missing body and/or API key property"},
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
bot_req({"text": 3, "key": None}), {"status": 401, "ret": "Invalid API key"}
|
bot_req({"body": 3, "key": None}), {"status": 401, "ret": "Invalid API key"}
|
||||||
)
|
)
|
||||||
# TODO: if the client from matrix_webhook has olm support, this won't be a 403 from synapse,
|
# TODO: if the client from matrix_webhook has olm support, this won't be a 403 from synapse,
|
||||||
# but a LocalProtocolError from matrix_webhook
|
# but a LocalProtocolError from matrix_webhook
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
bot_req({"text": 3}, KEY), {"status": 403, "ret": "Unknown room"}
|
bot_req({"body": 3}, KEY), {"status": 403, "ret": "Unknown room"}
|
||||||
)
|
)
|
||||||
|
|
||||||
async def test_message(self):
|
async def test_message(self):
|
||||||
"""Send a markdown message, and check the result."""
|
"""Send a markdown message with the old format, and check the result."""
|
||||||
text = "# Hello"
|
text = "# Hello"
|
||||||
messages = []
|
messages = []
|
||||||
client = nio.AsyncClient(MATRIX_URL, MATRIX_ID)
|
client = nio.AsyncClient(MATRIX_URL, MATRIX_ID)
|
||||||
|
@ -48,6 +48,28 @@ class BotTest(unittest.IsolatedAsyncioTestCase):
|
||||||
self.assertEqual(message.body, text)
|
self.assertEqual(message.body, text)
|
||||||
self.assertEqual(message.formatted_body, "<h1>Hello</h1>")
|
self.assertEqual(message.formatted_body, "<h1>Hello</h1>")
|
||||||
|
|
||||||
|
async def test_markdown_body(self):
|
||||||
|
"""Send a markdown message, and check the result."""
|
||||||
|
body = "# Hello"
|
||||||
|
messages = []
|
||||||
|
client = nio.AsyncClient(MATRIX_URL, MATRIX_ID)
|
||||||
|
|
||||||
|
await client.login(MATRIX_PW)
|
||||||
|
room = await client.room_create()
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
bot_req({"body": 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, "<h1>Hello</h1>")
|
||||||
|
|
||||||
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)
|
||||||
|
@ -56,6 +78,6 @@ class BotTest(unittest.IsolatedAsyncioTestCase):
|
||||||
await client.logout(all_devices=True)
|
await client.logout(all_devices=True)
|
||||||
await client.close()
|
await client.close()
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
bot_req({"text": "Re"}, KEY, room.room_id),
|
bot_req({"body": "Re"}, KEY, room.room_id),
|
||||||
{"status": 200, "ret": "OK"},
|
{"status": 200, "ret": "OK"},
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue