summaryrefslogtreecommitdiffstats
path: root/server/notification-providers/alertnow.js
blob: 4257ca9cd17f32df9bcfd3d52b75be4bd93d6205 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const NotificationProvider = require("./notification-provider");
const axios = require("axios");
const { setting } = require("../util-server");
const { getMonitorRelativeURL, UP, DOWN } = require("../../src/util");

class AlertNow extends NotificationProvider {
    name = "AlertNow";

    /**
     * @inheritdoc
     */
    async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
        const okMsg = "Sent Successfully.";

        try {
            let textMsg = "";
            let status = "open";
            let eventType = "ERROR";
            let eventId = new Date().toISOString().slice(0, 10).replace(/-/g, "");

            if (heartbeatJSON && heartbeatJSON.status === UP) {
                textMsg = `[${heartbeatJSON.name}] ✅ Application is back online`;
                status = "close";
                eventType = "INFO";
                eventId += `_${heartbeatJSON.name.replace(/\s/g, "")}`;
            } else if (heartbeatJSON && heartbeatJSON.status === DOWN) {
                textMsg = `[${heartbeatJSON.name}] 🔴 Application went down`;
            }

            textMsg += ` - ${msg}`;

            const baseURL = await setting("primaryBaseURL");
            if (baseURL && monitorJSON) {
                textMsg += ` >> ${baseURL + getMonitorRelativeURL(monitorJSON.id)}`;
            }

            const data = {
                "summary": textMsg,
                "status": status,
                "event_type": eventType,
                "event_id": eventId,
            };

            await axios.post(notification.alertNowWebhookURL, data);
            return okMsg;
        } catch (error) {
            this.throwGeneralAxiosError(error);
        }

    }
}

module.exports = AlertNow;