mirror of
https://github.com/kuhyx/signal-bot.git
synced 2026-11-24 06:36:19 +01:00
Per-person photo commands, where the mechanism is public and the album is not. Nothing identifying is in this repository: who the people are, what fires their photos and which file shows whom all live in one directory outside it, and the tests, the docs and the offline e2e run use three invented people instead. Several names in one message intersect -- "!alfa beta" is a photo with both -- and where an entry offers a choice the bot picks among the readings that actually have a photo, so a question the album could answer is not refused. A combination nobody photographed gets one line rather than silence, which from the group's side is indistinguishable from a broken bot. Scoping is enforced in the dispatcher, not in the handler: a command the group is not allowed resolves to nothing, so it is not answered, not counted as a command, and never named in a log line outside its own groups. A handler that quietly returned would leak all three. Commands are named by pseudonym (pack:p1), so the journal and command_uses say somebody asked for a photo and nothing about whose. Video works: the attachment goes out as data:video/mp4;filename=...;base64, which is the container's own spelling, read off its /swagger/doc.json. Told it is a photo, Signal renders a clip as an unplayable file. tool/tagger.py serves one page on loopback to fill the album in -- names are typed into the page, never as arguments, and it prints how many photos each command would have when it stops, because a command with zero is dead on arrival. Also renames the real people who were still sitting in the stats test fixtures. They remain in the history; the point is that nothing new is added to it. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017w1HZ3cZ7neL3rFdQ6VMBA
87 lines
3.0 KiB
Python
87 lines
3.0 KiB
Python
# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
|
|
"""Choosing the two or three things worth saying about a day.
|
|
|
|
There are around twenty things the bot could say every night. Saying all of
|
|
them is a wall of text nobody reads, and saying whichever three happen to be
|
|
computed first is arbitrary. So every candidate declares a priority and a
|
|
magnitude, and the picker takes the top few.
|
|
|
|
Two rules make the result readable rather than merely short:
|
|
|
|
* **One per family.** "Ala set a personal record", "Ala had 60% of the
|
|
day" and "Ala kept the crown" are three sentences about the same thing.
|
|
Families collapse them to the strongest.
|
|
* **Fully deterministic.** No clock, no randomness, and ties break on the
|
|
candidate's own name, so the same day always produces the same post. A
|
|
nightly report that reshuffles itself between two renders reads as a bug.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
from typing import TYPE_CHECKING, Final
|
|
|
|
if TYPE_CHECKING:
|
|
from collections.abc import Sequence
|
|
|
|
# Families. Anything in the same one is a restatement of the same fact.
|
|
RECORD: Final = "record"
|
|
CROWN: Final = "crown"
|
|
TREND: Final = "trend"
|
|
TIMING: Final = "timing"
|
|
VOICE: Final = "voice"
|
|
|
|
# How many highlight lines a day block has room for. Lives here rather than
|
|
# beside either caller, so the nightly post and !wczoraj cannot drift apart.
|
|
SLOTS: Final = 3
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class Highlight:
|
|
"""One thing worth saying, and how much it wants saying."""
|
|
|
|
kind: str
|
|
family: str
|
|
priority: int
|
|
magnitude: float
|
|
text: str
|
|
|
|
|
|
def select(candidates: Sequence[Highlight | None], slots: int) -> list[Highlight]:
|
|
"""Pick at most ``slots`` highlights, strongest first, one per family.
|
|
|
|
Args:
|
|
candidates: Every candidate, with None for the ones that did not
|
|
fire. Callers pass the whole list rather than filtering, so a
|
|
candidate that never fires is still visible in the code.
|
|
slots: How many lines the renderer has room for.
|
|
|
|
Returns:
|
|
The chosen highlights, most important first.
|
|
"""
|
|
fired = sorted(
|
|
(candidate for candidate in candidates if candidate is not None),
|
|
key=lambda c: (-c.priority, -c.magnitude, c.kind),
|
|
)
|
|
chosen: list[Highlight] = []
|
|
seen: set[str] = set()
|
|
for candidate in fired:
|
|
if candidate.family in seen:
|
|
continue
|
|
seen.add(candidate.family)
|
|
chosen.append(candidate)
|
|
if len(chosen) == slots:
|
|
break
|
|
return chosen
|
|
|
|
|
|
def ratio(value: float, baseline: float) -> float:
|
|
"""How far a value stands from its baseline, as a comparable fraction.
|
|
|
|
Used as ``magnitude`` so that "double the usual Tuesday" and "three times
|
|
the usual message length" can be ranked against each other at all. The
|
|
baseline is floored at one so a first-ever measurement does not divide by
|
|
zero and does not come out infinitely interesting.
|
|
"""
|
|
return abs(value - baseline) / max(baseline, 1)
|