51 lines
1.2 KiB
JavaScript
51 lines
1.2 KiB
JavaScript
|
const logger = require("./logger.js");
|
||
|
|
||
|
async function send(title, message, priority, ...tags) {
|
||
|
if (title?.length === 0 || message?.length === 0) {
|
||
|
return;
|
||
|
}
|
||
|
const url = global.config.notify.url;
|
||
|
if (url?.length === 0) {
|
||
|
return;
|
||
|
}
|
||
|
const timestamp = Date.now();
|
||
|
logger.debug("sending notification to '" + url + "'...");
|
||
|
const headers = new Headers();
|
||
|
headers.set("Authorization", "Basic " +
|
||
|
Buffer.from(
|
||
|
global.config.notify.user + ":" + global.config.notify.password
|
||
|
).toString("base64"));
|
||
|
headers.set("Title", title);
|
||
|
if (priority) {
|
||
|
headers.set("Priority", priority);
|
||
|
}
|
||
|
if (tags) {
|
||
|
headers.set("Tags", tags);
|
||
|
}
|
||
|
try {
|
||
|
const result = await fetch(url, {
|
||
|
method: "POST",
|
||
|
headers: headers,
|
||
|
body: message,
|
||
|
});
|
||
|
if (result.status !== 200) {
|
||
|
logger.warn(
|
||
|
"notification could not be sent (status code: " + result.status + ")"
|
||
|
);
|
||
|
return;
|
||
|
}
|
||
|
logger.debug(
|
||
|
"successfully sent notification after " +
|
||
|
(Date.now() - timestamp) / 1000 +
|
||
|
" seconds!"
|
||
|
);
|
||
|
} catch (err) {
|
||
|
logger.error("encountered an error sending notification (" + err + ")");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// exports
|
||
|
module.exports = {
|
||
|
send,
|
||
|
};
|