Skip to content

Commit 34b3ce5

Browse files
committed
更新README文档,添加回复API的调用示例
1 parent a5344f2 commit 34b3ce5

File tree

1 file changed

+84
-15
lines changed

1 file changed

+84
-15
lines changed

README.md

Lines changed: 84 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,64 @@ app.use('/wechat', wechat('some token', function (req, res, next) {
6363
```
6464
备注:token在[微信平台上申请](http://mp.weixin.qq.com/cgi-bin/callbackprofile?type=info&t=wxm-developer-ahead&lang=zh_CN)
6565

66+
### 回复消息
67+
当用户发送消息到微信公众账号,自动回复一条消息。这条消息可以是文本、图片、语音、视频、音乐、图文。详见:[官方文档](http://mp.weixin.qq.com/wiki/index.php?title=发送被动响应消息)
68+
69+
#### 回复文本
70+
```
71+
res.reply('Hello world!');
72+
// 或者
73+
res.reply({type: "text", content: 'Hello world!'});
74+
```
75+
#### 回复图片
76+
```
77+
res.reply({
78+
type: "image",
79+
content: {
80+
mediaId: 'mediaId'
81+
}
82+
});
83+
```
84+
#### 回复语音
85+
```
86+
res.reply({
87+
type: "voice",
88+
content: {
89+
mediaId: 'mediaId'
90+
}
91+
});
92+
```
93+
#### 回复视频
94+
```
95+
res.reply({
96+
type: "video",
97+
content: {
98+
mediaId: 'mediaId',
99+
thumbMediaId: 'thumbMediaId'
100+
}
101+
});
102+
```
103+
#### 回复音乐
104+
```
105+
res.reply({
106+
title: "来段音乐吧",
107+
description: "一无所有",
108+
musicUrl: "http://mp3.com/xx.mp3",
109+
hqMusicUrl: "http://mp3.com/xx.mp3"
110+
});
111+
```
112+
#### 回复图文
113+
```
114+
res.reply([
115+
{
116+
title: '你来我家接我吧',
117+
description: '这是女神与高富帅之间的对话',
118+
picurl: 'http://nodeapi.cloudfoundry.com/qrcode.jpg',
119+
url: 'http://nodeapi.cloudfoundry.com/'
120+
}
121+
]);
122+
```
123+
66124
### WXSession支持
67125
由于公共平台应用的客户端实际上是微信,所以采用传统的Cookie来实现会话并不现实,为此中间件模块在openid的基础上添加了Session支持。一旦服务端启用了`connect.session`中间件,在业务中就可以访问`req.wxsession`属性。这个属性与`req.session`行为类似。
68126

@@ -153,8 +211,8 @@ List.add('view', [
153211
## 详细API
154212
原始API文档请参见:[消息接口指南](http://mp.weixin.qq.com/wiki/index.php?title=消息接口指南)
155213

156-
目前微信公共平台能接收到6种内容:文字、图片、位置、音频、事件、链接。其中音频还未正式开放。支持三种回复:纯文本、图文、音乐。
157-
针对目前的业务形态,发布了0.3.x版本,该版本支持六种内容分别处理,以保持业务逻辑的简洁性。
214+
目前微信公共平台能接收到7种内容:文字、图片、音频、视频、位置、链接、事件。支持6种回复:纯文本、图文、音乐、音频、图片、视频
215+
针对目前的业务形态,发布了0.6.x版本,该版本支持六种内容分别处理,以保持业务逻辑的简洁性。
158216

159217
```
160218
app.use('/wechat', wechat('some token', wechat.text(function (message, req, res, next) {
@@ -172,7 +230,26 @@ app.use('/wechat', wechat('some token', wechat.text(function (message, req, res,
172230
// CreateTime: '1359124971',
173231
// MsgType: 'image',
174232
// PicUrl: 'http://mmsns.qpic.cn/mmsns/bfc815ygvIWcaaZlEXJV7NzhmA3Y2fc4eBOxLjpPI60Q1Q6ibYicwg/0',
233+
// MediaId: 'media_id',
175234
// MsgId: '5837397301622104395' }
235+
}).voice(function (message, req, res, next) {
236+
// message为音频内容
237+
// { ToUserName: 'gh_d3e07d51b513',
238+
// FromUserName: 'oPKu7jgOibOA-De4u8J2RuNKpZRw',
239+
// CreateTime: '1359125022',
240+
// MsgType: 'voice',
241+
// MediaId: 'OMYnpghh8fRfzHL8obuboDN9rmLig4s0xdpoNT6a5BoFZWufbE6srbCKc_bxduzS',
242+
// Format: 'amr',
243+
// MsgId: '5837397520665436492' }
244+
}).video(function (message, req, res, next) {
245+
// message为视频内容
246+
// { ToUserName: 'gh_d3e07d51b513',
247+
// FromUserName: 'oPKu7jgOibOA-De4u8J2RuNKpZRw',
248+
// CreateTime: '1359125022',
249+
// MsgType: 'video',
250+
// MediaId: 'OMYnpghh8fRfzHL8obuboDN9rmLig4s0xdpoNT6a5BoFZWufbE6srbCKc_bxduzS',
251+
// ThumbMediaId: 'media_id',
252+
// MsgId: '5837397520665436492' }
176253
}).location(function (message, req, res, next) {
177254
// message为位置内容
178255
// { ToUserName: 'gh_d3e07d51b513',
@@ -184,16 +261,6 @@ app.use('/wechat', wechat('some token', wechat.text(function (message, req, res,
184261
// Scale: '15',
185262
// Label: {},
186263
// MsgId: '5837398761910985062' }
187-
}).voice(function (message, req, res, next) {
188-
// message为音频内容
189-
// 微信官方还未正式开放音频内容,但是可以获取到部分信息,内容如下:
190-
// { ToUserName: 'gh_d3e07d51b513',
191-
// FromUserName: 'oPKu7jgOibOA-De4u8J2RuNKpZRw',
192-
// CreateTime: '1359125022',
193-
// MsgType: 'voice',
194-
// MediaId: 'OMYnpghh8fRfzHL8obuboDN9rmLig4s0xdpoNT6a5BoFZWufbE6srbCKc_bxduzS',
195-
// Format: 'amr',
196-
// MsgId: '5837397520665436492' }
197264
}).link(function (message, req, res, next) {
198265
// message为链接内容
199266
// { ToUserName: 'gh_d3e07d51b513',
@@ -218,7 +285,7 @@ app.use('/wechat', wechat('some token', wechat.text(function (message, req, res,
218285
})));
219286
```
220287

221-
注意: `text`, `image`, `location`, `voice`, `link`, `event`方法请至少指定一个。
288+
注意: `text`, `image`, `voice`, `video`, `location`, `link`, `event`方法请至少指定一个。
222289
这六个方法的设计适用于按内容类型区分处理的场景。如果需要更复杂的场景,请使用第一个例子中的API。
223290

224291
### 更简化的API设计
@@ -229,10 +296,12 @@ app.use('/wechat', wechat('some token').text(function (message, req, res, next)
229296
// TODO
230297
}).image(function (message, req, res, next) {
231298
// TODO
232-
}).location(function (message, req, res, next) {
233-
// TODO
234299
}).voice(function (message, req, res, next) {
235300
// TODO
301+
}).video(function (message, req, res, next) {
302+
// TODO
303+
}).location(function (message, req, res, next) {
304+
// TODO
236305
}).link(function (message, req, res, next) {
237306
// TODO
238307
}).event(function (message, req, res, next) {

0 commit comments

Comments
 (0)