#!/bin/bash
#返回access token
function getToken(){ #传入参数$1为corpid,参数$2为corpsecret
curl -s "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$1&corpsecret=$2" | awk -F '"' '{print $4}'
}
#返回media_id
function getMediaId(){ #传入参数$1为access token;参数$2为图片文件
curl -s -F media=@$2 "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=$1&type=image" | awk -F '"' '{print $8}'
}
#发送文字消息
function sendText(){ #传入参数$1为access token,$2为消息内容,$3指定接收消息的账号
curl -d '{"touser": "'$3'", "msgtype": "text", "agentid": 0, "text": {"content": "'$2'"}, "safe":"0"}' "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$1"
}
#发送图片消息
function sendImage(){ #传入参数$1为access token;参数$2为media_id,$3指定接收消息的账号
postdata='{"touser": "'$3'", "msgtype": "image", "agentid": 0, "image": {"media_id": "'$2'"}, "safe":"0"}'
curl -d "$postdata" "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$1"
}
corpid='xxxxxxxxxx' #使用前面记下来的值替换
corpsecret='xxxxxxxxxxxxxxxxxxxxxxxxxxxxx' #使用前面记下来的值替换
image='test.png'
text='这是发送的内容'
receiver='mer_aqu' #此处为接收者的id,根据企业号后台的设置(ID),可以是手机号、微信号或其它的。同时发送到多个关注者用“|”隔开。
token=`getToken $corpid $corpsecret`
sendText $token $text $receiver
media_id=`getMediaId $token $image`
sendImage $token $media_id $receiver