去年曾介绍过用脚本给微信订阅号关注者发送消息的方法,今天介绍一个使用自建服务器给企业号的关注者发消息(文字及图片)、接收消息、自动回复消息及做相应自动化操作的方法。
运维方面,可用来发告警的文字消息或图片,然后手机上收到消息后,可以通过在手机上直接回复已设定好的关键字或相应的命令,这时会传到自建服务器上,就可以自动运行相应的脚本、命令进行自动化操作,并可将操作结果发送出来。
闲话不多说,开始说步骤:
一、shell脚本发送文字消息或图片
1.到微信公众平台网站上(https://mp.weixin.qq.com/)注册一个企业号(其中主体类型选为“团队”的话可以不用提供企业相关证明)。
2.手机关注微信号
3.登陆企业号后台,选择“设置”-“功能设置”-“权限管理”-“发送消息”,可以看到CorpID和Secret,记下来后面会用到。
4.在服务器写shell脚本:
#!/bin/bash
#返回access token
function getToken(){ #传入参数$1为corpid,参数$2为corpsecret
curl -s "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$1&corpsecret=$2" | awk -F '"' '{print $4}'
}
#返回media_id
function getMediaId(){ #传入参数$1为access token;参数$2为图片文件
curl -s -F media=@$2 "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=$1&type=image" | awk -F '"' '{print $8}'
}
#发送文字消息
function sendText(){ #传入参数$1为access token,$2为消息内容,$3指定接收消息的账号
curl -d '{"touser": "'$3'", "msgtype": "text", "agentid": 0, "text": {"content": "'$2'"}, "safe":"0"}' "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$1"
}
#发送图片消息
function sendImage(){ #传入参数$1为access token;参数$2为media_id,$3指定接收消息的账号
postdata='{"touser": "'$3'", "msgtype": "image", "agentid": 0, "image": {"media_id": "'$2'"}, "safe":"0"}'
curl -d "$postdata" "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$1"
}
corpid='wxxxxxxxxxxxxx' #使用前面记下来的值替换
corpsecret='xxxxxxxxxxxxxxxxxxxx' #使用前面记下来的值替换
image='test.png'
text='这是发送的内容'
receiver='158xxxxxxxx' #此处为接收者的id,根据企业号后台的设置(ID),可以是手机号、微信号或其它的。同时发送到多个关注者用“|”隔开。
token=`getToken $corpid $corpsecret`
sendText $token $text $receiver
media_id=`getMediaId $token $image`
sendImage $token $media_id $receiver
运行脚本后手机就可以收到信息了:

二、设置微信服务器将收到的消息转发至自建服务器,自建服务器可以根据内容自动运行相应的脚本,以及进行自动回复。
1.在企业号后台 “应用中心”-“企业小服手”-“模式选择”里选择“回调模式”,填入自建服务器的url( http://wx.test.net:8080/Sample.php),并自动生成token和EncodingAESKey,记下来后面使用。
2.下载微信官方提供的加密库,这里下载的是php的。(http://qydev.weixin.qq.com/wiki/index.php?title=加解密库下载与返回码)
3.修改Sample.php ,注意!此处官方提供的脚本有误!
<?php
include_once "WXBizMsgCrypt.php";
$encodingAesKey = "XXXXXXXXXX"; //替换成上面生成的
$token = "XXXXXXXX"; //替换成上面生成的
$corpId = "wxxxxxxxxxx"; //#替换成上面生成自己的corpID
$sVerifyMsgSig = $_GET["msg_signature"]; //此处几行官方提供的有坑!
$sVerifyTimeStamp = $_GET["timestamp"];
$sVerifyNonce = $_GET["nonce"];
$sVerifyEchoStr = $_GET["echostr"];
$sEchoStr = "";
$wxcpt = new WXBizMsgCrypt($token, $encodingAesKey, $corpId);
$errCode = $wxcpt->VerifyURL($sVerifyMsgSig, $sVerifyTimeStamp, $sVerifyNonce, $sVerifyEchoStr, $sEchoStr);
if ($errCode == 0) {
echo $sEchoStr;
} else {
print("ERR: " . $errCode . "\n\n");
}
?>
4.回到企业号后台点击保存验证。
5.验证通过,修Sample.php进行消息接收和自动回复
<?php
include_once "WXBizMsgCrypt.php";
$encodingAesKey = "XXXXXXXXXXXXX";
$token = "XXXXXXXXXXXXXXXXXXX";
$corpId = "wxXXXXXXXXXXXXXXXX";
$wxcpt = new WXBizMsgCrypt($token, $encodingAesKey, $corpId);
$sReqMsgSig = $_GET["msg_signature"];
$sReqTimeStamp = $_GET["timestamp"];
$sReqNonce =$_GET["nonce"];
$sReqData = $GLOBALS["HTTP_RAW_POST_DATA"];
$sMsg = "";
$errCode = $wxcpt->DecryptMsg($sReqMsgSig, $sReqTimeStamp, $sReqNonce, $sReqData, $sMsg);
if ($errCode == 0) {
$postStr=$sMsg;
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$content = trim($postObj->Content);
//根据手机回复的内容执行相应的脚本或命令,也可以将执行结果再发送到关注者手机上。
if($fromUsername == '15800000000' && $content == "下线XX服务器"){
system('/usr/local/script/offline xx');
}elseif($fromUsername == '15800000000' && $content == "command" ){
system('command');
}
$time = time();
if($fromUsername=='15800000000') //关注者的ID
$reply='我自己'; //可以根据不同关注者、不同$content自动回复不同内容
else if ($fromUsername=='13900000000')
$reply='关注者二';
else
$reply='其它人';
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[你说的是%s,你是$reply]]></Content>
</xml>";
$sRespData= sprintf($textTpl, $fromUsername, $toUsername, $time, $content);
$sEncryptMsg = "";
$errCode = $wxcpt->EncryptMsg($sRespData, $sReqTimeStamp, $sReqNonce, $sEncryptMsg);
if ($errCode == 0) {
echo $sEncryptMsg;
} else {
print("ERR: " . $errCode . "\n\n");
// exit(-1);
}
} else {
print("ERR: " . $errCode . "\n\n");
//exit(-1);
}
?>
在手机上对企业号发送消息“测试测试”,收到了服务器的自动回复:

原文出自:
http://blog.too2.net/?p=230
转载请注明转自:辛碌力成【http://blog.too2.net】
发表评论