Shell脚本利用企业微信API-发送应用消息-到企业微信

使用shell脚本发送消息到企业微信的应用中,脚本支持文本,图片,文件 三种消息类型

需要用到jq命令,jq是一个json解析工具:jq官网
* 注意应用权限对应的部门或者用户是否正确,不然会发送失败,可以选择只按部门或用户发送
* token需要缓存的话,需redis支持,然后去掉redis_token()和redis_token()代码的注释,把第20行token变量代码加上注释

# 安装jq命令
# centos
yum install jq -y

# ubuntu
apt-get install jq -y
#!/bin/bash
# Author:jinchuang [https://wojc.cn/archives/983.html]
# 发送消息到企业微信应用里面

# 企业id
	id="xxxxxxxxxxxxxxxxxx"
# 应用id
	agentid="xxxxxxx"
# 应用secret
	secret="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# 部门id
	toparty=x
# 用户id [也就是用户账号,多个用户用|符号分开]
	touser=""

# API接口
	url="https://qyapi.weixin.qq.com/cgi-bin"

# 获取token [这里token没做缓存,如果频繁调用gettoken接口,会受到频率拦截,官方默认token值有效时间2小时]
	token=`curl -s "$url/gettoken?corpid=$id&corpsecret=$secret"|jq -r .access_token`

# 获取token写入Redis缓存
#function get_token(){
#	token=`curl -s "$url/gettoken?corpid=$id&corpsecret=$secret"|jq -r .access_token`
#	redis-cli set token $token >/dev/null 2>&1
#	redis-cli expire token 3000 >/dev/null 2>&1
#}

# 从Redis缓存读取token
#function redis_token(){
#	ttl_token=`redis-cli ttl token|awk '{print $1}'`
#	if [ "$ttl_token" -le "-2" ]
#	then
#		get_token
#	else
#		token=`redis-cli get token`
#		test_token=`curl -s "$url/agent/get?access_token=$token&agentid=$agentid"|jq -r .errcode`
#		if [ "$test_token" -eq "0" ]
#		then
#			sleep 0.1
#		elif [ "$test_token" -ne "0" ]
#			get_token
#		fi
#	fi
#}
#redis_token

# 发送消息参数
	part="message/send?access_token=$token"

# 上传临时素材参数
	upload="media/upload?access_token=$token&type=$1"

# 执行提示
function tips(){
    code=`jq -r .errcode`
    if [ "$code" == "0" ]
	then 
		echo -e "\033[32mSend successfully\033[0m"
	else
		echo -e "\033[31mSend fail,errcode:$code\033[0m"
	fi
}

# 判断输入文件路径
function file_dir(){
	while true
	do
		read -p "请输入[ $1 ]的路径:" f
		[[ -f $f ]] && break || echo -e "\033[31m请输入正确的 $1 路径!\033[0m"
	done
	upload_file_dir="$f"
		
}

# 上传临时素材
function upload_file() {
	# 发送的文件路径
	file_dir $1
	
	# post 参数
	upload_post="-H 'Content-Type:multipart/form-data' -F 'filename=@$upload_file_dir;type=application/octet-stream' $url/$upload$1"
	
	# 获取临时素材id
	get_upload_part=`curl -s -X POST $upload_post`
	upload_status=`echo "$get_upload_part"|jq -r .errcode`
	if [[ $upload_status == 0 ]];then media_id=`echo "$get_upload_part"|jq -r .media_id`;else echo -e "\033[31mFailed to upload file [$f]\033[0m";exit;fi
}

# 发送文本消息 [这里按部门发送toparty]
function send_t(){
	read -p "输入文本内容:" t
	textmsg='{"touser":"'$touser'","toparty":"'$toparty'","msgtype":"'text'","agentid":"'$agentid'","'text'":{"content":"'$t'"},"safe":0}'
	curl -s -X POST -d "$textmsg"  "$url/$part"|tips
}

# 发送文件图片消息 [这里按部门发送toparty]
function send(){
	msg='{"touser":"'$touser'","toparty":"'$toparty'","msgtype":"'$1'","agentid":"'$agentid'","'$1'":{"media_id":"'$media_id'"},"safe":0}'
	curl -s -X POST -d "$msg" "$url/$part"|tips
}

# 执行脚本
echo "
1、文本消息
2、文件消息
3、图片消息
"
read -p "选择发送消息类型: " x
case $x in
1)
	send_t
;;
2)
	upload_file file && send file
;;
3)
	upload_file image && send image
;;
*)
	echo "输入错误,执行结束"
;;
esac


执行过程结果图例

qwq.png

相关文章

Linux
Shell脚本利用webhook机器人-发送群消息-到企业微信
Linux
Shell下bc命令处理和数值的四舍五入
Linux
查询和下载备份数据的临时脚本
Linux
Linux jar包服务启动停止shell脚本
赞赏图片

评论