express-wend v0.1.2
express-wend
WeChat Backend for Node Express
Author: Gabriel Yum (Chicheng Ren)
MIT License
Quick Start
- Require the package
var expressWend = require('express-wend');
- Configure the Wend backend
var config = {
token: 'YOUR_TOKEN',
handleTextMsg: function(req, res) {...},
... (other handlers)
};
var wend = expressWend(config);
*: The configuration in WeChat Public Account panel uses the same route, you must make sure this token is the same one as the one you enter in that panel
- Add this middleware to a specific route
app.use('/sample', wend.getHandler(), ...other middleware here)
- WeChat Response
/* Sample: send back text content */
var config = {
...
handleTextMsg: function(req, res, next) {
var params = {
msgType: 'text',
content: 'Hello World'
};
res.wend.send(params);
next();
}
Injection into req, res
Express-wend inject objects into req and res in the given context (*: only available in handlers and later middleware)
- req.wend
console.log(req.wend);
/*
Expected output:
{
content: {
toUserName: ...,
fromUserName: ...
...
},
msgType: 'text' (or other)
eventType: null (exists if it is an event)
}
*/
- res.wend
var params = {
msgType: 'text',
content: 'Hello World'
};
res.wend.send(params);
Available message and event handlers
All the properties under req.wend.content follows the WeChat official Document using camel-case convention
Click here for all possible Messages
Click here for all possible Events
handleTextMsg(req, res, next)
msgType: 'text'
handleImageMsg(req, res, next)
msgType: 'image'
handleVoiceMsg(req, res, next)
msgType: 'voice'
handleVideoMsg(req, res, next)
msgType: 'video'
handleShortVideoMsg(req, res, next)
msgType: 'short-video'
handleLocationMsg(req, res, next)
msgType: 'location'
handleLinkMsg(req, res, next)
msgType: 'link'
handleSubscribeEvent(req, res, next)
msgType: 'event' eventType: 'subscribe'
handleUnsubscribeEvent(req, res, next)
msgType: 'event' eventType: 'unsubscribe'
handleClickEvent(req, res, next)
msgType: 'event' eventType: 'click'
handleScanSubscribeEvent(req, res, next)
msgType: 'event' eventType: 'scan-subscribe'
handleScanEvent(req, res, next)
msgType: 'event' eventType: 'scan'
handleViewEvent(req, res, next)
msgType: 'event' eventType: 'view'
handleLocationUpdateEvent(req, res, next)
msgType: 'event' eventType: 'location'
All possible message types
TBD
TODO List:
test cases
documentations
res.wend.send - README