Skip to content

Commit

Permalink
Merge pull request #16 from node-webot/koa2
Browse files Browse the repository at this point in the history
Support koa2
  • Loading branch information
JacksonTian committed Apr 17, 2017
2 parents 9173719 + ddd78b6 commit 615f93b
Show file tree
Hide file tree
Showing 10 changed files with 315 additions and 454 deletions.
25 changes: 21 additions & 4 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
{
"rules": {
"indent": [2, 2],
"quotes": [2, "single"],
"linebreak-style": [2, "unix"],
"indent": [
2,
2
],
"quotes": [
2,
"single"
],
"linebreak-style": [
2,
"unix"
],
"semi": [2, "always"],
"strict": [2, "global"],
"curly": 2,
Expand All @@ -29,7 +38,8 @@
"no-undef": 2,
"callback-return": [2, ["callback", "cb", "next"]],
"global-require": 0,
"no-console": 0
"no-console": 0,
"require-yield": 0
},
"env": {
"es6": true,
Expand All @@ -42,5 +52,12 @@
"before": true,
"after": true
},
"parserOptions": {
"ecmaVersion": 8,
"sourceType": "script",
"ecmaFeatures": {
"jsx": true
}
},
"extends": "eslint:recommended"
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
example
.DS_Store
coverage
.nyc_output
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
language: node_js
node_js:
- "4"
- "6"
- "7"
script: make test-coveralls
19 changes: 9 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
TESTS = test/*.js
REPORTER = spec
TIMEOUT = 20000
PATH := ./node_modules/.bin:$(PATH)
MOCHA = ./node_modules/mocha/bin/_mocha
COVERALLS = ./node_modules/coveralls/bin/coveralls.js
PATH := ./node_modules/.bin:$(PATH)

lint:
@eslint --fix lib index.js test

test:
NODE_ENV=test mocha -R $(REPORTER) -t $(TIMEOUT) \
$(MOCHA_OPTS) $(TESTS)
test: lint
@mocha -t $(TIMEOUT) -R spec $(TESTS)

test-cov:
@NODE_ENV=test \
istanbul cover --report html $(MOCHA) -- -t $(TIMEOUT) -R spec $(TESTS)
@nyc --reporter=html --reporter=text mocha -t $(TIMEOUT) -R spec $(TESTS)

test-coveralls:
@NODE_ENV=test \
istanbul cover --report lcovonly $(MOCHA) -- -t $(TIMEOUT) -R spec $(TESTS)
@nyc mocha -t $(TIMEOUT) -R spec $(TESTS)
@echo TRAVIS_JOB_ID $(TRAVIS_JOB_ID)
@cat ./coverage/lcov.info | $(COVERALLS) && rm -rf ./coverage
@nyc report --reporter=text-lcov | coveralls

.PHONY: test
147 changes: 73 additions & 74 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
co-wechat [![NPM version](https://badge.fury.io/js/co-wechat.png)](http://badge.fury.io/js/co-wechat) [![Build Status](https://travis-ci.org/node-webot/co-wechat.png?branch=master)](https://travis-ci.org/node-webot/co-wechat) [![Dependencies Status](https://david-dm.org/node-webot/co-wechat.png)](https://david-dm.org/node-webot/co-wechat) [![Coverage Status](https://coveralls.io/repos/node-webot/co-wechat/badge.png)](https://coveralls.io/r/node-webot/co-wechat)
======

微信公众平台消息接口服务中间件与API SDK
微信公众平台消息接口服务中间件与 API SDK

## 功能列表
- 自动回复(文本、图片、语音、视频、音乐、图文)
Expand All @@ -13,26 +13,25 @@ co-wechat [![NPM version](https://badge.fury.io/js/co-wechat.png)](http://badge.
$ npm install co-wechat
```

## Use with koa
## Use with koa2

```js
var wechat = require('co-wechat');
const wechat = require('co-wechat');

app.use(wechat('some token').middleware(function *() {
// 微信输入信息都在this.weixin上
var message = this.weixin;
app.use(wechat('some token').middleware(async (message) => {
// 微信输入信息就是这个 message
if (message.FromUserName === 'diaosi') {
// 回复屌丝(普通回复)
this.body = 'hehe';
return 'hehe';
} else if (message.FromUserName === 'text') {
//你也可以这样回复text类型的信息
this.body = {
return {
content: 'text object',
type: 'text'
};
} else if (message.FromUserName === 'hehe') {
// 回复一段音乐
this.body = {
return {
type: "music",
content: {
title: "来段音乐吧",
Expand All @@ -43,13 +42,13 @@ app.use(wechat('some token').middleware(function *() {
};
} else if (message.FromUserName === 'kf') {
// 转发到客服接口
this.body = {
return {
type: "customerService",
kfAccount: "test1@test"
};
} else {
// 回复高富帅(图文回复)
this.body = [
return [
{
title: '你来我家接我吧',
description: '这是女神与高富帅之间的对话',
Expand All @@ -60,101 +59,101 @@ app.use(wechat('some token').middleware(function *() {
}
}));
```
备注:token在微信平台的开发者中心申请

> 备注:token 在微信平台的开发者中心申请
### 回复消息

当用户发送消息到微信公众账号,自动回复一条消息。这条消息可以是文本、图片、语音、视频、音乐、图文。详见:[官方文档](http://mp.weixin.qq.com/wiki/index.php?title=发送被动响应消息)

#### 回复文本
```js
this.body = 'Hello world!';
async (message) => {
return 'Hello world!';
}
// 或者
this.body = {type: "text", content: 'Hello world!'};
async (message) => {
return {type: "text", content: 'Hello world!'};
}
```
#### 回复图片

```js
this.body = {
type: "image",
content: {
mediaId: 'mediaId'
}
};
async (message) => {
return {
type: "image",
content: {
mediaId: 'mediaId'
}
};
}
```
#### 回复语音

```js
this.body = {
type: "voice",
content: {
mediaId: 'mediaId'
}
};
async (message) => {
return {
type: "voice",
content: {
mediaId: 'mediaId'
}
};
}
```
#### 回复视频
```js
this.body = {
type: "video",
content: {
mediaId: 'mediaId',
thumbMediaId: 'thumbMediaId'
}
};
async (message) => {
return {
type: "video",
content: {
mediaId: 'mediaId',
thumbMediaId: 'thumbMediaId'
}
};
}
```
#### 回复音乐
```js
this.body = {
title: "来段音乐吧",
description: "一无所有",
musicUrl: "http://mp3.com/xx.mp3",
hqMusicUrl: "http://mp3.com/xx.mp3"
};
async (message) => {
return {
title: "来段音乐吧",
description: "一无所有",
musicUrl: "http://mp3.com/xx.mp3",
hqMusicUrl: "http://mp3.com/xx.mp3"
};
}
```
#### 回复图文
```js
this.body = [
{
title: '你来我家接我吧',
description: '这是女神与高富帅之间的对话',
picurl: 'http://nodeapi.cloudfoundry.com/qrcode.jpg',
url: 'http://nodeapi.cloudfoundry.com/'
}
];
async (message) => {
return [
{
title: '你来我家接我吧',
description: '这是女神与高富帅之间的对话',
picurl: 'http://nodeapi.cloudfoundry.com/qrcode.jpg',
url: 'http://nodeapi.cloudfoundry.com/'
}
];
}
```

#### 回复空串
```js
this.body = '';
async (message) => {
return '';
}
```

#### 转发到客服接口
```js
this.body = {
type: "customerService",
kfAccount: "test1@test" //可选
};
async (message) => {
return {
type: "customerService",
kfAccount: "test1@test" //可选
};
}
```

### WXSession支持
由于公共平台应用的客户端实际上是微信,所以采用传统的Cookie来实现会话并不现实,为此中间件模块在openid的基础上添加了Session支持。一旦服务端启用了`koa-generic-session`中间件,在业务中就可以访问`this.wxsession`属性。这个属性与`this.session`行为类似。

```js
var session = require('koa-generic-session');
app.use(session());
app.use(wechat('some token').middleware(function *() {
var info = this.weixin;
if (info.Content === '=') {
var exp = this.wxsession.text.join('');
this.wxsession.text = '';
this.body = exp;
} else {
this.wxsession.text = this.wxsession.text || [];
this.wxsession.text.push(info.Content);
this.body = '收到' + info.Content;
}
}));
```

`this.wxsession``this.session`采用相同的存储引擎,这意味着如果采用redis作为存储,这样`wxsession`可以实现跨进程共享。

## Show cases
### Node.js API自动回复

Expand Down
Loading

0 comments on commit 615f93b

Please sign in to comment.