Wechat is one of the largest messaging app by user count but its API and docs are not very developer-friendly. It is especially hard for non-Chinese speaking developers. This guide is my attempt to save others some pain and effort. Some solutions are opinionated and you are welcome to submit your suggestions via issues.
To register a (personal) public account, you will need a Chinese national ID. And there is a quota (currently 5) on how many accounts each ID can create. If you just want to play with the public account API and test your integration, you should start with the official sandboxed test accounts which only requires you to have a wechat account to create.
测试号二维码
panel. The test account will have a name like sandbox account of X
.3000
, run $ ngrok http 3000
and you will be automatically assigned a public forwarding domain that looks like your.ngrok.io
.your.ngrok.io/wechat
in the 接口配置信息
panel. As you might have guessed, we will be handling requests from wechat servers at the /wechat
endpoint.token
- as token in the 接口配置信息
panel. This string will serve as a secret for you to verify the identity of wechat servers.$ npm install wechat --save
.app.js
:var express = require('express');
var wechat = require('wechat');
var app = express();
app.use('/wechat', wechat('token', function (req, res, next) {
// message is located in req.weixin
var message = req.weixin;
console.log(message);
}));
app.listen(3000);
$ node app.js
or even better $ supervisor app.js
接口配置信息
panel to complete the webhook setup. It should show a green popup message briefly and the clicked button will disappear.