This is Jerry’s 88th article in 2020, and the 269th original article on wang Zixi’s official account.

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.

Series directory

(1) Establishment of wechat development environment

(2) How to consume API through wechat public account

(3) After wechat users follow the public account, they will automatically create customer master data in SAP C4C system

(4) How to push changes of SAP C4C master data to wechat official account

(5) How to embed SAP UI5 application in wechat official 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 official accounts (this article)

(8) Map integration of wechat public accounts

(9) How to save the messages sent by wechat users to wechat official accounts in SAP C4C system

(10) How to reply messages directly to subscribers of wechat public accounts in SAP C4C system

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.

Friends who have run their own public accounts know that the dialogue between fans and public accounts can only be maintained for 5 days. Therefore, there is a demand: the fans and the public chat record persistence, such as saved to a database, can be read out at any time later, for further analysis.

This article will introduce how to save the conversations between fans and public accounts to Redis(a key-value storage system).

Redis is a high performance key-value database. Redis largely compensates for memcached key/value storage, and in some cases complements relational databases. It provides Java, C/C++, C#, PHP, JavaScript, Perl, object-C, Python, Ruby, Erlang and other clients, easy to use.

Redis supports master/slave synchronization. Data can be synchronized from the primary server to any number of secondary servers, which can be the primary server associated with other secondary servers. This allows Redis to perform single-layer tree replication. Data can be written intentionally or unintentionally. The fully implemented publish/subscribe mechanism makes it possible to subscribe to a channel and receive a complete record of message releases from the master server when the tree is synchronized anywhere from the database. Synchronization helps with scalability and data redundancy for read operations.

First of all, talk with the wechat official account in the wechat app:

The above figure shows the intelligent reply function of wechat public account, and its implementation method was introduced in the previous article of this series: SAP system and wechat integration series tutorial 2: How to use wechat public account consumption API.

Add two new menus to the wechat official account: Review and Delete, as described in the fifth article of this series: How to embed SAP UI5 application in wechat official account menu.

The Review menu is responsible for reading the chat records stored in Redis and displaying them in wechat app. The Delete menu is responsible for clearing data in Redis.

Click the Review menu and the result is as shown below:

Explanation of four fields:

  • From: indicates the sender of the message
  • To: Indicates the receiver of the message
  • Question: Question raised by fans to the official account
  • Answer: The public account called Turing API and returned the reply

Click the Delete menu: Chat history is cleared.

Here are the implementation steps.

There are many cloud platforms that provide support for Redis, and the one I chose for this series is Heroku.

Open HeroKu’s Redis console and select the app you want to enable the Redis service from the list of apps: wechatJerry.

Heroku’s default Redis Plan: Hobby Dev is for development use only and is completely free.

After you click the Provision button, in the Console of the Heroku app, you can see that Redis has been added to wechatJerry’s Addons list.

The application variable REDIS_URL contains the url of the currently available Redis instance, which will be used to access the Redis instance in our future nodeJS code.

Call the wechat API and create two new menus for the public account, Review and Delete, and use the following payload:

Then in the wechat message server implementation, insert the log function call of the conversationLogService module in line 21 to store the conversation record in Redis before pushing the reply to the wechat user via the replyMessage function in line 22 below.

The complete code for the implementation of conversationLogService is available on Github.

Nodejs Redis client:

Github.com/NodeRedis/n…

Finally, implement the click response functions for Review and Delete public account menus, and call the getLog and deleteLog methods of conversationLogService respectively to perform the corresponding Redis operations.

The next article in this series will introduce how to integrate map controls into wechat official accounts. Thank you for reading.

Series directory

(1) Establishment of wechat development environment

(2) How to consume API through wechat public account

(3) After wechat users follow the public account, they will automatically create customer master data in SAP C4C system

(4) How to push changes of SAP C4C master data to wechat official account

(5) How to embed SAP UI5 application in wechat official 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 official accounts (this article)

(8) Map integration of wechat public accounts

(9) How to save the messages sent by wechat users to wechat official accounts in SAP C4C system

(10) How to reply messages directly to subscribers of wechat public accounts in SAP C4C system

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