#!/usr/bin/php
<?php
$url = "http://xxx/OAuth/webservice/weixin.asmx/GetAccessToken?AuthCode=xxxxxxxxxx";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url );
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$token = curl_exec($curl);
$url2 = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$token;
//$ch = curl_init();
echo $url2;
echo "\r\n\r\n";
// define headers
//$headers = [
//"Content-Type: application/json",
//"X-PW-AccessToken:$user_access_token",
//"X-PW-Application:developer_api",
//"X-PW-UserEmail:$user_email"
//];
$alertsendto = $argv[1];
$alertsubject = $argv[2];
$alertmessage = $argv[3];
if(substr( $alertsubject, 0, 8 ) == "Resolved")
{
// define data
$data = [
"touser" => $alertsendto,
"template_id" => "749MvAxne8LWN0pjE8IjYj-y_kXtK2UH9Ll-1M6O5xk",
"url" => "http://zabbix.xxx.xxx",
"data" => [
"first" => [ "value" => $alertsubject, "color" => "#00e53d" ],
"keyword1" => [ "value" => "", "color" => "#00e53d" ],
"keyword2" => [ "value" => "", "color" => "#00e53d" ],
"remark" => [ "value" => $alertmessage, "color" => "#00e53d" ],
]
];
}
else
{
// define data
$data = [
"touser" => $alertsendto,
"template_id" => "NsHTxaiVSqkX6AOOVyy2QFfWSdsA5dwFUNUy_4Oz6W4",
"url" => "http://zabbix.xxx.xxx",
"data" => [
"first" => [ "value" => $alertsubject, "color" => "#ff2b2b" ],
"keyword1" => [ "value" => "", "color" => "#ff2b2b" ],
"keyword2" => [ "value" => "", "color" => "#ff2b2b" ],
"keyword3" => [ "value" => date("Y-m-d H:i:s"), "color" => "#ff2b2b" ],
"remark" => [ "value" => $alertmessage, "color" => "#ff2b2b" ],
]
];
}
$json_encoded_data = json_encode($data);
echo $json_encoded_data;
// setup curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_encoded_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// send request
$response = curl_exec ($ch);
if (!$response)
{
/*
* Handle errors here
* curl_error($ch) includes an error message
*/
echo 'CURL Error: ' . curl_error($ch);
} else {
/*
* Handle successful response here
* Server returns a JSON representation of the created people
*/
print($response);
}
// close curl session
curl_close ($ch);
?>
Comments