var Ding = {
key: null,
at_all: null,
message: null,
user_info: "187xxxxxxxxxxxxxx,180xxxxxxxxxxxxxx",
msgtype: "markdown",
proxy: null,
sendMessage: function () {
var params = {
msgtype: 'markdown',
markdown: {
title: Ding.title,
text: Ding.message
},
at: {
atMobiles: [
Ding.user_info,
],
isAtAll: Ding.at_all
}
},
data,
response,
request = new CurlHttpRequest(),
url =
"http://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
if (Ding.proxy) {
request.setProxy(Ding.proxy);
}
request.AddHeader("Content-Type: application/json");
data = JSON.stringify(params);
// Remove replace() function if you want to see the exposed key in the log file.
Zabbix.Log(
4,
"[Ding Webhook] URL: " + url.replace(Ding.key, "<BOT KEY>")
);
Zabbix.Log(4, "[Ding Webhook] params: " + data);
response = request.Post(url, data);
Zabbix.Log(4, "[Ding Webhook] HTTP code: " + request.Status());
try {
response = JSON.parse(response);
} catch (error) {
response = null;
}
if (request.Status() !== 200 || response.errcode !== 0) {
if (typeof response.errmsg === "string") {
throw response.errmsg;
} else {
throw "Unknown error. Check debug log for more information.";
}
}
},
};
try{
var params = JSON.parse(value); //格式化脚本参数
if(typeof params.URL === "undefined"){ //验证URL参数
throw 'parameter "URL" is missing';
}
if(typeof params.To === "undefined"){ //验证To参数
throw 'parameter "To" is missing';
}
if(typeof params.Message === "undefined"){ //验证Message参数
throw 'parameter "Message" is missing';
}
if(typeof params.Subject === "undefined"){ //验证Subject参数
throw 'parameter "Message" is missing';
}
// 验证All参数 判断是否@所有人
if(params.All){
Ding.at_all = ture;
}else{
Ding.at_all = false;
}
// 验证代理 是否添加代理
if(params.HTTPProxy){
Ding.proxy = params.HTTPProxy;
}
// 参数赋值
Ding.url = params.URL;
Ding.message = params.Message;
Ding.user_info = params.To;
Ding.title = params.Subject;
// 发送请求
Ding.sendMessage();
return "OK";
}catch(error){
Zabbix.log(4, "sending failed: " + error);
throw "sending failed: " + error + ".";
}