From 713ced9c68cd70a8e56e8ce5ed27cb9bbe3e55c4 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Wed, 6 May 2026 21:46:21 +0300 Subject: Add shopping list features, rework --- src/bot.ts | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/bot.ts (limited to 'src/bot.ts') 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 => { + 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 => { + 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")); -- cgit v1.3