zabbix使用企業微信發消息


注冊一個企業微信,https://work.weixin.qq.com/

接收消息有2種方式,一是用企業微信,二是用個人微信(需要關注企業號,需要登錄掃描下圖邀請關注的二維碼):

 

 

 

官方api說明

地址:https://work.weixin.qq.com/api/doc#10167

 

過程

 

一:創建自建應用「報警」,然后用公司corpid和企業應用secret獲取token,https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$corpid&corpsecret=$corpsecret

 

二:帶着token、touser、agentid、content等參數,向接口https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$token 進行post數據

 

一、創建自建應用

自建應用里會生成AgentId和Secret

二、首先測試連接

 

地址:http://work.weixin.qq.com/api/devtools/devtool.php

 

corpid在「我的企業」最下面顯示,corpsecret就是上面的企業應用里的Secret的值

 

如果有返回一個access_token值,就說明沒問題

 

在zabbix里生成發送腳本

 

具體的代碼如下:

 

#!/bin/bash

access_token=$(/usr/bin/curl -s -G "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=xxxxxxxxx&corpsecret=xxxxxxxxxxxx" | awk -F\" '{print $10}')

#紅色xxxx部分修改為你自己的

 

function body() {

        local UserID="$1"

        local PartyID=""

        local AppID="1000002"

        local Msg="$2"

        printf '{\n'

        printf '\t"touser": "'"$UserID"\"",\n"

        printf '\t"toparty": "'"$PartyID"\"",\n"

        printf '\t"msgtype": "text",\n'

        printf '\t"agentid": "'"$AppID"\"",\n"

        printf '\t"text": {\n'

        printf '\t\t"content": "'"$Msg"\""\n"

        printf '\t},\n'

        printf '\t"safe":"0"\n'

        printf '}\n'

}

 

/usr/bin/curl --data-ascii "$(body $1 $2)" "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$access_token"

 

測試

sh weixin.sh 賬號 發送內容

注意:賬號在企業微信「通訊錄」里指用戶賬號,是一個唯一值,不能用姓名和英文名。


免責聲明!

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



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