summaryrefslogtreecommitdiffstats
path: root/server/notification-providers/smseagle.js
blob: 4e897006c77b7f8c71689addfda016609b3c7c2b (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const NotificationProvider = require("./notification-provider");
const axios = require("axios");

class SMSEagle extends NotificationProvider {
    name = "SMSEagle";

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

        try {
            let config = {
                headers: {
                    "Content-Type": "application/json",
                }
            };

            let postData;
            let sendMethod;
            let recipientType;

            let encoding = (notification.smseagleEncoding) ? "1" : "0";
            let priority = (notification.smseaglePriority) ? notification.smseaglePriority : "0";

            if (notification.smseagleRecipientType === "smseagle-contact") {
                recipientType = "contactname";
                sendMethod = "sms.send_tocontact";
            }
            if (notification.smseagleRecipientType === "smseagle-group") {
                recipientType = "groupname";
                sendMethod = "sms.send_togroup";
            }
            if (notification.smseagleRecipientType === "smseagle-to") {
                recipientType = "to";
                sendMethod = "sms.send_sms";
            }

            let params = {
                access_token: notification.smseagleToken,
                [recipientType]: notification.smseagleRecipient,
                message: msg,
                responsetype: "extended",
                unicode: encoding,
                highpriority: priority
            };

            postData = {
                method: sendMethod,
                params: params
            };

            let resp = await axios.post(notification.smseagleUrl + "/jsonrpc/sms", postData, config);

            if ((JSON.stringify(resp.data)).indexOf("message_id") === -1) {
                let error = "";
                if (resp.data.result && resp.data.result.error_text) {
                    error = `SMSEagle API returned error: ${JSON.stringify(resp.data.result.error_text)}`;
                } else {
                    error = "SMSEagle API returned an unexpected response";
                }
                throw new Error(error);
            }

            return okMsg;
        } catch (error) {
            this.throwGeneralAxiosError(error);
        }
    }
}

module.exports = SMSEagle;