blob: 1de3a22a31f1195e0fb1a2e858cf33209f1e19a5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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"));
|