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