1
0
mirror of https://github.com/louislam/uptime-kuma.git synced 2024-12-24 22:14:47 +02:00
uptime-kuma/server/notification-providers/heii-oncall.js

74 lines
2.4 KiB
JavaScript
Raw Normal View History

2024-02-13 01:58:54 +02:00
const { UP, DOWN, getMonitorRelativeURL } = require("../../src/util");
const { setting } = require("../util-server");
const NotificationProvider = require("./notification-provider");
const axios = require("axios");
2024-02-13 02:14:41 +02:00
const heiiOnCallBaseUrl = "https://heiioncall.com";
2024-02-13 01:58:54 +02:00
class HeiiOnCall extends NotificationProvider {
name = "HeiiOnCall";
/**
* @inheritdoc
*/
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
// Payload to Heii On-Call is the entire heartbat JSON
const payload = heartbeatJSON ? heartbeatJSON : {};
2024-02-13 02:14:41 +02:00
// If we can, add url back to mintor to payload
2024-02-13 01:58:54 +02:00
const baseURL = await setting("primaryBaseURL");
if (baseURL && monitorJSON) {
payload["url"] = baseURL + getMonitorRelativeURL(monitorJSON.id);
}
if (!heartbeatJSON) {
// No heartbeatJSON. Could be test button, but not necessarily. Just pull msg into payload.
payload["msg"] = msg;
return this.postNotification(notification, "alert", payload);
}
2024-02-13 01:58:54 +02:00
if (heartbeatJSON.status === DOWN) {
// Monitor is DOWN, alert on Heii On-Call
return this.postNotification(notification, "alert", payload);
}
if (heartbeatJSON.status === UP) {
// Monitor is UP, resolve on Heii On-Call
return this.postNotification(notification, "resolve", payload);
}
}
/**
* Post to Heii On-Call
* @param {BeanModel} notification Message title
2024-02-13 02:14:41 +02:00
* @param {string} action Trigger action (alert, resovle)
2024-02-13 01:58:54 +02:00
* @param {object} payload Data for Heii On-Call
* @returns {Promise<string>} Success message
*/
async postNotification(notification, action, payload) {
const config = {
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: "Bearer " + notification.heiiOnCallApiKey,
},
};
// Post to Heii On-Call Trigger https://heiioncall.com/docs#manual-triggers
2024-02-13 02:14:41 +02:00
try {
await axios.post(
`${heiiOnCallBaseUrl}/triggers/${notification.heiiOnCallTriggerId}/${action}`,
payload,
config
);
} catch (error) {
this.throwGeneralAxiosError(error);
}
return "Heii On-Call post sent successfully.";
2024-02-13 01:58:54 +02:00
}
}
module.exports = HeiiOnCall;