Series directory

(1) The construction of wechat development environment (this paper) (2) How to consume API through wechat public account (3) After wechat users follow the public account, Automatically create customer master data in SAP C4C system (4) How to push SAP C4C master data changes to wechat official account (5) How to embed SAP UI5 application in wechat official account menu (6) How to obtain wechat user information through OAuth2 and display it in SAP UI5 application (7) Use Redis to store the conversation records between wechat users and their official numbers (8) Map integration of wechat official numbers (9) How to save the messages sent by wechat users to the SAP C4C system (10) How to directly reply messages to the subscribers of wechat official numbers in the SAP C4C system — — — — — — — —

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

Recently, many friends asked me on wechat about the integration of SAP system and wechat official account, so I translated the English version into Chinese and re-posted it on my official account.

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

Preconditions for setting up a development environment

(1) Go to the wechat public platform to apply for a wechat public account. (2) The wechat message server in this tutorial is developed with NodeJS, so you need to have a basic understanding of nodeJS development.

Specific environment construction process

Login 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 public account, the wechat app will forward the message to the wechat messaging server to which the wechat public account is bound. After the message server finishes processing, it pushes the processing result back to wechat APP.

In the figure above, my wechat message server is deployed on Heroku cloud platform. Of course, you can also deploy the 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 in the figure above. We must follow a verification process defined in the wechat public account development guide, so as to truly establish a binding relationship between the message server and the wechat public account.

The verification process document is as follows:

After clicking the Submit button in the picture below, the wechat public platform will send an HTTP request to the wechat message server developed by us:

The URL format of an HTTP request is as follows:

https:///?signature=096abd439b41f9610aeabe2d7534084fd8dafa20&echostr=16168327802220428137&timestamp=1512810825&nonce=384 289189

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

If the calculated output value is not equal to the signature value passed in the HTTP request, the authentication fails and the error message is Token authentication failed.

Brief steps for developing wechat message server using NodeJS

(1) Create a nodeJS application with dependencies defined as follows in package.json. Define the command behavior executed by the start script:

node server.js

(2) To implement server.js, the main logic is to receive the HTTP authentication request sent to wechat message server after clicking the Submit button described above, and extract the timestamp (line 17 below) and nonce field (line 19 below) from the request. Calculate a hash value using the SHA-1 hash algorithm (line 29 below) and compare it to the signature value passed in by the HTTP request (line 31). If the two values are equal, the echostr passed in the HTTP request is returned to wechat public platform in line 32, and the verification is successful.

Some SAP practitioners joked that the code examples provided on wechat’s public platform are based on Python, rather than Java or JavaScript, which are more commonly used by SAP’s friends in the ecosystem. If you want to copy the nodeJS source code above, you can find it on Jerry’s SAP community blog.

Finally, deploy the NodeJS application to cloud platforms, such as Tencent Cloud, Ali Cloud, SAP cloud platform, etc.

When I wrote in 2017, I chose the Heroku cloud platform. For details on how to deploy your application, see this 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 of the background console of wechat public account. From the console of Heroku, you can see the log printed with console.log in nodeJS application, where the signature value of the HTTP request is consistent with the hash value calculated in NodeJS application. So it’s verified.

After the verification is passed, is Enabled will be displayed in the server configuration field of the console of wechat public account, indicating that the message server can accept messages sent by users to wechat public account using wechat APP.

In the next article of this series, Jerry will introduce how to receive the messages sent to the wechat official account by users using the wechat APP in the wechat message server, and how to push them back to the wechat app after processing them.

Thanks for reading.

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