1.配置微信公众号
由于没有认证的公众号,只能通过自己申请的个人订阅号(可以自行申请),并到开发者工具中开通公众平台测试帐号实现该功能。
1.获取测试公众号appID和appsecret
*2.关注测试号二维码获取用户openid
3.新增模板获取模板ID
得到模板id: OA0PX8pqc2X7t_y05y5GxZ8LutBpu341FIYSeQOkno
2.通过脚本实现消息发送
#!/bin/sh # 微信消息发送脚本 zhutw #全局配置-- #微信公众号appID appID=wxe14bed8b9882c259 #微信公众号appsecret appsecret=f97826cd18236872b07e53e330ae02b6 #微信公众号发送消息模板 tpl_id=0P67Th60_C_xExoCIdDJyEUap26r_-MR5VJ1g1qBNjw #消息模板: # {{first.DATA}} # 项目名称:{{name.DATA}} # 报警时间:{{date.DATA}} # # {{remark.DATA}} #获取微信公众号AccessToken,并缓存到本地 函数 getAccessToken(){ if [ -f "$HOME/.wechat_accesstoken" ]; then access_token=`cat $HOME/.wechat_accesstoken | awk -F":" ‘{print $1}‘` expires_in=`cat $HOME/.wechat_accesstoken | awk -F":" ‘{print $2}‘` time=`cat $HOME/.wechat_accesstoken | awk -F":" ‘{print $3}‘` if [ -z $access_token ] || [ -z $expires_in ] || [ -z $time ]; then rm -f $HOME/.wechat_accesstoken getAccessToken fi else content=$(curl "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appID&secret=$appsecret") echo "get content: $content" access_token=`echo $content | awk -F "\"" ‘{print $4}‘` expires_in=`echo $content | awk -F "\"" ‘{print $7}‘ | cut -d"}" -f1|cut -c2-` echo "access_token = $access_token" echo "expires_in = $expires_in" time=$(date +%s) echo "$access_token:$expires_in:$time" > $HOME/.wechat_accesstoken if [ -z $access_token ] || [ -z $expires_in ] || [ -z $time ]; then echo "not get access_token" exit 0 fi fi remain=$[$(date +%s) - $time] limit=$[$expires_in - 60] if [ $remain -gt $limit ]; then rm -f $HOME/.wechat_accesstoken getAccessToken fi } #发送消息函数 sendMessage(){ #消息json体 message=`cat << EOF { "touser":"$openid", "template_id":"$tpl_id", "url":"$url", "data":{ "first": { "value":"$first", "color":"#FF0000" }, "name":{ "value":"$name", "color":"#173177" }, "date": { "value":"$date", "color":"#173177" }, "remark":{ "value":"$remark", "color":"#FF0000" } } } EOF ` echo "send message : $message" curl -X POST -H "Content-Type: application/json" https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$access_token -d "$message" } #帮助信息函数 usage(){ cat <<EOF usage: $0 [-u openids -s summary -n name -t time -d detail -l link] [-h] u wechat user openid , multiple comma separated s message summary n project name t alarm time d message detail l link address h output this help and exit EOF } #获取脚本执行参数 while getopts ":u:s:n:t:d:h:l:" op; do case $op in u) openids="$OPTARG" ;; s) first="$OPTARG" ;; n) name="$OPTARG" ;; t) date="$OPTARG" ;; d) remark="$OPTARG" ;; l) url="$OPTARG" ;; *) usage exit 0 ;; esac done #判断条件满足发送消息 if [[ -n $openids && -n $first && -n $name && -n $date ]]; then getAccessToken OLD_IFS="$IFS" IFS="," arr=($openids) IFS="$OLD_IFS" for openid in ${arr[@]} do sendMessage done exit $? else echo "params error." usage exit 1 fi
关于微信公众号接口说明查看如下接口wiki:
https://mp.weixin.qq.com/wiki
1.开始开发->获取access_token
2.消息管理->模板消息接口
3.接收到消息
执行命令脚本即上述shell脚本内容。记得设置脚本执行权限 chmod +x sendMessageForWechat
shell
./sendMessageForWechat -u o4bHbvjL9aWoRCa29vdOQ9aJMq0w -s "192.168.1.90磁盘空间不足" -n 测试系统 -t "2017-01-15 13:00:10" -d "磁盘已使用超过80%,剩余5G,请及时处理" -l "http://m.baidu.com"
点击消息,打开百度链接。。。
---------------------
作者:slimina
来源:CSDN
原文:https://blog.csdn.net/zhu_tianwei/article/details/71246627
版权声明:本文为博主原创文章,转载请附上博文链接!
原文地址:https://www.cnblogs.com/5ifbk/p/11115092.html
时间: 2024-10-10 10:21:40