2021-10-13 04:55:01 +01:00
|
|
|
const NotificationProvider = require("./notification-provider");
|
|
|
|
const { DOWN, UP } = require("../../src/util");
|
2021-10-13 07:41:59 +01:00
|
|
|
const { default: axios } = require("axios");
|
|
|
|
const Crypto = require("crypto");
|
|
|
|
const qs = require("qs");
|
2021-10-13 04:55:01 +01:00
|
|
|
|
|
|
|
class AliyunSMS extends NotificationProvider {
|
|
|
|
name = "AliyunSMS";
|
|
|
|
|
|
|
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
|
|
|
let okMsg = "Sent Successfully.";
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (heartbeatJSON != null) {
|
2021-10-14 09:24:03 +01:00
|
|
|
let msgBody = JSON.stringify({
|
2021-10-13 04:55:01 +01:00
|
|
|
name: monitorJSON["name"],
|
|
|
|
time: heartbeatJSON["time"],
|
|
|
|
status: this.statusToString(heartbeatJSON["status"]),
|
|
|
|
msg: heartbeatJSON["msg"],
|
|
|
|
});
|
2021-10-13 07:41:59 +01:00
|
|
|
if (this.sendSms(notification, msgBody)) {
|
|
|
|
return okMsg;
|
|
|
|
}
|
|
|
|
} else {
|
2021-10-14 09:24:03 +01:00
|
|
|
let msgBody = JSON.stringify({
|
2021-10-13 07:41:59 +01:00
|
|
|
name: "",
|
|
|
|
time: "",
|
|
|
|
status: "",
|
|
|
|
msg: msg,
|
|
|
|
});
|
|
|
|
if (this.sendSms(notification, msgBody)) {
|
2021-10-13 04:55:01 +01:00
|
|
|
return okMsg;
|
|
|
|
}
|
2021-10-13 07:41:59 +01:00
|
|
|
}
|
2021-10-13 04:55:01 +01:00
|
|
|
} catch (error) {
|
|
|
|
this.throwGeneralAxiosError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-13 07:41:59 +01:00
|
|
|
async sendSms(notification, msgbody) {
|
2021-10-14 09:24:03 +01:00
|
|
|
let params = {
|
2021-10-13 07:41:59 +01:00
|
|
|
PhoneNumbers: notification.phonenumber,
|
|
|
|
TemplateCode: notification.templateCode,
|
|
|
|
SignName: notification.signName,
|
|
|
|
TemplateParam: msgbody,
|
|
|
|
AccessKeyId: notification.accessKeyId,
|
|
|
|
Format: "JSON",
|
|
|
|
SignatureMethod: "HMAC-SHA1",
|
|
|
|
SignatureVersion: "1.0",
|
|
|
|
SignatureNonce: Math.random().toString(),
|
|
|
|
Timestamp: new Date().toISOString(),
|
|
|
|
Action: "SendSms",
|
|
|
|
Version: "2017-05-25",
|
|
|
|
};
|
|
|
|
|
|
|
|
params.Signature = this.sign(params, notification.secretAccessKey);
|
2021-10-14 09:24:03 +01:00
|
|
|
let config = {
|
2021-10-13 07:41:59 +01:00
|
|
|
method: "POST",
|
|
|
|
url: "http://dysmsapi.aliyuncs.com/",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
|
|
},
|
|
|
|
data: qs.stringify(params),
|
|
|
|
};
|
|
|
|
|
2021-10-14 09:24:03 +01:00
|
|
|
let result = await axios(config);
|
2021-10-13 07:41:59 +01:00
|
|
|
if (result.data.Message == "OK") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Aliyun request sign */
|
|
|
|
sign(param, AccessKeySecret) {
|
2021-10-14 09:24:03 +01:00
|
|
|
let param2 = {};
|
|
|
|
let data = [];
|
2021-10-13 07:41:59 +01:00
|
|
|
|
2021-10-14 09:24:03 +01:00
|
|
|
let oa = Object.keys(param).sort();
|
2021-10-13 07:41:59 +01:00
|
|
|
|
2021-10-14 09:24:03 +01:00
|
|
|
for (let i = 0; i < oa.length; i++) {
|
|
|
|
let key = oa[i];
|
2021-10-13 07:41:59 +01:00
|
|
|
param2[key] = param[key];
|
|
|
|
}
|
|
|
|
|
2021-10-14 09:24:03 +01:00
|
|
|
for (let key in param2) {
|
2021-10-13 07:41:59 +01:00
|
|
|
data.push(`${encodeURIComponent(key)}=${encodeURIComponent(param2[key])}`);
|
|
|
|
}
|
|
|
|
|
2021-10-14 09:24:03 +01:00
|
|
|
let StringToSign = `POST&${encodeURIComponent("/")}&${encodeURIComponent(data.join("&"))}`;
|
2021-10-13 07:41:59 +01:00
|
|
|
return Crypto
|
|
|
|
.createHmac("sha1", `${AccessKeySecret}&`)
|
|
|
|
.update(Buffer.from(StringToSign))
|
|
|
|
.digest("base64");
|
|
|
|
}
|
|
|
|
|
2021-10-13 04:55:01 +01:00
|
|
|
statusToString(status) {
|
|
|
|
switch (status) {
|
|
|
|
case DOWN:
|
|
|
|
return "DOWN";
|
|
|
|
case UP:
|
|
|
|
return "UP";
|
|
|
|
default:
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = AliyunSMS;
|