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.

Series directory

(1) The construction of WeChat development environment (2) How to consume API through WeChat official account (this paper) (3) After WeChat users pay attention to the official account, Create customer master data automatically in SAP C4C system (4) How to push changes of SAP C4C master data to WeChat public account (5) How to embed SAP UI5 application into the menu of WeChat public account (6) How to obtain WeChat user information through OAuth2 and display it in SAP UI5 application (7) How to save messages sent by WeChat users to WeChat official accounts to SAP C4C system (10) How to reply messages directly to WeChat official account subscribers in SAP C4C system

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.

Keep in mind that the goal of this series is to learn how to use WeChat public account to integrate with SAP system (SAP Cloud for Customer, hereinafter referred to as C4C). To achieve this goal, you have to consume SAP C4C OData API through WeChat public account.

As the first step of learning, we first learned how to call the common API in WeChat public account, instead of SAP C4C OData API.

Suppose such a simple scenario: after users pay attention to the WeChat official account, they send some text messages to the official account through the WeChat APP. The public account receives these text messages and forwards them to the WeChat message server we mentioned in the setup of the WeChat development environment in the first article of this series. At this time, after the WeChat message server receives the forwarded text information, it calls some API with artificial intelligence and replies the API response to the user through WeChat APP.

Binding WeChat message server WeChat public number, in this scene is playing the role of a simple chat robot. Users and WeChat public account through a question-and-answer way to interact.

When Jerry was in college, he took his Unix Network Programming course. One of the exercises was to implement a pair of clients and servers on a socket. The client sends a string to the Server, and the Server receives it and sends it back intact, the so-called Echo Server.

Before we start implementing the WeChat chatbot, let’s make it easy and implement a simple WeChat Echo service where whenever a user sends any message to an official account, they receive the message itself, prefixed with “Add by Jerry”.

The project used in this article complete source code, in my Github can be downloaded.

First, in Server.js, start a Web server using the NodeJS Express library:

The implementation of the RoutesEngine in the second line of the image is located in index.js. When the WeChat app receives the text message sent by the user through the WeChat app, it will forward it to the WeChat message server through an HTTP POST request. When the WeChat message server receives the request, it calls line 5 for EchoService processing:

The implementation of the EchoService is in the file echo. Js. It first parses the text information entered by the user from the HTTP request contained in the variable req (line 11) and then adds “Add by Jerry: “, store the result in the variable content (line 13), and finally call the utility method you developed, replyMessage, to return the prefixed text message to the user.

The RepleMessage utility method in line 15 of the figure above needs to parse the WeChat user ID that sent the message from the HTTP POST request forwarded by the WeChat app to the WeChat message server, and then send the content with the spliced “Add by Jerry: “prefix to the WeChat user identified by this ID.

The implementation of replyMessage is in replyMessage.js:

With the Echo Service implementation experience, implementing WeChat chatbots is not difficult.

Just change the action in index.js after the HTTP POST request is received from calling EchoService to calling the new implementation, TuningService.

The implementation source code for the TuningService function that calls the Turing API is available for download here.

The Turing API I used in 2017 is provided by the following website:

Apply for an API key and paste it into the URL in line 6 below:

The text information input by the user is parsed and stored in the Body variable on line 20. After processing with EncodeURI, it is directly spliced into the tail of Turing API URL. Then, on line 27, the request function is used to call the Turing API. The response from the API is obtained in the anonymous callback function, and finally the utility method ReplyMessage is called to send the response to the WeChat app via the WeChat message server.

In the next article in this series, we will really start the integration with the SAP system: whenever a new WeChat user follows a WeChat public account, we will automatically create a customer master data for that WeChat user in the SAP C4C system.

Thanks for reading.

Other articles in this series

(1) The construction of WeChat development environment (2) How to consume API through WeChat official account (this paper) (3) After WeChat users pay attention to the official account, Create customer master data automatically in SAP C4C system (4) How to push changes of SAP C4C master data to WeChat public account (5) How to embed SAP UI5 application into the menu of WeChat public account (6) How to obtain WeChat user information through OAuth2 and display it in SAP UI5 application (7) How to save messages sent by WeChat users to WeChat official accounts to SAP C4C system (10) How to reply messages directly to WeChat official account subscribers in SAP C4C system

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