1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
#!/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
|
本文转自 295631788 51CTO博客,原文链接:http://blog.51cto.com/hequan/1899900,如需转载请自行联系原作者