mirror of
https://github.com/louislam/uptime-kuma.git
synced 2024-11-22 11:47:30 +01:00
a9a1cf1353
- I unified where in file the name of `NotificationProvider.name` is placed - I made sure that all the providers adhere to the signature of `NotificationProvider.send()` - I made sure that all the providers use `okMsg` if returning success messages directly from the function. Here a discussion should be had: Should this be refactored into a constant of `NotificationProvider`? I could imagine that `NotificationProvider.SENDING_SUCCESSFULL` could be a suitable alternative. - I made sure all providers have the URL they `POST`/`GET` to be extraced into a variable. => refactored this way due to Nelsons suggestion
74 lines
2.4 KiB
JavaScript
74 lines
2.4 KiB
JavaScript
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;
|