summary:
WeChat, a huge user base and extremely strong user stickiness, has attracted the attention of countless developers in the past two years. Nodejs, a development tool that has developed very fast in the past two years, is especially suitable for building mobile backends. This article uses examples developed by the author himself to describe how to develop his own WeChat public account based on Nodejs. In this example, express, wechat, mongodb, monk and other modules are mainly used.
Preparation:
1. Apply for a WeChat official account and go to https://mp.weixin.qq.com/ to apply. I will not explain it too much here.
2. Purchase a server. Amazon's EC2 is recommended here. For the first time users can choose micro instance. It is free for one year and is very convenient to apply. Just enter your credit card information. The whole process is in English, but the year is free, and it is worth spending more time.
Install the NodeJs development environment:
The code copy is as follows:
1. yum -y install gcc
2. yum -y install gcc-c++
3. yum -y install make automake
4. wget http://nodejs.org/dist/v0.10.29/node-v0.10.29.tar.gz
5. tar -xvzf node-v0.10.29.tar.gz
6. cd unzip directory
7. ./configure
8. make
9. make install
Install Mongodb:
The code copy is as follows:
1. wget http://downloads.10gen.com/linux/mongodb-linux-x86_64-enterprise-amzn64-2.6.3.tgz
2. tar -xvzf mongodb-linux-x86_64-enterprise-amzn64-2.6.3.tgz
3. sudo cp -R -n mongodb-linux-x86_64-enterprise-amzn64-2.6.3 /usr/local/mongodb
Example introduction:
The author's class formed a football team, and everyone handed over the money to the captain, and the captain paid each fee, recorded the fee and balance of each person and notified everyone. Since not everyone can come every time, the fees can only be distributed equally by the people participating in the event in AA, it is more troublesome to record. So the author created a WeChat official account. Each time, you only need to enter the activity consumption amount and select the number of participants to automatically generate each person's expenses and balances. After that, the details will be sent to the WeChat group and everyone can see it.
In this example, the author actually built a micro-web to record or display activity expenses and balances through a web page. The WeChat public account is equivalent to building a bridge between the user's WeChat and the micro website. When a WeChat user follows the author's public account, he can automatically reply to WeChat users for help through the WeChat public platform developer mode. In the help, it includes operating the corresponding web link, and you only need to click to enter the corresponding page.
Build a WeChat official account backend service:
Everything is done, only development is not available :)
Before you start, let’s briefly introduce the two modules of express and wechat:
express - an excellent web development framework. Using express, you can build your own website very quickly. Since the WeChat server will interact with the developer server through HTTP Post requests, the express framework is required.
Below is the log when new users follow, 103.7.30.84 is the IP address of the WeChat server.
The code copy is as follows:
103.7.30.84 POST /wechat?signature=8a8e408fdae6bbdd6e470af98865a5f993cea283×tamp=1408610461&nonce=1572142586 2 200
wechat - encapsulates the details of interaction with WeChat servers, so developers only need to pay attention to their own business.
First, we need to install express and use express to create the project:
Copy the code as follows: 1. npm install -g express
2. Express -e your_project parameter -e indicates that the ejs engine is used, and the jade engine is used by default without parameters.
3. cd your_project && npm install
The directory structure after installation is as follows:
The code copy is as follows:
[ec2-user@ip-172-31-2-188 your_project]$ ls
app.js bin node_modules package.json public routes views
Next install wechat:
The code copy is as follows:
1. npm install wechat
WeChat developer mode configuration:
Configure URL and token, the example is as follows:
WeChat server access authentication and automatic reply:
Modify app.js, the corresponding code is as follows:
The code copy is as follows:
app.use('/users', users);
app.use('/weixin', weixin);
app.use(express.query()); // Or app.use(express.query());
app.use('/wechat', wechat('hchismylove', function (req, res, next) {
// All input information on WeChat is on req.weixin
var message = req.weixin;
console.log(message);
if((message.MsgType == 'event') && (message.Event == 'subscribe'))
{
var refillStr = "<a href=/"http://your_IP/weixin/refill?weixinId=" + message.FromUserName + "/">1. Click to record team recharge</a>"
var consumptionStr = "<a href=/"http://your_IP/weixin/consume?weixinId=" + message.FromUserName + "/">2. Click to record team consumption</a>"
var deleteStr = "<a href=/"http://your_IP/weixin/delete?weixinId=" + message.FromUserName + "/">3. Click to fall back the record</a>"
var historyStr = "<a href=/"http://your_IP/weixin/history?weixinId=" + message.FromUserName + "/">4. Click to query history</a>"
var emptyStr = " ";
var replyStr = "Thank you for your attention!" + "/n"+ emptyStr + "/n" + refillStr + "/n"+ emptyStr + "/n" + consumeStr
+ "/n"+ emptyStr + "/n" + deleteStr + "/n"+ emptyStr + "/n" + historyStr;
res.reply(replyStr);
}
}));
The following line of code can be used to realize WeChat server access authentication:
The code copy is as follows: app.use('/wechat', wechat('your_token', function (req, res, next) {
The following code implements automatic sending of help when new users follow:
The code copy is as follows:
if((message.MsgType == 'event') && (message.Event == 'subscribe'))
{
....
res.reply(replyStr);
}
The screenshot of WeChat is as follows: