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 '{\n'
 19   printf '\t"touser": "'$UserID'",\n'
 20   #printf '\t"toparty": "$PartyID",\n'
 21   printf '\t"msgtype": "text",\n'
 22   printf '\t"agentid": "'$AppID'",\n'
 23   printf '\t"text": {\n'
 24   printf '\t\t"content": "'$Msg'"\n'
 25   printf '\t},\n'
 26   printf '\t"safe":"0"\n'
 27   printf '}\n'
 28 }
 29 #body $1
 30 curl --data-ascii "$(body $1)" $PURL
 31 printf '\n'
 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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM