wechat-message-handlers
v0.1.0
Published
为处理微信消息提供一种简便统一的方式
Downloads
5
Readme
wechat-message-handlers
为处理微信消息提供一种简便统一的方式
Examples
Wechat Enterprise
以下是在微信企业号中使用的示例,来自wechat-enterprise-signup
import { Router } from 'express';
import wechat from 'wechat-enterprise';
import {handleEvent, handleText} from 'wechat-message-handlers';
const router = new Router();
let TextHandlers = {
/*
处理用户输入的姓名
*/
ready_for_name: (msg, req, res) => {
req.wxsession.user_name = msg.Content;
let str = `请输入您的手机号码:`;
req.wxsession.mobile_signup = 'ready_for_mobile';
res.reply(str);
},
ready_for_mobile: (msg, req, res) => {
req.wxsession.user_mobile = msg.Content;
let str = `${req.wxsession.user_name}, ${msg.Content}`;
res.reply(str);
},
unhandled: (msg, req, res) => {
res.reply('do nothing');
}
}
let EventHandlers = {
/*
开始进入自主注册的程序
*/
mobile_signup: (msg, req, res, next) => {
let str = `欢迎注册,请输入您的姓名:`
req.wxsession.mobile_signup = 'ready_for_name';
res.reply(str);
}
}
let getTextHandler = (msg, req) => {
if(req.wxsession && req.wxsession.mobile_signup) {
return TextHandlers[req.wxsession.mobile_signup];
} else {
return TextHandlers.unhandled;
}
}
router.use('/', wechat(wxcfg, wechat.text(handleText(getTextHandler))
.event(handleEvent(EventHandlers))));
export default router;
Notes
- 上述例子中的
TextHandlers
对象中,subscribe
和unsubscribe
被保留,用作特定的订阅和取消订阅时间的处理函数。