天天看點

微信開發系列之七 - 使用Redis存儲微信聊天記錄

In the second blog Wechat development series 2 – development Q&A service using nodejs of this series, we have developed a kind of Q&A service in Wechat which leverages a free Tuning Restful service so that you could chat with this service:

微信開發系列之七 - 使用Redis存儲微信聊天記錄
微信開發系列之七 - 使用Redis存儲微信聊天記錄

n this blog, I will show the steps how to store all of those conversation logs via Redis, so that you can review the conversations within Wechat app or delete them if necessary.

Implemented feature

Two new sub menus “Review” and “Delete” are developed under menu “Conversation”:

微信開發系列之七 - 使用Redis存儲微信聊天記錄
微信開發系列之七 - 使用Redis存儲微信聊天記錄

Once Review menu is pressed, the conversation log will be read out from Redis and displayed in Wechat app:

微信開發系列之七 - 使用Redis存儲微信聊天記錄
微信開發系列之七 - 使用Redis存儲微信聊天記錄

Delete menu will trigger the deletion of the conversation log belonging to current user who has pressed it. Once conversation is deleted, corresponding notification will be sent out if user has pressed the Review menu again.

微信開發系列之七 - 使用Redis存儲微信聊天記錄
微信開發系列之七 - 使用Redis存儲微信聊天記錄

Implementation detail

(1) Since as before my WeChat server runs on Heroku, which supports Redis as well.

Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with radius queries.

In this blog I will use Redis to store conversation logs.

It is very easy in Heroku to enable an application with Redis support, just select your application from drop down list and press Continue:

微信開發系列之七 - 使用Redis存儲微信聊天記錄

he default plan “Hobby Dev” is completely for free and enough for this prototype development.

微信開發系列之七 - 使用Redis存儲微信聊天記錄

Once done, in you application dashboard you can see the Redis as addon:

微信開發系列之七 - 使用Redis存儲微信聊天記錄

and the actual url for this Redis instance is automatically set in application variable which could be used in your program

微信開發系列之七 - 使用Redis存儲微信聊天記錄

ou don’t need any additional configuration on Redis, which is now ready for development.

(2) follow the steps in blog Wechat development series 5 – embedded your UI5 application to Wechat app to create menu and two submenu.

It is enough to create menu via postman. You’d better first remove the old menu done in previous blog and recreate a new menu from scratch:

微信開發系列之七 - 使用Redis存儲微信聊天記錄

he source code of HTTP post payload for the new menu creation:

微信開發系列之七 - 使用Redis存儲微信聊天記錄

3) Now every time when Tuning service receives a query from end user and is ready to send the answer of this query, add logic to record this conversation detail.

Just add one line for logging in tuning.js:

微信開發系列之七 - 使用Redis存儲微信聊天記錄

The conversationLogService is implemented in conversationLogService.js, which simply delegates the logging call to Redis service wrapper module.

微信開發系列之七 - 使用Redis存儲微信聊天記錄

Redis service wrapper module is implemented in file redisClient.js.

This wrapper module is built on top of open source Redis module for nodejs, whose source code could be found from github:

https://github.com/NodeRedis/node_redis
微信開發系列之七 - 使用Redis存儲微信聊天記錄
微信開發系列之七 - 使用Redis存儲微信聊天記錄
微信開發系列之七 - 使用Redis存儲微信聊天記錄

4) implement event handling logic when menu “review” and “delete” are pressed.

Corresponding API provided by Redis are called to read log records from Redis or just clear the list where the log records are stored.

微信開發系列之七 - 使用Redis存儲微信聊天記錄

繼續閱讀