The English version of this series was written by Jerry in 2017. This tutorial is a total of ten articles and is published on the SAP community.

Recently, many friends asked me about the integration of SAP system and WeChat official account on WeChat, so I translated the English version I wrote at that time into Chinese and re-published it on my official account.

It should be noted that after three years, the development process of WeChat public number may change, please identify by yourself. As for the system integrated with WeChat official account, I chose SAP Cloud for Customer three years ago.

Preconditions for setting up the development environment

(1) Go to the WeChat public platform to apply for a WeChat official account. (2) The WeChat message server in this tutorial is developed with NodeJS, so it is necessary to have a basic understanding of NodeJS development.

Specific environment construction process

Login to the WeChat public platform, enter the public account management console: Development->Basic Configuration

In the URL field below, maintain your WeChat message server address. When we use the WeChat app to send a message to a WeChat official account, the WeChat app will forward the message to the WeChat message server to which the WeChat official account is bound. The message server will push the processing result back to WeChat APP after processing.

In the figure above, my WeChat message server is deployed on Heroku cloud platform. Of course, you can also deploy your WeChat message server developed with NodeJS on other cloud platforms, such as Ali cloud, Tencent cloud, SAP cloud platform, etc.

It is not enough to simply maintain the URL of the WeChat message server on the cloud platform in the URL field above. We must follow a verification process defined in the WeChat public account development guide, so that the message server can truly establish a binding relationship with WeChat public account.

The verification process document is as follows:

After clicking the Submit button in the figure below, the WeChat public platform will send an HTTP request to the WeChat message server we developed:

The format of the HTTP request URL is as follows:

https://

/?signature=096abd439b41f9610aeabe2d7534084fd8dafa20&echostr=16168327802220428137&timestamp=1512810825&nonce=3842891 89

After receiving this request, our WeChat message server will take the account token of WeChat public account, the timestamp and the nonce field contained in the HTTP request as input, and calculate an output value according to the instructions in the WeChat development guide. If the output value is equal to the signature value passed in by the HTTP request, Then the WeChat message server passes verification and can receive subsequent user requests from WeChat APP.

If the calculated output value is not equal to the signature value passed in by the HTTP request, authentication failed with error message: Token Authentication Failed.

Brief steps for developing a WeChat message server using NodeJS:

(1) Create a new NodeJS application. The dependencies defined in package.json are as follows. Define the behavior of the command executed by the start script:

node server.js

(2) To implement Server.js, the main logic is to receive the HTTP validation request sent to the WeChat message server after clicking the Submit button described above, and extract the timestamp (line 17 below) and the nonce field (line 19 below) from the request. A hash value is calculated using the SHA-1 hashing algorithm (line 29 of the figure below) and then compared to the signature value passed in by the HTTP request (line 31). If the two values are equal, return the echostr from the HTTP request to the WeChat public platform on line 32, and the validation passes.

In the past, some SAP practitioners have joked that the code examples presented on the WeChat public platform are based on Python, rather than Java or JavaScript, which is more commonly used by our friends in the SAP ecosystem. If you want to copy the NodeJS source code shown above, you can find it on Jerry’s SAP Community Blog.

Finally, the NodeJS application is deployed to cloud platforms, such as Tencent cloud, Ali cloud, SAP cloud platform and so on.

When I wrote this in 2017, I chose the Heroku cloud platform. For detailed steps to deploy your application, see my article:

Step by step to host your UI5 application in Heroku

After the message server is deployed to the cloud platform, click the Submit button in the background console of the WeChat public account. From the Heroku console, you can see the log printed with console.log in the NodeJS application. The incoming HTTP request signature value is the same as the hash value calculated in the NodeJS application. So it’s validated.

After verification, the IS enabled prompt can be seen in the server configuration field of the console of WeChat public account, indicating that the message server has been able to accept messages sent by users to WeChat public account using WeChat APP.

In the next article of this series, Jerry will show you how to receive messages from a WeChat app to a WeChat official account in a WeChat messaging server, and then push them back to the WeChat app.

Thanks for reading.

More of Jerry’s original articles can be found on “Wang Zixi “: