uptime-kuma/server/notification-providers/aliyun-sms.js

109 lines
3.2 KiB
JavaScript
Raw Normal View History

2021-10-12 23:55:01 -04:00
const NotificationProvider = require("./notification-provider");
const { DOWN, UP } = require("../../src/util");
2021-10-13 02:41:59 -04:00
const { default: axios } = require("axios");
const Crypto = require("crypto");
const qs = require("qs");
2021-10-12 23:55:01 -04: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-13 02:41:59 -04:00
var msgBody = JSON.stringify({
2021-10-12 23:55:01 -04:00
name: monitorJSON["name"],
time: heartbeatJSON["time"],
status: this.statusToString(heartbeatJSON["status"]),
msg: heartbeatJSON["msg"],
});
2021-10-13 02:41:59 -04:00
if (this.sendSms(notification, msgBody)) {
return okMsg;
}
} else {
var msgBody = JSON.stringify({
name: "",
time: "",
status: "",
msg: msg,
});
if (this.sendSms(notification, msgBody)) {
2021-10-12 23:55:01 -04:00
return okMsg;
}
2021-10-13 02:41:59 -04:00
}
2021-10-12 23:55:01 -04:00
} catch (error) {
this.throwGeneralAxiosError(error);
}
}
2021-10-13 02:41:59 -04:00
async sendSms(notification, msgbody) {
var params = {
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);
var config = {
method: "POST",
url: "http://dysmsapi.aliyuncs.com/",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
data: qs.stringify(params),
};
var result = await axios(config);
if (result.data.Message == "OK") {
return true;
}
return false;
}
/** Aliyun request sign */
sign(param, AccessKeySecret) {
var param2 = {},
data = [];
var oa = Object.keys(param).sort();
for (var i = 0; i < oa.length; i++) {
var key = oa[i];
param2[key] = param[key];
}
for (var key in param2) {
data.push(`${encodeURIComponent(key)}=${encodeURIComponent(param2[key])}`);
}
var StringToSign = `POST&${encodeURIComponent("/")}&${encodeURIComponent(data.join("&"))}`;
return Crypto
.createHmac("sha1", `${AccessKeySecret}&`)
.update(Buffer.from(StringToSign))
.digest("base64");
}
2021-10-12 23:55:01 -04:00
statusToString(status) {
switch (status) {
case DOWN:
return "DOWN";
case UP:
return "UP";
default:
return status;
}
}
}
module.exports = AliyunSMS;