Coverage for notifications/adapters/slack.py: 100.00%
25 statements
« prev ^ index » next coverage.py v7.10.6, created at 2026-11-30 18:53 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2026-11-30 18:53 +0000
1"""Slack, through a per-channel incoming webhook."""
3from __future__ import annotations
5from typing import ClassVar
7import httpx
9from notifications.adapters.base import ChannelAdapter, DeliveryResult
11# Slack parses <...> as links and mentions, so a player named "<!channel>" would
12# ping the room. Ampersand first, or it would re-escape the other two.
13_ESCAPES = (("&", "&"), ("<", "<"), (">", ">"))
14_TIMEOUT_SECONDS = 5.0
17def escape(text: str) -> str:
18 for char, entity in _ESCAPES:
19 text = text.replace(char, entity)
20 return text
23class SlackAdapter(ChannelAdapter):
24 id: ClassVar[str] = "slack"
25 label: ClassVar[str] = "Slack"
26 icon: ClassVar[str] = "#️⃣"
27 invite_pattern: ClassVar[str] = (
28 r"^https://join\.slack\.com/t/[A-Za-z0-9-]{1,80}/shared_invite/[A-Za-z0-9_-]{4,200}$"
29 )
30 invite_error: ClassVar[str] = "Must be a Slack invite link (https://join.slack.com/t/.../shared_invite/...)"
31 placeholder: ClassVar[str] = "https://hooks.slack.com/services/…"
32 hint: ClassVar[str] = "Add an Incoming Webhook to your Slack channel and paste the full webhook URL."
33 setup_steps: ClassVar[tuple[str, ...]] = (
34 "Go to api.slack.com/apps → Create New App → Pick features, name it Missing Table and pick your "
35 "workspace, or open the app you already made.",
36 "Under Features → Incoming Webhooks, turn Activate Incoming Webhooks on.",
37 "Click Add New Webhook to Workspace, pick the channel and click Authorize. If the channel is set to "
38 "Private in Slack, join it first or it is not listed. Each webhook posts to one channel.",
39 "Copy the Webhook URL (it starts https://hooks.slack.com/services/) and paste it here. "
40 "Treat it like a password.",
41 "Then click Send test to confirm a message arrives.",
42 )
43 # Exact host and shape: the backend POSTs here, so this is the SSRF guard.
44 destination_pattern: ClassVar[str] = r"^https://hooks\.slack\.com/services/[A-Za-z0-9]+/[A-Za-z0-9]+/[A-Za-z0-9]+$"
45 destination_error: ClassVar[str] = "Slack destination must be an incoming webhook URL"
46 # Fans only (SB-1673): Slack's terms require users to be over 16, and U17
47 # squads include 15-year-olds. Players go on Discord.
48 min_player_age: ClassVar[int | None] = None
49 # No Capability.THREADS: a webhook does not return the `ts` a thread needs.
51 def _deliver(self, destination: str, content: str) -> DeliveryResult:
52 response = httpx.post(
53 destination,
54 json={"text": escape(content), "mrkdwn": False},
55 timeout=_TIMEOUT_SECONDS,
56 )
57 response.raise_for_status()
58 return DeliveryResult(message_ref=None)