Files
loki/scripts/test/notify-categories.ts
CatoandClaude Opus 5.5 b912cb4d91 feat(notify): every person chooses what reaches their own Telegram (#1543)
* feat(notify): every person chooses what reaches their own Telegram

Loki is multi-tenant, but Telegram was one hard-wired chat (the operator's)
on a bot whose incoming messages belong to OpenClaw. Now each account sets
it up in Settings → Notifications → Telegram:

- Connect Telegram opens Loki's notification bot with a one-time code;
  pressing Start links that chat (POST /api/telegram/webhook, verified by
  Telegram's secret-token header). No chat ids, nothing to type. /stop or
  blocking the bot unlinks. The webhook registers itself on first use.
- Six kinds of message (config/notify-categories.ts), each on/off per
  person; "servers" is shown to the operator only.
- Every app sender now goes through the gateway as that person
  (lib/notify/person.ts → deliverNotice): approvals, feedback, fixes, runs,
  studio, build requests, sign-ups, autopilot, site live, gateway down. The
  gateway applies the person's choices, sends to their own linked chat, and
  falls back to the operator's old chat for the operator only.
  scripts/test/one-telegram-door.ts fails the build on a direct send.
- Buttons Loki composes from its own records (a customer's own site) are
  trusted; links from outside still meet the host allowlist.
- Migration 0132 adds the link and choices to notification_preferences.
- New env: TELEGRAM_NOTIFY_BOT_TOKEN, TELEGRAM_NOTIFY_BOT_USERNAME (checked as
  a pair at boot). Until set, Settings says Telegram is not set up and the
  operator's existing chat keeps working.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtzUbLhg3oqt4X1NaN

* fix(openclaw): the heartbeat follows the same one-tap contract

The box audit (openclaw-day-plan run 3) found the sender of "Gateway was
not running. Restarted automatically. All good now.": the OpenClaw
workspace's tools/health-guardian.sh, not in any repo, whose status lines the
agent relays. The contract now goes on HEARTBEAT.md too (between markers,
backed up, idempotent), and the installer prints how health-guardian is
started and its speaking lines, numbers and secrets redacted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtzUbLhg3oqt4X1NaN

* style(drizzle): format the generated 0132 snapshot and journal

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtzUbLhg3oqt4X1NaN

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-02 09:05:10 +01:00

62 lines
1.9 KiB
TypeScript

/**
* Each person chooses which kinds of message reach their Telegram
* (config/notify-categories.ts); the gateway files every notice under a kind
* (lib/notify/policy.ts categoryOf).
*
* Run: npx tsx scripts/test/notify-categories.ts
*/
import {
categoriesFor,
isCategoryOn,
NOTIFY_CATEGORIES,
NOTIFY_CATEGORY_IDS,
} from "@/config/notify-categories";
import { categoryOf } from "@/lib/notify/policy";
import { startLink } from "@/lib/telegram/notify-bot";
let failures = 0;
function check(name: string, cond: boolean) {
if (cond) console.log(` ✓ ${name}`);
else {
failures += 1;
console.log(` ✗ ${name}`);
}
}
console.log("notify-categories:");
check(
"a person with no choices gets every kind's default",
NOTIFY_CATEGORY_IDS.every((c) => isCategoryOn({}, c) === NOTIFY_CATEGORIES[c].defaultOn),
);
check("a kind turned off stays off", !isCategoryOn({ people: false }, "people"));
check("a kind turned on stays on", isCategoryOn({ plans: true }, "plans"));
check("a customer is not offered the server kind", !categoriesFor(false).includes("servers"));
check("the operator is", categoriesFor(true).includes("servers"));
check(
"every kind has words a person can read",
NOTIFY_CATEGORY_IDS.every(
(c) => NOTIFY_CATEGORIES[c].label.length > 3 && NOTIFY_CATEGORIES[c].note.length > 10,
),
);
check("the box files under servers", categoryOf({ source: "box" }) === "servers");
check(
"OrangeCat's bots file under other assistants",
categoryOf({ source: "orangecat-cat" }) === "bots",
);
check(
"an explicit kind wins",
categoryOf({ source: "box", category: "decisions" }) === "decisions",
);
process.env.TELEGRAM_NOTIFY_BOT_USERNAME = "@LokiNoticesBot";
check(
"the connect link opens the bot with the code",
startLink("abc_123") === "https://t.me/LokiNoticesBot?start=abc_123",
);
if (failures) {
console.log(`\n ${failures} failure(s)`);
process.exit(1);
}