2021-09-07 14:42:46 +00:00
|
|
|
const NotificationProvider = require("./notification-provider");
|
|
|
|
const axios = require("axios");
|
|
|
|
|
|
|
|
class Octopush extends NotificationProvider {
|
|
|
|
name = "octopush";
|
|
|
|
|
2023-08-11 07:46:41 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2021-09-07 14:42:46 +00:00
|
|
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
2024-03-14 13:21:15 +00:00
|
|
|
const okMsg = "Sent Successfully.";
|
|
|
|
const urlV2 = "https://api.octopush.com/v1/public/sms-campaign/send";
|
|
|
|
const urlV1 = "https://www.octopush-dm.com/api/sms/json";
|
2021-09-07 14:42:46 +00:00
|
|
|
|
|
|
|
try {
|
2021-10-05 17:43:04 +00:00
|
|
|
// Default - V2
|
2022-10-01 14:03:28 +00:00
|
|
|
if (notification.octopushVersion === "2" || !notification.octopushVersion) {
|
2021-10-05 17:43:04 +00:00
|
|
|
let config = {
|
2021-09-13 08:22:05 +00:00
|
|
|
headers: {
|
2021-10-05 17:43:04 +00:00
|
|
|
"api-key": notification.octopushAPIKey,
|
|
|
|
"api-login": notification.octopushLogin,
|
|
|
|
"cache-control": "no-cache"
|
2021-09-07 14:42:46 +00:00
|
|
|
}
|
2021-10-05 17:43:04 +00:00
|
|
|
};
|
|
|
|
let data = {
|
2021-09-13 08:22:05 +00:00
|
|
|
"recipients": [
|
2021-10-05 17:43:04 +00:00
|
|
|
{
|
2021-09-13 08:22:05 +00:00
|
|
|
"phone_number": notification.octopushPhoneNumber
|
2021-10-05 17:43:04 +00:00
|
|
|
}
|
2021-09-13 08:22:05 +00:00
|
|
|
],
|
|
|
|
//octopush not supporting non ascii char
|
|
|
|
"text": msg.replace(/[^\x00-\x7F]/g, ""),
|
|
|
|
"type": notification.octopushSMSType,
|
|
|
|
"purpose": "alert",
|
|
|
|
"sender": notification.octopushSenderName
|
2021-10-05 17:43:04 +00:00
|
|
|
};
|
2024-03-14 13:21:15 +00:00
|
|
|
await axios.post(urlV2, data, config);
|
2022-10-01 14:03:28 +00:00
|
|
|
} else if (notification.octopushVersion === "1") {
|
2021-10-05 17:43:04 +00:00
|
|
|
let data = {
|
2021-09-13 08:22:05 +00:00
|
|
|
"user_login": notification.octopushDMLogin,
|
2021-10-05 17:43:04 +00:00
|
|
|
"api_key": notification.octopushDMAPIKey,
|
|
|
|
"sms_recipients": notification.octopushDMPhoneNumber,
|
|
|
|
"sms_sender": notification.octopushDMSenderName,
|
2022-04-17 07:43:03 +00:00
|
|
|
"sms_type": (notification.octopushDMSMSType === "sms_premium") ? "FR" : "XXX",
|
2021-10-05 17:43:04 +00:00
|
|
|
"transactional": "1",
|
|
|
|
//octopush not supporting non ascii char
|
|
|
|
"sms_text": msg.replace(/[^\x00-\x7F]/g, ""),
|
|
|
|
};
|
2021-09-13 08:22:05 +00:00
|
|
|
|
2021-10-05 17:43:04 +00:00
|
|
|
let config = {
|
|
|
|
headers: {
|
|
|
|
"cache-control": "no-cache"
|
|
|
|
},
|
|
|
|
params: data
|
|
|
|
};
|
2022-10-01 14:42:34 +00:00
|
|
|
|
|
|
|
// V1 API returns 200 even on error so we must check
|
|
|
|
// response data
|
2024-03-14 13:21:15 +00:00
|
|
|
let response = await axios.post(urlV1, {}, config);
|
2022-10-01 14:42:34 +00:00
|
|
|
if ("error_code" in response.data) {
|
2022-10-01 18:48:00 +00:00
|
|
|
if (response.data.error_code !== "000") {
|
|
|
|
this.throwGeneralAxiosError(`Octopush error ${JSON.stringify(response.data)}`);
|
|
|
|
}
|
2022-10-01 14:42:34 +00:00
|
|
|
}
|
2021-10-05 17:43:04 +00:00
|
|
|
} else {
|
|
|
|
throw new Error("Unknown Octopush version!");
|
|
|
|
}
|
2021-09-07 14:42:46 +00:00
|
|
|
|
|
|
|
return okMsg;
|
|
|
|
} catch (error) {
|
|
|
|
this.throwGeneralAxiosError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Octopush;
|