My previous article wechat program development series tutorial (a) development environment construction introduced the construction of the wechat development environment, this article we will step by step to develop some specific functions.

Function Requirements: When a wechat user follows your official account, you use JavaScript to send a welcome message to this fan.

The specific implementation

After we log in the console of wechat official account, click Development -> Basic configuration:

You can see the address of the wechat message server we configured. In my first tutorial, we developed a Web server using NodeJS locally, and then deployed it to your favorite cloud platform, such as Tencent Cloud, Ali Cloud, Baidu Cloud, etc. (I chose cloud platform Heroku), and then maintained the URL of the deployed application to the server configuration of wechat official account console, as shown in the picture below. After maintenance, wechat users pay attention to the public account or send messages to the public account, will be delivered to your NodeJS application through wechat platform, where we can program to achieve some requirements. This NodeJS application is referred to as a “message server” below.

We first get an app object using nodeJS express Module:

var express = require('express');

var app = express();
Copy the code

When a wechat user follows your public account, the wechat platform will send an HTTP POST request to your message server. You need to respond programmatically to this POST request.

app.route('/').post(function(req,res){
	var content;
	// Store the content of the HTTP POST sent by wechat platform into the variable content
	req.on("data".function(data){
		content = data.toString("utf-8"); }); req.on("end".function(){
		console.log("new http post: " + content );
		// Prints HTTP POST requests for debugging
Copy the code

// Parse the event object from the HTTP request sent by wechat platform. If a fan clicks, the event type is SUBSCRIBE.

var msgType = formattedValue(getXMLNodeValue('MsgType', content));
// A fan clicked the follow button
if( event === "subscribe") {// Reply a welcome message to your followers
	var replyxml = replyMessage(content, "Welcome, welcome at last!"); res.send(replyxml); }}Copy the code

The above code logic is very clear, read the comments are easy to understand. The key is how to reply the welcome message to the fans who click the follow button.

The core logic lies in the replyMessage function, which is tasked with parsing the fan’s openID from the HTTP POST content sent by wechat to the messaging server. The code is as follows:

Input parameter 1: all HTTP posts sent by wechat to the message server

Input parameter 2: Welcome message to be pushed to fans

Output parameter: wechat messages prepared to return welcome messages to fans through HTTP, which must meet the message specifications defined by wechat, as shown in the code below.

module.exports = function(originalBody, contentToReply){
	// Extract the receiver of the message from the original message
	var ToUserName = getXMLNodeValue('ToUserName', originalBody);
	// Extract the sender of the message from the original message
	var FromUserName = getXMLNodeValue('FromUserName',originalBody);
	var CreateTime = getXMLNodeValue('CreateTime',originalBody);
	// Tell the wechat platform that the message type is text message
	var MsgType = "
      ";
	// Prepare to add the text content of the welcome message to the message
	var Content = contentToReply;
	// Start assembling messages to send to wechat fans
	var xml = '<xml><ToUserName>'+FromUserName+'</ToUserName><FromUserName>'+ToUserName+'</FromUserName><CreateTime>'+CreateTime+'</CreateTime><MsgType>'
	+ MsgType + '</MsgType><Content>'+Content+'</Content></xml>';
	console.log("xml to be sent: " + xml);
	// Prints message packets
	return xml;
	// Return a message packet};Copy the code

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