Seven series of tutorial for SAP system and WeChat integration: Redis: storage of WeChat users and official account dialogue record

Time:2021-5-5

This is the eighty-eighth article of Jerry in 2020, and the 269th official account of the Wang Zixi public number.

The English version of this series, written by Jerry in 2017, contains a total of ten articles published in the sap community.

Series catalog

(1) Construction of wechat development environment

(2) how to spend API through WeChat official account

(3) WeChat users automatically create the customer master data in the SAP C4C system after they are concerned about the official account.

(4) how to change the SAP C4C master data to WeChat official account.

(5) how to embed SAP UI5 application into WeChat official account menu.

(6) How to get wechat user information through oauth2 and display it in SAP ui5 application

(7) use Redis to store WeChat users and official account dialogues.

(8) map integration of WeChat official account

(9) how to save WeChat users’ messages to WeChat official account to SAP C4C system

(10) how to reply directly to the subscribers of WeChat official account in SAP C4C system

Chinese English friends have official questions on the integration of SAP system and WeChat official account on WeChat. Recently, I translated the English version I wrote to Chinese and reissued it on my official account.

It should be noted that after three years, the development process of WeChat official account may change. Please identify it yourself. The system that integrates with WeChat official account, I chose SAP Cloud for Customer. three years ago.

Friends who run official account know that the conversation between fans and official account can only last 5 days. Therefore, adorable demand is created: persisting the chat record of fans and official account, such as saving to a database, then read it out at any time, and further analysis.

Seven series of tutorial for SAP system and WeChat integration: Redis: storage of WeChat users and official account dialogue record

This article will introduce the dialogue between fans and official account, and save it to Redis (a Key-Value storage system).

Redis is a high performance key value database. The emergence of redis largely compensates for the shortage of key / value storage such as memcached, which can play a very good complementary role to relational database in some occasions. It provides Java, C / C + +, C #, PHP, JavaScript, Perl, Object-C, python, ruby, Erlang and other clients, which is very convenient to use.

Redis supports master-slave synchronization. The data can be synchronized from the master server to any number of slave servers, and the slave server can be the master server associated with other slave servers. This enables redis to perform single-layer tree replication. Save can write data intentionally or unintentionally. Because of the full implementation of publish / subscribe mechanism, when the slave database synchronizes the tree anywhere, it can subscribe to a channel and receive the complete message publishing record of the master server. Synchronization is helpful for scalability and data redundancy of read operations.

First, we will talk with WeChat official account in WeChat app.

Seven series of tutorial for SAP system and WeChat integration: Redis: storage of WeChat users and official account dialogue record

The intelligent reply function of WeChat official account shown in the above figure is a former implementation of this series: two of the series tutorial of SAP system and WeChat integration: how to introduce it through WeChat official account consumption API.

According to the fifth article in this series: how to insert SAP UI5 application into the menu of WeChat official account, add two new menus to official account: Review and Delete.

Seven series of tutorial for SAP system and WeChat integration: Redis: storage of WeChat users and official account dialogue record

The review menu is responsible for reading the chat records stored in redis and displaying them in the wechat app; The delete menu is responsible for clearing the data in redis.

The effect after clicking the review menu is shown in the following figure:

Seven series of tutorial for SAP system and WeChat integration: Redis: storage of WeChat users and official account dialogue record

Explanation of four fields:

  • From: sender of message
  • To: the receiver of the message
  • Question: questions raised by fans to official account
  • Answer: official account calls Turing API, reply returned.

Effect after clicking the delete menu: the chat record is cleared.

Seven series of tutorial for SAP system and WeChat integration: Redis: storage of WeChat users and official account dialogue record

Here are the implementation steps.

Many cloud platforms provide support for redis. The cloud platform I chose for this series is heroku

Open the redis console of heroku, and select the application to enable redis service from the application list: wechatjerry

Seven series of tutorial for SAP system and WeChat integration: Redis: storage of WeChat users and official account dialogue record

Heroku’s default redis plan: Hobby dev is for development purposes only and is completely free of charge.

Seven series of tutorial for SAP system and WeChat integration: Redis: storage of WeChat users and official account dialogue record

After clicking the provision button, in the heroku application console, you can see that redis has appeared in the add ons list of the wechatjerry application.

Seven series of tutorial for SAP system and WeChat integration: Redis: storage of WeChat users and official account dialogue record

Application variable redis_ The URL contains the URL of the currently available redis instance. In our later nodejs code, we will use this URL to access the redis instance.

Seven series of tutorial for SAP system and WeChat integration: Redis: storage of WeChat users and official account dialogue record

Call WeChat API to create two new menus for the official account, Review and Delete, using the following payload:

Seven series of tutorial for SAP system and WeChat integration: Redis: storage of WeChat users and official account dialogue record

Then, in the implementation of wechat message server, before pushing the reply to wechat users through the replymessage function in line 22 of the figure below, insert the log function call of conversionlogservice module in line 21 to store the conversation record in redis.

Seven series of tutorial for SAP system and WeChat integration: Redis: storage of WeChat users and official account dialogue record

The complete code of conversionlogservice implementation is on my GitHub.

It calls an open source redis client of nodejs version:

https://github.com/NodeRedis/…

Seven series of tutorial for SAP system and WeChat integration: Redis: storage of WeChat users and official account dialogue record

Finally, click response function of Review and Delete official account menu is implemented, calling getLog and deleteLog method of conversationLogService respectively, and executing corresponding Redis operation.

Seven series of tutorial for SAP system and WeChat integration: Redis: storage of WeChat users and official account dialogue record

The next article in this series will introduce how to integrate map controls into WeChat official account, thanks for reading.

Series catalog

(1) Construction of wechat development environment

(2) how to spend API through WeChat official account

(3) WeChat users automatically create the customer master data in the SAP C4C system after they are concerned about the official account.

(4) how to change the SAP C4C master data to WeChat official account.

(5) how to embed SAP UI5 application into WeChat official account menu.

(6) How to get wechat user information through oauth2 and display it in SAP ui5 application

(7) use Redis to store WeChat users and official account dialogues.

(8) map integration of WeChat official account

(9) how to save WeChat users’ messages to WeChat official account to SAP C4C system

(10) how to reply directly to the subscribers of WeChat official account in SAP C4C system

More of Jerry’s original articles are: “Wang Zixi”:
Seven series of tutorial for SAP system and WeChat integration: Redis: storage of WeChat users and official account dialogue record