docker-compose.yml: - Every env var documented inline with type/default/purpose - env_file: .env (required: false) — container starts without it - NTFY_TOPIC uses :? operator so compose errors clearly if unset - New vars: NTFY_TOKEN (private topics), NTFY_PRIORITY, TZ, APP_PORT, DEV_PORT - Removed duplicate NTFY_URL entry notifier/index.mjs: - Reads TZ from env (was hardcoded "America/Chicago") - Reads NTFY_PRIORITY from env (was hardcoded "default") - Adds Authorization: Bearer header when NTFY_TOKEN is set - Startup log shows auth status and full schedule config .env.example: mirrors every compose variable with descriptions Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
72 lines
2.6 KiB
JavaScript
72 lines
2.6 KiB
JavaScript
import cron from "node-cron";
|
|
|
|
const NTFY_URL = process.env.NTFY_URL || "https://ntfy.sh";
|
|
const NTFY_TOPIC = process.env.NTFY_TOPIC;
|
|
const NTFY_PRIORITY = process.env.NTFY_PRIORITY || "default";
|
|
const NTFY_TOKEN = process.env.NTFY_TOKEN; // Bearer token for private topics
|
|
const CRON_SCHEDULE = process.env.CRON_SCHEDULE || "0 9 1 * *";
|
|
const TZ = process.env.TZ || "America/Chicago";
|
|
const APP_URL = process.env.APP_URL || "http://localhost:8080";
|
|
|
|
if (!NTFY_TOPIC) {
|
|
console.error("NTFY_TOPIC is required. Set it in your environment or docker-compose.");
|
|
process.exit(1);
|
|
}
|
|
|
|
const MONTH_NAMES = [
|
|
"January", "February", "March", "April", "May", "June",
|
|
"July", "August", "September", "October", "November", "December",
|
|
];
|
|
|
|
async function sendReminder() {
|
|
const now = new Date();
|
|
// We fire on the 1st — remind for the month that just ended
|
|
const prevMonth = new Date(now.getFullYear(), now.getMonth() - 1, 1);
|
|
const monthName = MONTH_NAMES[prevMonth.getMonth()];
|
|
const year = prevMonth.getFullYear();
|
|
|
|
const title = `Time to write your ${monthName} letter`;
|
|
const message = `${monthName} ${year} is done. Take 10 minutes to capture the month before it fades. Open your letter app to answer this month's 6 prompts.`;
|
|
|
|
try {
|
|
const headers = {
|
|
Title: title,
|
|
Priority: NTFY_PRIORITY,
|
|
Tags: "pencil,calendar",
|
|
Click: APP_URL,
|
|
"Content-Type": "text/plain",
|
|
};
|
|
if (NTFY_TOKEN) headers["Authorization"] = `Bearer ${NTFY_TOKEN}`;
|
|
|
|
const res = await fetch(`${NTFY_URL}/${NTFY_TOPIC}`, {
|
|
method: "POST",
|
|
headers,
|
|
body: message,
|
|
});
|
|
|
|
if (res.ok) {
|
|
console.log(`[${new Date().toISOString()}] Reminder sent for ${monthName} ${year}`);
|
|
} else {
|
|
console.error(`[${new Date().toISOString()}] ntfy returned ${res.status}: ${await res.text()}`);
|
|
}
|
|
} catch (err) {
|
|
console.error(`[${new Date().toISOString()}] Failed to send reminder:`, err.message);
|
|
}
|
|
}
|
|
|
|
console.log(`[${new Date().toISOString()}] Notifier started`);
|
|
console.log(` Topic: ${NTFY_URL}/${NTFY_TOPIC}`);
|
|
console.log(` Priority: ${NTFY_PRIORITY}`);
|
|
console.log(` Auth: ${NTFY_TOKEN ? "Bearer token set" : "none (public topic)"}`);
|
|
console.log(` Schedule: ${CRON_SCHEDULE} (${TZ})`);
|
|
console.log(` App URL: ${APP_URL}`);
|
|
|
|
// Run on schedule
|
|
cron.schedule(CRON_SCHEDULE, sendReminder, { timezone: TZ });
|
|
|
|
// Send a startup test ping in dev mode
|
|
if (process.env.NTFY_TEST_ON_START === "true") {
|
|
console.log("Sending test notification on startup...");
|
|
sendReminder();
|
|
}
|