使用微信做系统监控告警的脚本

之前想用微信做监控,没有找到api,用公众号不会有信息提示。后来研究了一下企业微信,接口比较简单,几乎和钉钉一毛一样。同时微信和企业微信关联,可以产生信息提醒,不需要安装企业微信客户端。可以自己注册一个企业号,很方便。

使用方式:脚本名称 参数1 参数2

参数1为接受人员 ,多个用“|”分割;

参数2为信息内容

corpid,corpsecret,agentid根据自己注册的内容修改,脚本里边一部分查询部门列表的内容没有用可以删掉,token可以临时缓存一下提高效率。

#!/bin/php

<?php

//获取token
$corpid="ccccccccccccccccccccccccccccccccccccccccccc";
$corpsecret="ccccccccccccccccccccccccccccccccccccccccccccccccccccccccc";
$agentid="ccccccc";
$userlist=array();

$url="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=".$corpid."&corpsecret=".$corpsecret;
$ch=curl_init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_HEADER,0);
$output=curl_exec($ch);
curl_close($ch);
$out_array=json_decode($output,true);

$access_token=$out_array["access_token"];
echo $access_token;


$url="https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=".$access_token;
$ch=curl_init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_HEADER,0);
$output=curl_exec($ch);
curl_close($ch);
$out_array=json_decode($output,true);
print_r($out_array);

$url="https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=".$access_token."&department_id=1";
$ch=curl_init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_HEADER,0);
$output=curl_exec($ch);
curl_close($ch);
$out_array=json_decode($output,true);
print_r($out_array);
$url="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=".$access_token;
$post_array=array("touser" => $argv[1],"agentid"=> $agentid, "msgtype" => "text", "text" => array("content" => $argv[2]));
$post_string=json_encode($post_array);

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      'Content-Type: application/json',
      'Content-Length: ' . strlen($post_string))
);
$output = curl_exec($ch);
curl_close($ch);
print_r($output);



?>


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值