peppa/apps/bot/src/index.ts
2026-11-26 21:47:39 +00:00

32 lines
1.2 KiB
TypeScript

// Entry point: `peppa-bot` (launchd agent quest.telecom.peppa.bot).
import { createDb } from "@peppa/db";
import { loadEnv } from "@peppa/shared";
import { setRunnerClient } from "@peppa/runner";
import { installCrashReporting, installServiceLog } from "@peppa/core";
import { createBot } from "./bot.ts";
import { TelegramApi } from "./telegram.ts";
const env = loadEnv();
if (!env.TELEGRAM_BOT_TOKEN) {
console.error("TELEGRAM_BOT_TOKEN is not set");
process.exit(1);
}
const db = createDb(env.PEPPA_DB);
// PEP-21: the bot only enqueues runner commands; the runner service (watchdog) runs them.
setRunnerClient(true, db);
// PEP-91: a crash of the bot goes to Bugsink before launchd restarts it.
installCrashReporting(db, "bot");
// PEP-291: the log also goes to $PEPPA_HOME/logs/bot.log — it survives a redeploy.
installServiceLog("bot");
const bot = createBot({
db,
api: new TelegramApi(env.TELEGRAM_BOT_TOKEN, process.env.TELEGRAM_API_URL),
secret: env.TELEGRAM_BOT_TOKEN,
webUrl: env.PEPPA_WEB_URL,
log: (m) => console.log(`${new Date().toISOString()} ${m}`),
});
await bot.start();
for (const sig of ["SIGINT", "SIGTERM"] as const) {
process.on(sig, () => void bot.stop().then(() => process.exit(0)));
}