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:
2026-10-28 13:06:58 +01:00
parent bfaaa7cd03
commit 5d0ea480bf
14 changed files with 1462 additions and 11 deletions
+11 -4
View File
@@ -12,6 +12,7 @@ from contextlib import AbstractAsyncContextManager, asynccontextmanager
from pathlib import Path
from typing import Any
import httpx
from fastapi import FastAPI, Request
from fastapi.concurrency import run_in_threadpool
from fastapi.responses import JSONResponse
@@ -29,7 +30,7 @@ from fluksio.core import security
from fluksio.core.config import settings
from fluksio.core.db import engine as db_engine
from fluksio.core.db import prepare
from fluksio.flow import logs, modules
from fluksio.flow import logs, modules, telegram
from fluksio.flow.alert_ledger import AlertLedger
from fluksio.flow.alerts import AlertManager
from fluksio.flow.artifacts import ArtifactStore, VolatileStore
@@ -74,7 +75,9 @@ _SECRET_IN_HOOK_PATH = re.compile(r"(/hooks/[^/\s?]+/(?:[^/\s?]*/)*)[^/\s?]+")
def _redact(text: str) -> str:
return _SECRET_IN_HOOK_PATH.sub(r"\1***", _CREDENTIAL_IN_QUERY.sub(r"\1***", text))
return telegram.redact(
_SECRET_IN_HOOK_PATH.sub(r"\1***", _CREDENTIAL_IN_QUERY.sub(r"\1***", text))
)
class _RedactTokens(logging.Filter):
@@ -87,15 +90,19 @@ class _RedactTokens(logging.Filter):
def filter(self, record: logging.LogRecord) -> bool:
if isinstance(record.args, tuple):
# httpx logs its URL as an `httpx.URL`, not a string.
record.args = tuple(
_redact(arg) if isinstance(arg, str) else arg for arg in record.args
_redact(str(arg)) if isinstance(arg, str | httpx.URL) else arg
for arg in record.args
)
return True
# The handshake lines go to `uvicorn.error`, the request lines to `uvicorn.access`,
# and the tunnel connector names the path of a call it could not proxy.
for _name in ("uvicorn.access", "uvicorn.error", "fluksio.cloud.connector"):
# httpx logs every request it makes at INFO, with the URL, and a Telegram bot's
# token is part of that URL.
for _name in ("uvicorn.access", "uvicorn.error", "fluksio.cloud.connector", "httpx"):
logging.getLogger(_name).addFilter(_RedactTokens())