Wechat development series 7 – use Redis to store Wechat conversation history
- Wechat development series 1 – setup your development environment
- Wechat development series 2 – development Q&A service using nodejs
- Wechat development series 3 – Trigger C4C Account creation in Wechat app
- Wechat development series 4 – Send C4C Data change notification to Wechat app
- Wechat development series 5 – embedded your UI5 application to Wechat app
- Wechat development series 6 – Retrieve Wechat User info via oAuth2 and display it in UI5 application
- Wechat development series 7 – use Redis to store Wechat conversation history
The complete source code used in this series could be found from my github repository.
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:
Implemented feature
Implementation detail
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.
2. follow the steps in blog Wechat development series 5 – embedded your UI5 application to Wechat app to create menu and two submenu.
{ "button":[ { "name":"UI5", "sub_button":[{ "type": "view", "name": "Jerry List", "url": "https://wechatjerry.herokuapp.com/ui5" },{ "type": "click", "name": "Other UI5 application", "key": "dataQuery" }] }, { "name":"Conversation", "sub_button":[{ "type": "click", "name": "Review", "key": "review" },{ "type": "click", "name": "Delete", "key": "delete" }] } ] }
Date.prototype.today = function () { return ((this.getDate() < 10)?"0":"") + this.getDate() +"/"+(((this.getMonth()+1) < 10)?"0":"") + (this.getMonth()+1) +"/"+ this.getFullYear(); } Date.prototype.timeNow = function () { return ((this.getHours() < 10)?"0":"") + this.getHours() +":"+ ((this.getMinutes() < 10)?"0":"") + this.getMinutes() +":"+ ((this.getSeconds() < 10)?"0":"") + this.getSeconds(); } function logConversation(wholeContent, question, answer){ var fromUserId = formattedValue(getXMLNodeValue('FromUserName', wholeContent)); var toUserId = formattedValue(getXMLNodeValue('ToUserName', wholeContent)); var fromUserName = config.userMap[fromUserId] || fromUserId; var toUserName = config.userMap[toUserId] || toUserId; var datetime = "Send Time: " + new Date().today() + " " + new Date().timeNow(); redisClient.insert(toUserId, objectToString(fromUserName, toUserName, datetime, question, answer)); }; function objectToString(fromUserName, toUserName, datetime, question, answer){ var record = { "from": fromUserName, "to": toUserName, "sendTime": datetime, "question": question, "answer": answer }; return JSON.stringify(record); } function getList(sToUserOpenId){ return redisClient.getList(sToUserOpenId); } function deleteLog(sToUserOpenId){ return redisClient.clearList(sToUserOpenId); } var oService = { log: logConversation, getLog: getList, deleteLog: deleteLog } module.exports = oService;
var redis = require("redis"), client = redis.createClient(process.env.REDIS_URL || "redis://h:p99a8dd0d92871b9ffe7a026e6d70beecd7f2a0e743fa1e2840a58ce048f41c4a@ec2-34-237-158-248.compute-1.amazonaws.com:9479"); // by default localhost will be used!! client.on("error", function (err) { console.log("Trouble......... Redis startup failed: " + err); }); function insertIntoList(sOpenId, oElement){ client.lpush(sOpenId, oElement); } function clearList(sOpenId){ return new Promise(function(resolve,reject){ client.del(sOpenId, function(error, count){ if(error){ console.log("error when clear list:" + error); reject(error); } var reply = "list clear successfully"; console.log(reply); resolve(reply); }); }); } function getListContent(sOpenId){ return new Promise(function(resolve,reject){ client.lrange(sOpenId, 0, -1, function(err, reply) { console.log("content for list: " + sOpenId + " **********: " + reply + "***"); var content = reply; if( content == ""){ content = "no conversation log found."; console.log("reject content: " + content); reject(content); } else { resolve(formatToWechat(content)); } }); }); } function formatToWechat(raw){ var formatted = "[" + raw + "]"; var result = ""; var logs = JSON.parse(formatted); for( var i = 0; i < logs.length; i++){ var record = "record[" + i + "]:" + " [[from]] " + logs[i].from + " [[to]] " + logs[i].to + " [[sendTime]] " + logs[i].sendTime + " [[question]] " + logs[i].question + " [[answer]] " + logs[i].answer; if( i === 0){ result = record; } else{ result = result + " " + " " + record; } } return result; } var oRedisClient = { insert: insertIntoList, clearList: clearList, getList: getListContent }; module.exports = oRedisClient;
Wechat development series 7 – use Redis to store Wechat conversation history