shell脚本--用企业微信实现发送信息

由于token有时效,需要安装jq–yum -y install jq

#!/bin/sh

expireTime=7200
dbFile="db.json"

corpid="XXX"  #企业IP
agentid=XXX#应用ID
corpsecret="XXXX" #自建应用,单独的secret
touser="XXX" # 接收者用户名,@all 全体成员
toparty="2"  # 接收部门ID
content="内容"


if [ ! -f "$dbFile" ];then
        touch "$dbFile"
fi

# 获取token
req_time=`jq '.req_time' $dbFile`
current_time=$(date +%s)
refresh=false
if [ ! -n "$req_time" ];then
        refresh=true
else
        if [ $((current_time-req_time)) -gt $expireTime ];then
        refresh=true
        fi
fi
if $refresh ;then
	req_access_token_url="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$corpid&corpsecret=$corpsecret"
	echo $req_access_token_url
	access_res=$(curl -s -G $req_access_token_url | jq -r '.access_token')

	## 保存文件
	echo "" > $dbFile
	echo -e "{" > $dbFile
	echo -e "\t\"access_token\":\"$access_res\"," >> $dbFile
	echo -e "\t\"req_time\":$current_time" >> $dbFile
	echo -e "}" >> $dbFile
	echo ">>>刷新Token成功<<<"
fi 

## 发送消息
msg_body="{\"touser\":\"$touser\",\"toparty\":\"$toparty\",\"msgtype\":\"text\",\"agentid\":$agentid,\"text\":{\"content\":\"$content\"}}"
access_token=`jq -r '.access_token' $dbFile`
req_send_msg_url=https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$access_token
req_msg=$(curl -s -H "Content-Type: application/json" -X POST -d $msg_body $req_send_msg_url | jq -r '.errmsg')

echo "触发报警发送动作,返回信息为:" $req_msg
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值