2022-04-02 02:40:33 -04:00
|
|
|
const NotificationProvider = require("./notification-provider");
|
|
|
|
const axios = require("axios");
|
|
|
|
const { DOWN, UP } = require("../../src/util");
|
|
|
|
|
2022-04-14 02:29:54 -04:00
|
|
|
class PushDeer extends NotificationProvider {
|
2022-04-02 02:40:33 -04:00
|
|
|
|
2022-04-14 02:29:54 -04:00
|
|
|
name = "PushDeer";
|
2022-04-02 02:40:33 -04:00
|
|
|
|
2023-08-11 03:46:41 -04:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2022-04-02 02:40:33 -04:00
|
|
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
|
|
|
let okMsg = "Sent Successfully.";
|
2023-07-20 05:06:37 -04:00
|
|
|
let endpoint = "/message/push";
|
2023-07-21 10:01:20 -04:00
|
|
|
let serverUrl = notification.pushdeerServer || "https://api2.pushdeer.com";
|
2023-07-21 10:11:13 -04:00
|
|
|
let pushdeerlink = `${serverUrl.trim().replace(/\/*$/, "")}${endpoint}`;
|
2022-04-02 02:40:33 -04:00
|
|
|
|
|
|
|
let valid = msg != null && monitorJSON != null && heartbeatJSON != null;
|
|
|
|
|
|
|
|
let title;
|
2022-04-17 03:43:03 -04:00
|
|
|
if (valid && heartbeatJSON.status === UP) {
|
2022-04-05 20:38:48 -04:00
|
|
|
title = "## Uptime Kuma: " + monitorJSON.name + " up";
|
2022-04-17 03:43:03 -04:00
|
|
|
} else if (valid && heartbeatJSON.status === DOWN) {
|
2022-04-05 20:38:48 -04:00
|
|
|
title = "## Uptime Kuma: " + monitorJSON.name + " down";
|
2022-04-02 02:40:33 -04:00
|
|
|
} else {
|
2022-04-05 20:38:48 -04:00
|
|
|
title = "## Uptime Kuma Message";
|
2022-04-02 02:40:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
let data = {
|
|
|
|
"pushkey": notification.pushdeerKey,
|
|
|
|
"text": title,
|
|
|
|
"desp": msg.replace(/\n/g, "\n\n"),
|
|
|
|
"type": "markdown",
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
let res = await axios.post(pushdeerlink, data);
|
|
|
|
|
|
|
|
if ("error" in res.data) {
|
|
|
|
let error = res.data.error;
|
|
|
|
this.throwGeneralAxiosError(error);
|
|
|
|
}
|
|
|
|
if (res.data.content.result.length === 0) {
|
2022-04-14 02:29:54 -04:00
|
|
|
let error = "Invalid PushDeer key";
|
2022-04-02 02:40:33 -04:00
|
|
|
this.throwGeneralAxiosError(error);
|
2022-04-17 03:43:03 -04:00
|
|
|
} else if (JSON.parse(res.data.content.result[0]).success !== "ok") {
|
2022-04-02 02:40:33 -04:00
|
|
|
let error = "Unknown error";
|
|
|
|
this.throwGeneralAxiosError(error);
|
|
|
|
}
|
|
|
|
return okMsg;
|
|
|
|
} catch (error) {
|
|
|
|
this.throwGeneralAxiosError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 02:29:54 -04:00
|
|
|
module.exports = PushDeer;
|