diff options
| author | Jan Tuomi <jan@jantuomi.fi> | 2026-05-06 21:46:21 +0300 |
|---|---|---|
| committer | Jan Tuomi <jan@jantuomi.fi> | 2026-05-06 21:46:21 +0300 |
| commit | 713ced9c68cd70a8e56e8ce5ed27cb9bbe3e55c4 (patch) | |
| tree | 29f9997386b9f46db9f263ef63a5e01ca19db393 /src/bot.ts | |
| parent | cba8c3d49b53702a488eaff16109e29e49d94b6d (diff) | |
Add shopping list features, rework
Diffstat (limited to 'src/bot.ts')
| -rw-r--r-- | src/bot.ts | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/bot.ts b/src/bot.ts new file mode 100644 index 0000000..1de3a22 --- /dev/null +++ b/src/bot.ts @@ -0,0 +1,49 @@ +import { bot, broadcastMessage } from "./tg"; +import { getRecurringTasks, markTaskDone } from "./db"; +import { isThisWeek, today, now } from "./tasks"; +import { isMonday, getHours, format } from "date-fns"; + +const CHECK_INTERVAL_MS = 60 * 1000; +let lastRunKey: string | null = null; + +const runWeeklyTasks = async (): Promise<void> => { + try { + console.log("[weekly] Starting recurring tasks check"); + const tasks = getRecurringTasks(); + const dueTasks = tasks.filter(isThisWeek); + + if (dueTasks.length > 0) { + const taskText = dueTasks.map((t) => `• ${t.name} (${t.interval})`).join("\n"); + await broadcastMessage(`📋 Tällä viikolla tehtävät hommat:\n${taskText}`); + const todayStr = today(); + for (const t of dueTasks) { + markTaskDone(t.id, todayStr); + } + } else { + await broadcastMessage("Tällä viikolla ei toistuvia hommia! 🎉"); + } + console.log("[weekly] Done"); + } catch (err) { + console.error("[weekly] Error:", err); + } +}; + +const checkAndRun = async (): Promise<void> => { + const current = now(); + if (isMonday(current) && getHours(current) === 9) { + const key = format(current, "yyyy-MM-dd"); + if (key !== lastRunKey) { + lastRunKey = key; + await runWeeklyTasks(); + } + } +}; + +setInterval(checkAndRun, CHECK_INTERVAL_MS); +console.log("[bot] Scheduled weekly tasks for Monday 09:00"); + +bot.launch(); +console.log("[bot] Bot is running"); + +process.once("SIGINT", () => bot.stop("SIGINT")); +process.once("SIGTERM", () => bot.stop("SIGTERM")); |
