Coverage for notifications/invite_request_alerts.py: 94.34%
43 statements
« prev ^ index » next coverage.py v7.10.6, created at 2026-11-30 15:10 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2026-11-30 15:10 +0000
1"""Admin ping when a new invite request arrives (SB-1313).
3A request sitting unseen in the admin panel is a prospective user waiting on
4nobody. This sends one short message to the operator's Telegram chat and/or
5Discord channel per *new* `invite_requests` row; honeypot hits and duplicate
6pending submissions never get here.
8Destinations are the operator's, not a club's, so they come from env rather
9than `club_notifications`:
11- `MT_ADMIN_TELEGRAM_CHAT_ID` — same chat the ingest alerts use. Sent with the
12 existing `TELEGRAM_BOT_TOKEN`.
13- `MT_ADMIN_DISCORD_WEBHOOK_URL` — a webhook into an admin-only channel.
15Either, both or neither may be set. Neither is a quiet no-op.
16"""
18from __future__ import annotations
20import os
22import structlog
24from notifications.senders import send_to
26logger = structlog.get_logger()
28TELEGRAM_CHAT_ID_ENV = "MT_ADMIN_TELEGRAM_CHAT_ID"
29DISCORD_WEBHOOK_ENV = "MT_ADMIN_DISCORD_WEBHOOK_URL"
32def _admin_destinations() -> list[tuple[str, str]]:
33 destinations = []
34 chat_id = os.getenv(TELEGRAM_CHAT_ID_ENV)
35 if chat_id:
36 destinations.append(("telegram", chat_id))
37 webhook = os.getenv(DISCORD_WEBHOOK_ENV)
38 if webhook:
39 destinations.append(("discord", webhook))
40 return destinations
43def _defang(value: str) -> str:
44 # Name and team are typed by the public. A zero-width space after "@"
45 # keeps "@everyone" in a name from pinging a whole Discord server.
46 return value.replace("@", "@")
49def format_invite_request_alert(name: str, team: str | None, wants_ios_beta: bool) -> str:
50 base_url = os.getenv("APP_BASE_URL", "https://missingtable.com").rstrip("/")
51 lines = [
52 "📬 New MT invite request",
53 f"Name: {_defang(name)}",
54 f"Team: {_defang(team) if team else '—'}",
55 ]
56 if wants_ios_beta:
57 lines.append("iPhone beta: yes")
58 # The admin panel has no URL deep link; Requests is its first section.
59 lines.append(f"Review: {base_url}/ (Admin → Requests)")
60 return "\n".join(lines)
63def notify_new_invite_request(name: str, team: str | None, wants_ios_beta: bool = False) -> int:
64 """Ping each configured admin destination. Returns how many sends succeeded.
66 Never raises: this runs after the requester already has their response,
67 and a broken ping must not turn into a failed request or a noisy task.
68 """
69 destinations = _admin_destinations()
70 if not destinations:
71 logger.info(
72 "Invite request not pinged — neither %s nor %s is set",
73 TELEGRAM_CHAT_ID_ENV,
74 DISCORD_WEBHOOK_ENV,
75 )
76 return 0
78 try:
79 content = format_invite_request_alert(name, team, wants_ios_beta)
80 except Exception:
81 logger.exception("Could not format invite request alert")
82 return 0
84 sent = 0
85 for platform, destination in destinations:
86 try:
87 send_to(platform, destination, content)
88 sent += 1
89 except Exception:
90 # Destination values are never logged.
91 logger.exception("Could not ping admins about invite request", platform=platform)
92 return sent