My previous article ** wechat program development series tutorial (I) ** Development environment setup has introduced the development environment setup of wechat server. As the second article in the development series, this article introduces how to develop a simple QUESTION and answer service for your wechat subscription account, which is very fun.

The full code for this series is on my Github:

Github.com/i042416/wec…

The scenario implemented in this article

Followers of your subscription number who send a message to the subscription number will receive the same text reply as they sent the message, prefixed with “Add by Jerry:”.

It’s not interesting, is it? Since the wechat message server of this subscription number is developed by ourselves, we can implement some interesting logic after receiving the text sent by fans on the message server. For example, call some OF the Turing API of artificial intelligence and chat with fans of subscribing numbers like the following:

Here are the steps.

1. Open my github project server.js file:

Implement the following code. This server.js is the entry point to the entire message server, and routesEngine is designed to handle different requests sent by wechat to the message server. Of course, the implementation is in the index.js folder jerryapp/routes.

var express = require('express');

var routesEngine = require('./jerryapp/routes/index.js');

var app = express();

routesEngine(app);

app.listen(process.env.PORT || 3000.function () {

console.log('Listening on port, process.cwd(): ' + process.cwd() );

});
Copy the code

2. Now let’s implement index.js. When a fan’s text request is sent to the message server, it calls its own nodeJS Module “echoService” for processing.

var request = require('request');

var echoService = require(".. /service/echo.js");

module.exports = function (app) {

app.route('/').post(function(req,res){

echoService(req, res);

});

};
Copy the code

Open echo.js to see the implementation:

You can see in line 6 below that the prefix “Add by Jerry:” was added to the reply.

Turing services with artificial intelligence

If you don’t want to reply to your subscription followers with a dull “Add by Jerry:” comment out the ecoService in index.js and use the Turing API instead. This is shown in Figure 29 below.

The call implementation of Turing API is located in tuned.js, which actually consumes an ARTIFICIAL intelligence service exposed through the RestFul API:

The url is as follows:

www.tuling123.com/openapi/api…

We only need to paste the text sent by the fans of the subscription number in the wechat App to the end of the above URL, and call the API to get the Turing API analysis response result. Take the Weather forecast for Chengdu as an example:

Visit www.tuling123.com to learn more about the Turing robot.

For more of Jerry’s original technical articles, please follow the public account “Wang Zixi” or scan the following QR code: