summaryrefslogtreecommitdiffstats
path: root/server/notification-providers/linenotify.js
blob: 2622e3f1ca6db015980acf0c3eea429768fadb90 (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
const NotificationProvider = require("./notification-provider");
const axios = require("axios");
const qs = require("qs");
const { DOWN, UP } = require("../../src/util");

class LineNotify extends NotificationProvider {
    name = "LineNotify";

    /**
     * @inheritdoc
     */
    async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
        const okMsg = "Sent Successfully.";
        const url = "https://notify-api.line.me/api/notify";

        try {
            let config = {
                headers: {
                    "Content-Type": "application/x-www-form-urlencoded",
                    "Authorization": "Bearer " + notification.lineNotifyAccessToken
                }
            };
            if (heartbeatJSON == null) {
                let testMessage = {
                    "message": msg,
                };
                await axios.post(url, qs.stringify(testMessage), config);
            } else if (heartbeatJSON["status"] === DOWN) {
                let downMessage = {
                    "message": "\n[🔴 Down]\n" +
                        "Name: " + monitorJSON["name"] + " \n" +
                        heartbeatJSON["msg"] + "\n" +
                        `Time (${heartbeatJSON["timezone"]}): ${heartbeatJSON["localDateTime"]}`
                };
                await axios.post(url, qs.stringify(downMessage), config);
            } else if (heartbeatJSON["status"] === UP) {
                let upMessage = {
                    "message": "\n[✅ Up]\n" +
                        "Name: " + monitorJSON["name"] + " \n" +
                        heartbeatJSON["msg"] + "\n" +
                        `Time (${heartbeatJSON["timezone"]}): ${heartbeatJSON["localDateTime"]}`
                };
                await axios.post(url, qs.stringify(upMessage), config);
            }
            return okMsg;
        } catch (error) {
            this.throwGeneralAxiosError(error);
        }
    }
}

module.exports = LineNotify;