天天看点

linux 脚本调用企业微信_微信企业号 发送信息 shell

##微信企业号发送信息shell

#可作为shell函数模块调用,用于微信通知、jenkins发版微信通知等等

微信API官方文档 https://work.weixin.qq.com/api/doc#90002/90151/90854

#!/bin/bash

# wechat.send.sh

# 微信企业号发送信息 shell

# blog https://www.cnblogs.com/elvi/p/11444388.html

##############################

function sendmsg() {

CorpID="wwe518*企业微信账号唯一ID"

Secret="自定义应用的密钥"

AgentId=1000004 #应用id

Url="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CorpID&corpsecret=$Secret"

Gtoken=$(curl -s -G $Url|awk -F\" '{print $10}')

PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken"

[ `echo $Gtoken |wc -L` -gt 100 ] || { echo "get token error !";exit 1; }

curl -s --data-ascii '{

"toparty": "'"$1"'",

"msgtype": "text",

"agentid": "'"$AgentId"'",

"text": {"content": "'"$2"'"}

}' $PURL &>/tmp/wechat.log

if [ `grep "errmsg.*ok" /tmp/wechat.log |wc -l` -ne 1 ];then

echo 'send error !'

cat /tmp/wechat.log

exit 1

fi

}

##############################

#测试 sendmsg

#内容

msg="@警告

主机:$(hostname)

信息:Node test

时间:$(date +"%F %T")

"

# 发送 "部门id" "内容"

sendmsg "3" "$msg"

##############################