mirror of
https://github.com/kuhyx/signal-bot.git
synced 2026-11-22 15:52:34 +01:00
Around twenty things could be said about a day. Saying all of them is a wall nobody reads; saying whichever three are computed first is arbitrary. So each candidate declares a priority and a magnitude and the picker takes the top few, with two rules that make the result readable: one line per family, so "set a record", "had 60% of the day" and "kept the crown" do not become three sentences about the same person; and fully deterministic ordering, so a day renders identically twice. Nine candidates so far: personal records with what they beat, the group's busiest day ever, a photo record, crown streaks (naming everyone when it is shared), a silence broken, a monologue, one person dominating the day, an unusual showing for that weekday, and a night owl. Every candidate is (DayFacts) -> Highlight | None with the same signature. On day one of this release almost all of them return None, and a shape where "not yet" is the ordinary case is much easier to get right than one where every guard is bespoke. Two off-by-ones fixed by writing the sentence out and checking it: - "odezwał się po 3 dniach ciszy" was counting four. The day somebody last spoke is not silent, and neither is the day they came back on, so the gap excludes both ends. The function is now named for that. - A personal record on somebody's first ever day was being announced as a record. It is, trivially, and saying so every time is noise. Verified against the live database, read-only on a copy: the real group renders correctly at 324 messages, claims no average length for the backfilled day, and fires no highlights at all -- which is right, since there is one day of history for them to stand against. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015d48t4qsQLBJnDwDeo2Nom
194 lines
7.0 KiB
Python
194 lines
7.0 KiB
Python
# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
|
|
"""The bot object itself: the command wiring and the two loops it owns.
|
|
|
|
Split out of app.py, which had grown to the 250-line cap with the ASGI
|
|
entrypoint, the object graph and the startup sequence all in one file. This
|
|
half is "what the bot does"; startup.py is "how it comes up".
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import datetime as dt
|
|
import logging
|
|
from collections.abc import Callable
|
|
from dataclasses import dataclass
|
|
from typing import TYPE_CHECKING, Final
|
|
|
|
from signal_bot.animals import register_animal_commands
|
|
from signal_bot.announce import announce_all
|
|
from signal_bot.changelog import load as load_changelog
|
|
from signal_bot.decorators import cooldown
|
|
from signal_bot.dispatcher import Dispatcher
|
|
from signal_bot.http_client import ImageFetcher
|
|
from signal_bot.popes_command import register_pope_command
|
|
from signal_bot.portraits import PortraitCache
|
|
from signal_bot.registry import CommandRegistry
|
|
from signal_bot.stats import curiosities, periods
|
|
from signal_bot.stats.backfill import backfilled_day
|
|
from signal_bot.stats.facts import gather
|
|
from signal_bot.stats.highlights import select
|
|
from signal_bot.stats.nightly import days_to_report, mark_reported
|
|
from signal_bot.stats.queries_day import day_rows, day_totals, with_silent
|
|
from signal_bot.stats.render_day import DayView, render_day
|
|
from signal_bot.versioning import POPE, __version__
|
|
|
|
if TYPE_CHECKING:
|
|
import httpx
|
|
|
|
from signal_bot.config import Config
|
|
from signal_bot.discovery import GroupDirectory
|
|
from signal_bot.liveness import Liveness
|
|
from signal_bot.store import Store
|
|
from signal_bot.transport_base import Transport
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
Clock = Callable[[], dt.datetime]
|
|
|
|
COOLDOWN_MESSAGE = "BEEP BOOP POCZEKAJ {seconds} SEKUND."
|
|
|
|
# How many highlight lines the nightly post has room for.
|
|
HIGHLIGHT_SLOTS: Final = 3
|
|
|
|
|
|
def now_local() -> dt.datetime:
|
|
"""Current Warsaw time -- the group's day, not the host's.
|
|
|
|
One definition of "day" for the whole bot. The daily scheduler fires
|
|
against this, ``CommandContext.day`` keys the daily limits off it, and
|
|
every event is filed under it, so a host in another timezone (or a
|
|
container on UTC) cannot make those three disagree.
|
|
"""
|
|
return periods.now()
|
|
|
|
|
|
@dataclass
|
|
class Bot:
|
|
"""The wired-up bot and its two long-running loops."""
|
|
|
|
config: Config
|
|
store: Store
|
|
transport: Transport
|
|
directory: GroupDirectory
|
|
dispatcher: Dispatcher
|
|
liveness: Liveness
|
|
# The same clock the dispatcher files events against. One clock, so
|
|
# "which day is it" cannot differ between writing an event and reporting
|
|
# on it -- which is precisely the drift the old reset-after-post had.
|
|
clock: Clock = now_local
|
|
|
|
async def publish_report(self) -> None:
|
|
"""Post each group's report for the last full day.
|
|
|
|
Yesterday, not "since the last post". The old scheme reset a counter
|
|
after publishing, so a group nobody spoke in was never reset and its
|
|
next report silently covered two days. A day is now a property of
|
|
when a message was sent, which cannot drift.
|
|
|
|
Nothing is deleted afterwards. Each group is isolated on purpose: one
|
|
group refusing the report -- the bot removed from it, a transient send
|
|
failure -- must not cost every other group its report.
|
|
"""
|
|
latest = periods.previous_day(self.clock())
|
|
for group_id in self.directory.groups:
|
|
for day in days_to_report(self.store, group_id, latest):
|
|
if not await self._report_one(group_id, day):
|
|
# Left unmarked on purpose, so the next run retries it
|
|
# rather than skipping the day forever.
|
|
break
|
|
mark_reported(self.store, group_id, day)
|
|
|
|
async def _report_one(self, group_id: str, day: str) -> bool:
|
|
"""Post one group's report for one day. False means try again later."""
|
|
rows = day_rows(self.store.events, group_id, day)
|
|
if not rows:
|
|
# Settled, not pending. The bot is in every group it was ever
|
|
# added to, so posting "nobody spoke" would mean a nightly ping
|
|
# into every quiet group, forever.
|
|
logger.info("no messages in %s on %s; not posting a report", group_id, day)
|
|
return True
|
|
named = self.store.events.roster(group_id, self.directory.member_identities(group_id))
|
|
with_zeros = with_silent(rows, [(p.uuid, p.display_name) for p in named])
|
|
backfilled = backfilled_day(self.store, group_id)
|
|
facts = gather(self.store.events, group_id, day, with_zeros, exclude=backfilled)
|
|
picks = select(curiosities.for_day(facts), HIGHLIGHT_SLOTS)
|
|
text = render_day(
|
|
day,
|
|
with_zeros,
|
|
day_totals(with_zeros),
|
|
DayView(
|
|
since=self.store.events.first_day(group_id),
|
|
approximate=backfilled == day,
|
|
highlights=tuple(pick.text for pick in picks),
|
|
),
|
|
)
|
|
try:
|
|
await self.transport.send_text(text, group_id)
|
|
except Exception:
|
|
logger.exception("daily report failed for group %s", group_id)
|
|
return False
|
|
return True
|
|
|
|
async def announce_release(self) -> None:
|
|
"""Tell every watched group about a version it has not seen yet."""
|
|
await announce_all(
|
|
self.transport,
|
|
self.store,
|
|
list(self.directory.groups),
|
|
load_changelog(),
|
|
__version__,
|
|
POPE.name,
|
|
)
|
|
|
|
async def listen(self) -> None:
|
|
"""Consume frames forever, handing each to the dispatcher."""
|
|
async for frame in self.transport.receive():
|
|
# Every frame counts, including receipts and typing indicators:
|
|
# the question this answers is "can anything still reach us?",
|
|
# not "did anybody address the bot?".
|
|
self.liveness.note_envelope()
|
|
await self.dispatcher.handle(frame)
|
|
|
|
|
|
def build_bot(
|
|
config: Config,
|
|
client: httpx.AsyncClient,
|
|
store: Store,
|
|
transport: Transport,
|
|
directory: GroupDirectory,
|
|
liveness: Liveness,
|
|
clock: Clock = now_local,
|
|
) -> Bot:
|
|
"""Assemble the object graph from its injected dependencies."""
|
|
registry = CommandRegistry()
|
|
fetcher = ImageFetcher(client)
|
|
rate_limit = cooldown(config.cooldown_seconds, COOLDOWN_MESSAGE)
|
|
register_animal_commands(
|
|
registry,
|
|
fetcher,
|
|
decorate=rate_limit,
|
|
endpoints=config.image_api_urls,
|
|
)
|
|
register_pope_command(
|
|
registry,
|
|
PortraitCache(fetcher, config.commons_api_url),
|
|
decorate=rate_limit,
|
|
)
|
|
dispatcher = Dispatcher(
|
|
config=config,
|
|
directory=directory,
|
|
registry=registry,
|
|
store=store,
|
|
transport=transport,
|
|
clock=clock,
|
|
)
|
|
return Bot(
|
|
config=config,
|
|
store=store,
|
|
transport=transport,
|
|
directory=directory,
|
|
dispatcher=dispatcher,
|
|
liveness=liveness,
|
|
clock=clock,
|
|
)
|