Alerts: a Telegram channel, one chat or group each, connected by a one-time link
The channel sends through the Bot API with httpx only: HTML with escaped text, cut to fit a message, silent for a warning, a short 429 waited out once, and errors that say what to do about a blocked bot, a lost chat or a bad token and never quote the request URL. The bot token is a stored secret: saving a channel refuses a literal one, the card's "Store token" writes the secret and a reference, and no endpoint returns it. A chat id is never typed. Connect starts a ten minute code, and the chat that sends `/start <code>` back through the bot is the one connected, so it is a chat somebody opened a link for. The engine fetches updates only while a code is waiting (no standing poll, no webhook, no inbound address) and ignores everything else without a reply. Connect needs a saved form, and the form takes the connected chat in without touching what was typed since. The token is part of every Bot API URL, so the httpx logger gets the same filter the request lines have, which now also masks an httpx.URL argument.
This commit is contained in:
@@ -61,11 +61,13 @@ class Channel(BaseModel):
|
||||
"""Somewhere to send an alert."""
|
||||
|
||||
name: str
|
||||
kind: Literal["ntfy", "smtp", "webhook", "dashboard", "webpush"]
|
||||
kind: Literal["ntfy", "smtp", "webhook", "dashboard", "webpush", "telegram"]
|
||||
enabled: bool = True
|
||||
# ntfy: server + topic + optional token. smtp: to. webhook: url.
|
||||
# dashboard: message — the record a notification widget reads.
|
||||
# webpush: nothing — it goes to whichever browsers subscribed here.
|
||||
# telegram: token (always a secret reference) + chat_id and label, which
|
||||
# connecting a chat fills in. One channel is one chat.
|
||||
config: dict[str, Any] = Field(default_factory=dict)
|
||||
|
||||
|
||||
@@ -414,6 +416,8 @@ class AlertManager:
|
||||
await self._send_dashboard(config, alert)
|
||||
elif channel.kind == "webpush":
|
||||
await self._send_webpush(alert)
|
||||
elif channel.kind == "telegram":
|
||||
await self._send_telegram(config, alert)
|
||||
else:
|
||||
await self._send_email(config, alert)
|
||||
except Exception as exc:
|
||||
@@ -466,6 +470,23 @@ class AlertManager:
|
||||
if await webpush.send_to_all(alert.model_dump()) == 0:
|
||||
raise ValueError("No browsers are subscribed")
|
||||
|
||||
async def _send_telegram(self, config: dict[str, Any], alert: Alert) -> None:
|
||||
from fluksio.flow import telegram
|
||||
|
||||
token = config.get("token")
|
||||
if not token:
|
||||
raise ValueError("a telegram channel needs a bot token")
|
||||
chat_id = config.get("chat_id")
|
||||
if not chat_id:
|
||||
raise ValueError("this telegram channel is not connected to a chat yet")
|
||||
await telegram.send_message(
|
||||
str(token),
|
||||
str(chat_id),
|
||||
alert.title,
|
||||
alert.body,
|
||||
quiet=alert.severity == "warning",
|
||||
)
|
||||
|
||||
async def _send_webhook(self, config: dict[str, Any], alert: Alert) -> None:
|
||||
url = config.get("url")
|
||||
if not url:
|
||||
|
||||
Reference in New Issue
Block a user