add apprise support

This commit is contained in:
LouisLam
2021-07-18 18:51:58 +08:00
parent 13c9244e3f
commit 66037e236c
6 changed files with 133 additions and 74 deletions

View File

@@ -2,9 +2,16 @@ const axios = require("axios");
const {R} = require("redbean-node");
const FormData = require('form-data');
const nodemailer = require("nodemailer");
const child_process = require("child_process");
class Notification {
static async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
let res = {
ok: true,
msg: "Sent Successfully"
}
if (notification.type === "telegram") {
try {
await axios.get(`https://api.telegram.org/bot${notification.telegramBotToken}/sendMessage`, {
@@ -185,7 +192,7 @@ class Notification {
var pushoverlink = 'https://api.pushover.net/1/messages.json'
try {
if (heartbeatJSON == null) {
let data = {'message': "<b>Uptime Kuma Pushover testing successful.</b>",
let data = {'message': "<b>Uptime Kuma Pushover testing successful.</b>",
'user': notification.pushoveruserkey, 'token': notification.pushoverapptoken, 'sound':notification.pushoversounds,
'priority': notification.pushoverpriority, 'title':notification.pushovertitle, 'retry': "30", 'expire':"3600", 'html': 1}
let res = await axios.post(pushoverlink, data)
@@ -210,6 +217,10 @@ class Notification {
return false;
}
} else if (notification.type === "apprise") {
return Notification.apprise(notification, msg)
} else {
throw new Error("Notification type is not supported")
}
@@ -274,16 +285,26 @@ class Notification {
return true;
}
static async discord(notification, msg) {
const client = new Discord.Client();
await client.login(notification.discordToken)
static async apprise(notification, msg) {
let s = child_process.spawnSync("apprise", [ "-vv", "-b", msg, notification.appriseURL])
let output = s.stdout.toString();
const channel = await client.channels.fetch(notification.discordChannelID);
await channel.send(msg);
console.log(output)
client.destroy()
if (output) {
return {
ok: ! output.includes("ERROR"),
msg: output
}
} else {
return { }
}
}
return true;
static checkApprise() {
let commandExistsSync = require('command-exists').sync;
let exists = commandExistsSync('apprise');
return exists;
}
}

View File

@@ -35,12 +35,15 @@ let needSetup = false;
(async () => {
await initDatabase();
console.log("Adding route")
app.use('/', express.static("dist"));
app.get('*', function(request, response, next) {
response.sendFile(process.cwd() + '/dist/index.html');
});
console.log("Adding socket handler")
io.on('connection', async (socket) => {
socket.emit("info", {
@@ -437,12 +440,9 @@ let needSetup = false;
try {
checkLogin(socket)
await Notification.send(notification, notification.name + " Testing")
let res = await Notification.send(notification, notification.name + " Testing")
callback({
ok: true,
msg: "Sent Successfully"
});
callback(res);
} catch (e) {
callback({
@@ -451,11 +451,20 @@ let needSetup = false;
});
}
});
socket.on("checkApprise", async (callback) => {
try {
checkLogin(socket)
callback(Notification.checkApprise());
} catch (e) {
callback(false);
}
});
});
console.log("Init")
server.listen(port, hostname, () => {
console.log(`Listening on ${hostname}:${port}`);
startMonitors();
});
@@ -551,10 +560,11 @@ async function initDatabase() {
}
console.log("Connecting to Database")
R.setup('sqlite', {
filename: path
});
console.log("Connected")
R.freeze(true)
await R.autoloadModels("./server/model");
@@ -569,6 +579,7 @@ async function initDatabase() {
jwtSecretBean.value = passwordHash.generate(dayjs() + "")
await R.store(jwtSecretBean)
console.log("Stored JWT secret into database")
} else {
console.log("Load JWT secret from database.")
}