mirror of
https://github.com/kuhyx/signal-bot.git
synced 2026-11-24 03:26:31 +01:00
One row per live command on a new Commands tab of the management page,
each with an on/off switch, an optional "off until" moment and a cooldown
that overrides the bot's default for that command alone. Every switch is
live: the bot keeps them in its own SQLite (command_settings, a permanent
table) and reads the row on every attempt, so nothing goes through
/_reload and nothing restarts.
A command that is off answers "BEEP BOOP !kot WYŁĄCZONE." (with "DO HH:MM"
for a timed switch) rather than going quiet, so nobody reports it broken;
the refusal is filed as refused_off, which the analytics count with the
other refusals, and it neither starts the cooldown nor counts as a use.
The bot grows GET /_commands and PUT /_commands/{name}; the management app
proxies them under /api/commands and passes the bot's 404/503 through
unwrapped. !cisza and !odcisz carry no cooldown and the page says so
instead of accepting a number the bot would ignore (test_wiring_ungated
keeps that set honest against the real table).
Proven end to end: scripts/e2e.sh now brackets a PUT with switch_off.yaml
and switch_on.yaml, the same actor refused and then served, with no reload
between them. Also bumps vitest and @vitest/coverage-v8 to 5.0.0 and
@types/react-dom to 19.2.7, which the freshness gate asked for.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
81 lines
2.8 KiB
Python
81 lines
2.8 KiB
Python
# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
|
|
"""The switchboard, proxied.
|
|
|
|
The page reads the bot's database read-only everywhere else, and it stays
|
|
that way here: a switch is written by the *bot*, through its own loopback
|
|
route, so there is exactly one process with the database open for writing
|
|
and the live command table the switches are joined against is the one the
|
|
dispatcher is actually using. This module forwards and passes the answer
|
|
on, the way ``/api/selftest`` does.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
import httpx
|
|
from fastapi import APIRouter, HTTPException, status
|
|
from pydantic import BaseModel
|
|
|
|
if TYPE_CHECKING:
|
|
from signal_bot.manage.deps import ManageDeps
|
|
|
|
|
|
class Setting(BaseModel):
|
|
"""What the page may decide about one command; mirrors the bot's request."""
|
|
|
|
enabled: bool = True
|
|
disabled_until: float | None = None
|
|
cooldown_seconds: float | None = None
|
|
|
|
|
|
def router(deps: ManageDeps) -> APIRouter:
|
|
"""The list and the per-command PUT, both forwarded to the bot.
|
|
|
|
Args:
|
|
deps: The running app's configuration and shared HTTP client.
|
|
|
|
Returns:
|
|
A router to include in the management app.
|
|
"""
|
|
api = APIRouter(prefix="/api")
|
|
|
|
async def forward(request: httpx.Request) -> dict[str, Any]:
|
|
"""Send one request to the bot and hand back its body, verbatim."""
|
|
try:
|
|
response = await deps.client.send(request)
|
|
except httpx.HTTPError as error:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_502_BAD_GATEWAY,
|
|
detail=f"the bot did not answer: {error}",
|
|
) from error
|
|
body: dict[str, Any] = response.json()
|
|
# 503 while connecting and 404 for a name the table does not hold
|
|
# are the bot's own sentences, passed on unchanged -- unwrapped
|
|
# from the bot's own ``detail`` so this does not nest one inside
|
|
# another on the way through.
|
|
if response.status_code != status.HTTP_200_OK:
|
|
raise HTTPException(status_code=response.status_code, detail=body.get("detail", body))
|
|
return body
|
|
|
|
@api.get("/commands")
|
|
async def list_commands() -> dict[str, Any]:
|
|
"""Every live command with its switches."""
|
|
return await forward(
|
|
deps.client.build_request("GET", f"{deps.bot_url}/_commands", timeout=10.0)
|
|
)
|
|
|
|
@api.put("/commands/{name}")
|
|
async def put_command(name: str, setting: Setting) -> dict[str, Any]:
|
|
"""Store one command's switches, live."""
|
|
return await forward(
|
|
deps.client.build_request(
|
|
"PUT",
|
|
f"{deps.bot_url}/_commands/{name}",
|
|
json=setting.model_dump(),
|
|
timeout=10.0,
|
|
)
|
|
)
|
|
|
|
return api
|