fix: improve configuration handling for ntfy notifications
This commit is contained in:
@@ -1,24 +1,26 @@
|
||||
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 NTFY_URL = process.env.NTFY_URL
|
||||
const TOPIC = process.env.NTFY_TOPIC
|
||||
const TOKEN = process.env.NTFY_TOKEN
|
||||
|
||||
if (!TOKEN) {
|
||||
console.error("ntfy token not set")
|
||||
if (!TOKEN || !NTFY_URL || !TOPIC) {
|
||||
console.error("ntfy token, url or topic not set")
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
|
||||
|
||||
async function main() {
|
||||
const usd = await getUSD()
|
||||
const usage = await getUsage()
|
||||
const weather = await getWeather()
|
||||
const [usd, usage, weather] = await Promise.all([
|
||||
getUSD(),
|
||||
getUsage(),
|
||||
getWeather()
|
||||
])
|
||||
|
||||
const message = `Good Morning, SilicoFlare!
|
||||
|
||||
@@ -34,7 +36,7 @@ ${usage}
|
||||
Have a great day!`
|
||||
|
||||
const ntfy = await fetch(
|
||||
`https://ntfy.hy-pr.link/${TOPIC}`,
|
||||
`${NTFY_URL}/${TOPIC}`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
@@ -60,7 +62,7 @@ async function runMain() {
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
fetch(
|
||||
`https://ntfy.hy-pr.link/${TOPIC}`,
|
||||
`${NTFY_URL}/${TOPIC}`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
@@ -83,7 +85,7 @@ cron.schedule("0 7 * * *", () => {
|
||||
})
|
||||
|
||||
|
||||
const es = new EventSource("https://ntfy.hy-pr.link/daily-digest/sse", {
|
||||
const es = new EventSource(`${NTFY_URL}/${TOPIC}/sse`, {
|
||||
fetch: (input, init) =>
|
||||
fetch(input, {
|
||||
...init,
|
||||
|
||||
Reference in New Issue
Block a user