2021-10-07 12:56:32 -07:00
|
|
|
const NotificationProvider = require("./notification-provider");
|
|
|
|
const axios = require("axios");
|
|
|
|
|
|
|
|
class PromoSMS extends NotificationProvider {
|
|
|
|
name = "promosms";
|
|
|
|
|
2023-08-11 00:46:41 -07:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2021-10-07 12:56:32 -07:00
|
|
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
2024-03-14 06:21:15 -07:00
|
|
|
const okMsg = "Sent Successfully.";
|
|
|
|
const url = "https://promosms.com/api/rest/v3_2/sms";
|
2021-10-07 12:56:32 -07:00
|
|
|
|
2023-01-11 05:32:57 -08:00
|
|
|
if (notification.promosmsAllowLongSMS === undefined) {
|
2023-01-11 05:36:33 -08:00
|
|
|
notification.promosmsAllowLongSMS = false;
|
2023-01-11 05:32:57 -08:00
|
|
|
}
|
|
|
|
|
2023-01-11 23:14:31 -08:00
|
|
|
//TODO: Add option for enabling special characters. It will decrese message max length from 160 to 70 chars.
|
|
|
|
//Lets remove non ascii char
|
|
|
|
let cleanMsg = msg.replace(/[^\x00-\x7F]/g, "");
|
|
|
|
|
2021-10-07 12:56:32 -07:00
|
|
|
try {
|
|
|
|
let config = {
|
|
|
|
headers: {
|
2021-10-08 00:11:13 -07:00
|
|
|
"Content-Type": "application/json",
|
2022-04-13 09:30:32 -07:00
|
|
|
"Authorization": "Basic " + Buffer.from(notification.promosmsLogin + ":" + notification.promosmsPassword).toString("base64"),
|
2021-10-08 00:11:13 -07:00
|
|
|
"Accept": "text/json",
|
2021-10-07 12:56:32 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
let data = {
|
2021-10-08 00:11:13 -07:00
|
|
|
"recipients": [ notification.promosmsPhoneNumber ],
|
2023-01-11 23:14:31 -08:00
|
|
|
//Trim message to maximum length of 1 SMS or 4 if we allowed long messages
|
|
|
|
"text": notification.promosmsAllowLongSMS ? cleanMsg.substring(0, 639) : cleanMsg.substring(0, 159),
|
2023-01-11 05:32:57 -08:00
|
|
|
"long-sms": notification.promosmsAllowLongSMS,
|
2021-10-08 00:11:13 -07:00
|
|
|
"type": Number(notification.promosmsSMSType),
|
2021-10-07 12:56:32 -07:00
|
|
|
"sender": notification.promosmsSenderName
|
|
|
|
};
|
|
|
|
|
2024-03-14 06:21:15 -07:00
|
|
|
let resp = await axios.post(url, data, config);
|
2021-10-08 02:49:12 -07:00
|
|
|
|
|
|
|
if (resp.data.response.status !== 0) {
|
|
|
|
let error = "Something gone wrong. Api returned " + resp.data.response.status + ".";
|
|
|
|
this.throwGeneralAxiosError(error);
|
|
|
|
}
|
2022-04-13 09:30:32 -07:00
|
|
|
|
2021-10-07 12:56:32 -07:00
|
|
|
return okMsg;
|
|
|
|
} catch (error) {
|
|
|
|
this.throwGeneralAxiosError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = PromoSMS;
|