-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck.js
More file actions
executable file
·182 lines (157 loc) · 5.43 KB
/
check.js
File metadata and controls
executable file
·182 lines (157 loc) · 5.43 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env node
// check.js — Cron script. Runs every minute.
//
// Delivery chain for due reminders:
// 1. Webhook → POST to MCP server's HTTP endpoint (works if Claude session is running)
// 2. Bus → POST to HQ Bus broker (optional, for multi-session setups)
// 3. Telegram → Direct API fallback (when Claude doesn't respond in time)
//
// Recurrence: when a recurring reminder fires, the next occurrence is auto-spawned.
//
// Usage: node check.js
// Cron: * * * * * cd /path/to/claude-code-reminders && node check.js >> check.log 2>&1
import { load, addReminder, markDelivered, markTelegramSent } from "./lib/store.js";
import { nextOccurrence } from "./lib/recurrence.js";
import { config } from "dotenv";
import { dirname } from "path";
import { fileURLToPath } from "url";
const __dirname = dirname(fileURLToPath(import.meta.url));
config({ path: `${__dirname}/.env` });
const DATA_DIR = process.env.REMINDERS_DIR || __dirname;
const WEBHOOK_PORT = process.env.WEBHOOK_PORT || "8787";
const WEBHOOK_URL = `http://127.0.0.1:${WEBHOOK_PORT}`;
const BUS_BROKER_URL = process.env.BUS_BROKER_URL || "";
const BUS_TARGET_SESSION = process.env.BUS_TARGET_SESSION || "";
const TELEGRAM_BOT_TOKEN = process.env.TELEGRAM_BOT_TOKEN;
const TELEGRAM_CHAT_ID = process.env.TELEGRAM_CHAT_ID;
const ACK_TIMEOUT = parseInt(process.env.ACK_TIMEOUT_MINUTES || "2") * 60 * 1000;
const now = Date.now();
// --- Phase 1: Send due pending reminders ---
const reminders = load(DATA_DIR);
const due = reminders.filter((r) => {
if (r.status !== "pending") return false;
if (r.paused) return false;
const t = new Date(r.datetime).getTime();
return t > 0 && t <= now;
});
for (const r of due) {
let delivered = false;
// Try 1: Webhook to MCP server (Claude session running locally)
delivered = await tryWebhook(r);
// Try 2: HQ Bus (optional, for multi-session setups)
if (!delivered && BUS_BROKER_URL) {
delivered = await tryBus(r);
}
if (delivered) {
markDelivered(DATA_DIR, r.id);
log(`Delivered [${r.id}]: ${r.message}`);
} else {
// No Claude session reachable — send directly to Telegram
await sendTelegram(r, "No active Claude session");
}
// Spawn next occurrence for recurring reminders
if (r.recurrence) {
spawnNextOccurrence(r);
}
}
// --- Phase 2: Escalate unacknowledged reminders to Telegram ---
const stale = load(DATA_DIR).filter((r) => {
if (r.status !== "delivered" || !r.deliveredAt) return false;
const elapsed = now - new Date(r.deliveredAt).getTime();
return elapsed > ACK_TIMEOUT;
});
for (const r of stale) {
await sendTelegram(r, "Claude didn't respond in time");
}
// --- Delivery methods ---
async function tryWebhook(reminder) {
try {
const resp = await fetch(WEBHOOK_URL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
id: reminder.id,
message: reminder.message,
recurrence: reminder.recurrence || null,
}),
signal: AbortSignal.timeout(3000),
});
return resp.ok;
} catch {
return false;
}
}
async function tryBus(reminder) {
const recurTag = reminder.recurrence ? ` [${reminder.recurrence}]` : "";
const content = `⏰ Reminder [${reminder.id}]: ${reminder.message}${recurTag}\n\nTo acknowledge, call: acknowledge_reminder({ id: "${reminder.id}" })`;
try {
const resp = await fetch(`${BUS_BROKER_URL}/send`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
to: BUS_TARGET_SESSION,
from: "reminders",
content,
}),
signal: AbortSignal.timeout(3000),
});
return resp.ok;
} catch {
return false;
}
}
async function sendTelegram(reminder, reason) {
if (!TELEGRAM_BOT_TOKEN) {
log(`No TELEGRAM_BOT_TOKEN — cannot send fallback for [${reminder.id}]`);
return;
}
const chatId = extractChatId(reminder.target) || TELEGRAM_CHAT_ID;
if (!chatId) {
log(`No chat ID for [${reminder.id}]`);
return;
}
const text = `⏰ Reminder: ${reminder.message}\n\n📡 ${reason} — sending directly.`;
try {
const resp = await fetch(
`https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ chat_id: chatId, text }),
signal: AbortSignal.timeout(5000),
}
);
const data = await resp.json();
if (data.ok) {
markTelegramSent(DATA_DIR, reminder.id);
log(`Telegram sent [${reminder.id}]: ${reminder.message} (${reason})`);
} else {
log(`Telegram failed [${reminder.id}]: ${JSON.stringify(data)}`);
}
} catch (err) {
log(`Telegram error [${reminder.id}]: ${err.message}`);
}
}
// --- Helpers ---
function extractChatId(target) {
if (!target) return null;
const m = target.match(/^telegram:(.+)$/);
return m ? m[1] : null;
}
function spawnNextOccurrence(reminder) {
try {
const nextDt = nextOccurrence(reminder.recurrence, reminder.datetime);
const next = addReminder(DATA_DIR, {
datetime: nextDt,
message: reminder.message,
target: reminder.target,
recurrence: reminder.recurrence,
});
log(`Spawned next [${next.id}] for ${nextDt}: ${reminder.message} (${reminder.recurrence})`);
} catch (err) {
log(`Failed to spawn next for [${reminder.id}]: ${err.message}`);
}
}
function log(msg) {
console.error(`[${new Date().toISOString()}] ${msg}`);
}