Getting Started with Node.js WeChat Official Account Development
Setting up a server: WeChat Official Account development requires a server to receive and process messages. I recommend applying for a free cloud server from Tencent Cloud, click here to apply, available at 9:30 AM daily. I chose Ubuntu as the server image. For how to set up a Node environment on the server, refer to my other blog post Using Linux for Web Frontend Development. The principle of Official Account development is to set up a receiving interface; once developer mode is enabled, WeChat’s server will forward messages to this interface.
Setting Up a Server
WeChat Official Account development requires a server to receive and process messages. I recommend applying for a free cloud server from Tencent Cloud, click here to apply, available at 9:30 AM daily. I chose Ubuntu as the server image. For how to set up a Node environment on the server, refer to my other blog post Using Linux for Web Frontend Development. The principle of Official Account development is to set up a receiving interface; once developer mode is enabled, WeChat’s server will forward messages to this interface.
Development Steps
Fill in Server Configuration

Verify Server Address Validity
After completing the configuration, the server will receive a GET verification request from WeChat, which includes the following parameters:
- signature - WeChat encrypted signature, encrypted using the token parameter filled in by the developer along with the timestamp and nonce parameters from the request
- timestamp - Timestamp
- nonce - Random number
- echostr - Random string; when verification passes, return this string to WeChat’s server to complete the verification
Verification Process
- Sort the token, timestamp, and nonce parameters in lexicographic order
- Concatenate the three parameter strings into one string and perform SHA1 encryption
- The developer can compare the encrypted string with the signature to verify that the request originates from WeChat
Verification Code
1 | app.get('/wechat', (req, res) => { |
Implement Business Logic Based on the API Documentation
Taking text messages as an example, below is a text message forwarded by WeChat to the server:
1
2
3
4
5
6
7
8<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>1348831860</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[this is a test]]></Content>
<MsgId>1234567890123456</MsgId>
</xml>
Message Handling:
1 | app.post('/wechat', (req, res) => { |
The turingRobot function is used to send user messages to the Turing Robot:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20const request = require('request');
function getTuringResponse(info) {
if(typeof info !== 'string') {
info = info.toString();
}
let options = {
method:'GET',
url: 'http://www.tuling123.com/openapi/api?key=13a74dbd0f6b45d69ac49334e7027742&info='+info
};
return new Promise((resolve, reject) => {
request(options, (err, res, body) => {
if (res) {
resolve(body);
} else {
reject(err);
}
});
})
}
module.exports = getTuringResponse;
The auto-reply module autoReply:
1 | function autoReply(requestData, info) { |
One More Thing
Using WeChat’s official Node middleware makes Official Account development more convenient and efficient.
Link
1 | const turingRobot = require('./turingRobot'); |
Presentation slides
Source code
References
Node.js WeChat Development - Implementing Auto Reply
WeChat Official Platform Development Overview - Getting Started Guide (login required)
Using Linux for Web Frontend Development
Getting Started with Node.js WeChat Official Account Development
http://quanru.github.io/2016/08/14/Getting Started with Node.js WeChat Official Account Development

