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 cron from "node-cron"
|
||||||
import dayjs from "dayjs"
|
import dayjs from "dayjs"
|
||||||
import "dayjs/locale/es"
|
import "dayjs/locale/es"
|
||||||
import { getUSD, getUsage, getWeather } from "./fx"
|
import { getUSD, getUsage, getWeather } from "./fx"
|
||||||
import { EventSource } from "eventsource"
|
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
|
const TOKEN = process.env.NTFY_TOKEN
|
||||||
|
|
||||||
if (!TOKEN) {
|
if (!TOKEN || !NTFY_URL || !TOPIC) {
|
||||||
console.error("ntfy token not set")
|
console.error("ntfy token, url or topic not set")
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
const usd = await getUSD()
|
const [usd, usage, weather] = await Promise.all([
|
||||||
const usage = await getUsage()
|
getUSD(),
|
||||||
const weather = await getWeather()
|
getUsage(),
|
||||||
|
getWeather()
|
||||||
|
])
|
||||||
|
|
||||||
const message = `Good Morning, SilicoFlare!
|
const message = `Good Morning, SilicoFlare!
|
||||||
|
|
||||||
@@ -34,7 +36,7 @@ ${usage}
|
|||||||
Have a great day!`
|
Have a great day!`
|
||||||
|
|
||||||
const ntfy = await fetch(
|
const ntfy = await fetch(
|
||||||
`https://ntfy.hy-pr.link/${TOPIC}`,
|
`${NTFY_URL}/${TOPIC}`,
|
||||||
{
|
{
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
@@ -60,7 +62,7 @@ async function runMain() {
|
|||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err)
|
console.error(err)
|
||||||
fetch(
|
fetch(
|
||||||
`https://ntfy.hy-pr.link/${TOPIC}`,
|
`${NTFY_URL}/${TOPIC}`,
|
||||||
{
|
{
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
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) =>
|
||||||
fetch(input, {
|
fetch(input, {
|
||||||
...init,
|
...init,
|
||||||
|
|||||||
Reference in New Issue
Block a user