linux企业微信api接口,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://me.jinchuang.org/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

执行过程结果图例

29233b0dcf6fc6891488b98620fa3afd.png

商业转载请联系作者获得授权,非商业转载请注明出处 本文地址:https://wojc.cn/archives/983.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值