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 账号 发送内容

注意:账号在企业微信「通讯录」里指用户账号,是一个唯一值,不能用姓名和英文名。

猜你喜欢

转载自www.cnblogs.com/Ghost-bird/p/8961713.html