Build your own WeChat official account based on Nodejs

Source: Internet
Author: User

Build your own public account based on Nodejs
Abstract:

The huge user base and strong user stickiness have 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 backend systems. This article describes how to develop your own public account based on Nodejs using an example developed by the author. In this instance, we mainly use express, wechat, mongodb, monk and other modules.


Preparations:

1. Apply for public number, go to https://mp.weixin.qq.com/application, here do not elaborate too much.

2. purchase servers. Amazon EC2 is recommended here. You can select micro instance for the first time. It is free for one year. It is very convenient to apply. You only need to enter your credit card information, but the entire process is full of English.
The year is free, and it is worthwhile to spend more time.


Install the NodeJs development environment:
1. yum-y install gcc2. yum-y install gcc-c ++ 3. yum-y install make automake4. wget http://nodejs.org/dist/v0.10.29/node-v0.10.29.tar.gz5. tar-xvzf node-v0.10.29.tar.gz 6. cd decompress directory 7 .. /configure 8. make9. make install

Install Mongodb:
1. wget http://downloads.10gen.com/linux/mongodb-linux-x86_64-enterprise-amzn64-2.6.3.tgz2. 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: I have a football team in my class. The Team Leader pays the money and records the fee and balance of each person. Because not everyone can come every time, but the cost can only be shared by the participants in the activity in AA mode, it is difficult to record. Therefore, the author made a public number, each time you only need to enter the activity consumption amount and select the number of participants, You can automatically generate the cost and balance of each person, and then send the details to the group, everyone can see it.
In this example, I actually set up a micro-website to record or display the Activity Cost and balance through Web pages. The public account is equivalent to setting up a bridge between the user and the micro-website. When the user pays attention to the author's public account, the user can automatically reply to the user through the public platform developer mode. The Help page contains the corresponding web link operations. You only need to click to enter the corresponding page.
If you are interested, please pay attention to "AA assistant", no.: aazhushou
Building the public account backend service: everything is ready, and development is not enough :)
Before you start, we will briefly introduce the express and wechat modules: express-excellent Web development framework. Using express, you can quickly build your own website. Because the server uses an HTTP Post request to interact with the developer server, the express framework is required.
The following is the log generated when a new user is interested. 103.7.30.84 is the IP address of the server.
103.7.30.84 POST /wechat?signature=8a8e408fdae6bbdd6e470af98865a5f993cea283&timestamp=1408610461&nonce=1572142586 2 200

Wechat-encapsulates the details of interaction with the server, so that developers only need to pay attention to their own business.
First, we need to install express and use express to create a project:
1. The npm install-g express2. 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:
[ec2-user@ip-172-31-2-188 your_project]$ lsapp.js  bin  node_modules  package.json  public  routes  views


Next, install wechat:
1. npm install wechat


Developer mode configuration: configure the URL and token. For example, after the server configuration (Enabled) is disabled, modify the configuration, disable the server configuration, push user messages and events required by developers, will not be forwarded to this URL URLHttp: // your_IP/wechat TokenYour_token


Server access authentication and automatic reply:

Modify app. js with the following code:

 

App. use ('/', routes); 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) {// the input information is in req. var message = req on weixin. weixin; console. log (message); if (message. msgType = 'event') & (message. event = 'subscribe') {var refillStr = "1. click record team recharge "var consumeStr =" 2. click to record team consumption "var deleteS Tr = "3. Click the rollback record" var historyStr = "4. Click query history" 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 );}}));


Use the following code to implement server access authentication:

app.use('/wechat', wechat('your_token', function (req, res, next) {

The following code automatically sends help when a new user pays attention to it:

if((message.MsgType == 'event') && (message.Event == 'subscribe')){....res.reply(replyStr);}

 


As follows:








Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.