天天看点

微信企业号:shell定时发送图片 到 指定人

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

<code>#!/bin/bash</code>

<code>#返回access token</code>

<code>function</code> <code>getToken(){ </code><code>#传入参数$1为corpid,参数$2为corpsecret</code>

<code>    </code><code>curl -s </code><code>"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$1&amp;corpsecret=$2"</code> <code>| </code><code>awk</code> <code>-F </code><code>'"'</code> <code>'{print $4}'</code>

<code>}</code>

<code>#返回media_id</code>

<code>function</code> <code>getMediaId(){ </code><code>#传入参数$1为access token;参数$2为图片文件</code>

<code>    </code><code>curl -s -F media=@$2 </code><code>"https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=$1&amp;type=image"</code> <code>| </code><code>awk</code> <code>-F </code><code>'"'</code> <code>'{print $8}'</code>

<code>#发送文字消息</code>

<code>function</code> <code>sendText(){ </code><code>#传入参数$1为access token,$2为消息内容,$3指定接收消息的账号</code>

<code>    </code><code>curl -d </code><code>'{"touser": "'</code><code>$3</code><code>'", "msgtype": "text", "agentid": 0, "text": {"content": "'</code><code>$2</code><code>'"}, "safe":"0"}'</code>  <code>"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$1"</code>

<code>#发送图片消息</code>

<code>function</code> <code>sendImage(){ </code><code>#传入参数$1为access token;参数$2为media_id,$3指定接收消息的账号</code>

<code>    </code><code>postdata=</code><code>'{"touser": "'</code><code>$3</code><code>'", "msgtype": "image", "agentid": 0, "image": {"media_id": "'</code><code>$2</code><code>'"}, "safe":"0"}'</code>

<code>    </code><code>curl -d </code><code>"$postdata"</code>  <code>"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$1"</code>

<code>corpid=</code><code>'xxxxxxxxxx'</code> <code>#使用前面记下来的值替换</code>

<code>corpsecret=</code><code>'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'</code>  <code>#使用前面记下来的值替换</code>

<code>image=</code><code>'test.png'</code>

<code>text=</code><code>'这是发送的内容'</code>

<code>receiver=</code><code>'mer_aqu'</code> <code>#此处为接收者的id,根据企业号后台的设置(ID),可以是手机号、微信号或其它的。同时发送到多个关注者用“|”隔开。</code>

<code>token=`getToken $corpid $corpsecret`</code>

<code>sendText $token $text $receiver</code>

<code>media_id=`getMediaId $token $image`</code>

<code>sendImage $token $media_id $receiver</code>

本文转自 295631788 51CTO博客,原文链接:http://blog.51cto.com/hequan/1899900,如需转载请自行联系原作者