49 lines
1.4 KiB
TypeScript
49 lines
1.4 KiB
TypeScript
import { createQS } from "./util";
|
|
|
|
type TSendNotificationOptions = {
|
|
ntfy: {
|
|
link?: string;
|
|
} | null,
|
|
discord: {
|
|
href?: string
|
|
avatar_url: string,
|
|
botname: string
|
|
}
|
|
}
|
|
|
|
export async function sendNotification( title: string, body: string, options: TSendNotificationOptions ) {
|
|
console.dir({
|
|
sendNotification: {
|
|
title,
|
|
body
|
|
}
|
|
});
|
|
const QS = {
|
|
ntfy: options.ntfy ? createQS(options.ntfy) : null,
|
|
discord: createQS(options.discord)
|
|
}
|
|
if ( ! ( process.env.notification_mock == "true" ) ) {
|
|
const response = await fetch(`${ process.env.apprise_https == "true" ? "https" : "http"}://${process.env.apprise_host ? process.env.apprise_host : "apprise"}:${process.env.apprise_port ? String(process.env.apprise_port) : "80" }/notify`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json"
|
|
},
|
|
body: JSON.stringify({
|
|
urls: [
|
|
`ntfys://${process.env.ntfy_username}:${process.env.ntfy_password}@${process.env.ntfy_host}/${process.env.ntfy_topic}${ QS.ntfy ? "?" + QS.ntfy : ""}`,
|
|
`discord://${process.env.dc_webhook}?${QS.discord}`
|
|
].join(","),
|
|
title: title,
|
|
body: body,
|
|
format: "markdown"
|
|
})
|
|
});
|
|
const responseBody = await response.json();
|
|
return responseBody;
|
|
} else {
|
|
console.dir({
|
|
sendNotification: "mocking"
|
|
})
|
|
}
|
|
|
|
} |