88 lines
1.7 KiB
TypeScript
88 lines
1.7 KiB
TypeScript
import { $ } from "bun"
|
|
import cron from "node-cron"
|
|
import dayjs from "dayjs"
|
|
import "dayjs/locale/es"
|
|
import { getUSD, getUsage, getWeather } from "./fx"
|
|
import { EventSource } from "eventsource"
|
|
|
|
const TOPIC = "daily-digest"
|
|
const TOKEN = process.env.NTFY_TOKEN
|
|
|
|
if (!TOKEN) {
|
|
console.error("ntfy token not set")
|
|
process.exit(1)
|
|
}
|
|
|
|
|
|
|
|
async function main() {
|
|
const usd = await getUSD()
|
|
const usage = await getUsage()
|
|
const weather = await getWeather()
|
|
|
|
const message = `Good Morning, SilicoFlare!
|
|
|
|
Today is **${dayjs().format("dddd, DD MMMM YYYY")}**.
|
|
Hoy es **${dayjs().locale("es").format("dddd, D [de] MMMM [de] YYYY")}**.
|
|
|
|
${weather}
|
|
|
|
${usd}
|
|
|
|
${usage}
|
|
|
|
Have a great day!`
|
|
|
|
const ntfy = await fetch(
|
|
`https://ntfy.hy-pr.link/${TOPIC}`,
|
|
{
|
|
method: "POST",
|
|
headers: {
|
|
Authorization: `Bearer ${TOKEN}`,
|
|
Title: `Daily Digest for ${dayjs().format("YYYY-MM-DD")}`,
|
|
Markdown: "yes",
|
|
Tags: "calendar"
|
|
},
|
|
body: message,
|
|
},
|
|
)
|
|
|
|
if (!ntfy.ok) {
|
|
throw new Error(`ntfy failed: ${ntfy.status}`)
|
|
}
|
|
|
|
console.log("Notification sent at " + dayjs().format("YYYY-MM-dd HH:mm:ss"))
|
|
}
|
|
|
|
cron.schedule("0 7 * * *", () => {
|
|
main().catch((err) => {
|
|
console.error(err)
|
|
})
|
|
})
|
|
|
|
|
|
const es = new EventSource("https://ntfy.hy-pr.link/daily-digest/sse", {
|
|
fetch: (input, init) =>
|
|
fetch(input, {
|
|
...init,
|
|
headers: {
|
|
...init?.headers,
|
|
Authorization: `Bearer ${TOKEN}`,
|
|
},
|
|
}),
|
|
});
|
|
|
|
es.onmessage = (e) => {
|
|
const msg = JSON.parse(e.data).message as string
|
|
|
|
if (msg === "/digest") {
|
|
main().catch((err) => {
|
|
console.error(err)
|
|
})
|
|
}
|
|
};
|
|
|
|
es.onerror = (e) => {
|
|
console.error(e);
|
|
};
|