Answer product-owner round 1: 0 🔴, 0 🟡 (0 misread); escape the characters Slack reads as control characters, and say the DM isn't only Mattermost's

This commit is contained in:
2026-11-14 23:36:30 +01:00
parent 6a553b9574
commit a8d0a9011c
3 changed files with 10 additions and 4 deletions
+4 -3
View File
@@ -1,5 +1,6 @@
"""Sends a direct message from a Slack app's bot to one user, through Slack's Web API. `notifier` picks it where a project's config names `slack`."""
"""Sends a direct message from a Slack app's bot to one user, through Slack's Web API. The `notifier` setting `slack` picks it."""
import dataclasses
import html
import pathlib
from failure import Failure
@@ -29,8 +30,8 @@ def read_config(path: pathlib.Path) -> Config | Failure:
def send(config: Config, message: str, http: Http) -> Failure | None:
"""Posts message to the recipient's member ID, which Slack opens as a DM from the app's bot; the bot needs the `chat:write` scope."""
answer = http("POST", f"{API}/chat.postMessage", config.token, {"channel": config.recipient, "text": message})
"""Posts message to the recipient's member ID, which Slack opens as a DM from the app's bot; the bot needs the `chat:write` scope. Slack reads `&`, `<` and `>` as control characters, so they are escaped as its docs ask."""
answer = http("POST", f"{API}/chat.postMessage", config.token, {"channel": config.recipient, "text": html.escape(message, quote=False)})
if isinstance(answer, Failure):
return answer
if isinstance(answer, dict) and answer.get("ok") is True:
+5
View File
@@ -51,6 +51,11 @@ class Send(unittest.TestCase):
self.assertIsNone(dm.send(CONFIG, "Which one?", slack))
self.assertEqual(slack.requests, [("POST", "https://slack.com/api/chat.postMessage", "xoxb-secret", {"channel": "U0123ABCD", "text": "Which one?"})])
def test_escapes_the_three_characters_slack_reads_as_control_characters(self):
slack = Slack({"ok": True})
self.assertIsNone(dm.send(CONFIG, "Use <b> & [<setting>]?", slack))
self.assertEqual(slack.requests[0][3], {"channel": "U0123ABCD", "text": "Use &lt;b&gt; &amp; [&lt;setting&gt;]?"})
def test_fails_naming_slacks_error_code_where_slack_answers_ok_false(self):
self.assertEqual(dm.send(CONFIG, "hi", Slack({"ok": False, "error": "invalid_auth"})), Failure("POST /api/chat.postMessage refused the message: invalid_auth"))