shell脚本之微信报警功能的实现

导语:现在越来越流行微信报警功能了。下面就来看看具体实现吧!

1.先申请一个微信企业号

传送门:http://work.weixin.qq.com/

2.添加用户

2.创建应用

3.创建管理组并添加管理员

接下来准备三个东西:

CorpID 在我的企业一栏中

AgentId

Secret 这2个都在应用中

API调试传送门:http://work.weixin.qq.com/api/devtools/devtool.php

 

shell脚本的实现

  1 #!/bin/bash
  2 # -*- coding: utf-8 -*-
  3 ###SCRIPT_NAME:weixin.sh###
  4 ###send message from weixin for monitoring###
  5 ###leo###
  6 ###V1-2017-09-05
  7 
  8 CropID='xxx'
  9 Secret='xxx'
 10 GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret"
 11 Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F " '{print $10}')
 12 PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken"
 13 function body() {
 14   local int AppID=xxx           # 企业号中的应用id
 15   local UserID=$1            # 部门成员id,zabbix中定义的微信接收者
 16   #local PartyID=$2            # 部门id,定义了范围,组内成员都可接收到消息
 17   local Msg="hello" # 过滤出zabbix中传递的第三个参数
 18   printf '{
'
 19   printf '	"touser": "'$UserID'",
'
 20   #printf '	"toparty": "$PartyID",
'
 21   printf '	"msgtype": "text",
'
 22   printf '	"agentid": "'$AppID'",
'
 23   printf '	"text": {
'
 24   printf '		"content": "'$Msg'"
'
 25   printf '	},
'
 26   printf '	"safe":"0"
'
 27   printf '}
'
 28 }
 29 #body $1
 30 curl --data-ascii "$(body $1)" $PURL
 31 printf '
'
 32 echo "over!"

cli 测试

sh weixin.sh mei
{
    "touser": "mei",
    "msgtype": "text",
    "agentid": "xxx",
    "text": {
        "content": "hello"
    },
    "safe":"0"
}

over!

手机上

OK!到此,通过微信企业号发送报警就成功实现了!

 其他相关:

错误码查询页:https://work.weixin.qq.com/api/doc/#10649

发送消息帮助页:https://work.weixin.qq.com/api/doc/#10167

原文地址:https://www.cnblogs.com/leomei91/p/7479562.html