在Node.js Bot服务中实现Redis

博特信息

  • 应用ID:776ba3b4-38e5-4582-809d-7c8d773cfe9b
  • SDK平台:Node.js
  • SDK版本:
  • 活跃渠道:直线
  • 部署环境:Auzure Bot服务

问题描述

我需要帮助实施Redis来保存Bot状态。 我正在一个项目中工作,这个项目的确需要我们尽可能减less延迟。 知道我们正在使用DocumentDB,但是由于Redis和内存一起工作,这可能会更快。

我已经按照教程使用mongo DB, Microsoft Bot框架MongoDB作为中间层来存储会话状态,数据和上下文,并且正在编辑文件/lib/IStorageClient.js来连接,保存和从redis中检索。

代码示例

这是我的/lib/IStorageClient.js实现,而不是使用MongoDB连接我已经把Redis的连接

 "use strict"; var Consts = require('./Consts'); var redis = require('redis'); var IStorageClient = (function () { function IStorageClient(options) { this.options = options; } IStorageClient.prototype.initialize = function (callback) { var _this = this; var host = "MyRedis.redis.cache.windows.net"; var auth = "KEY"; var client = redis.createClient(6380,host , {auth_pass: auth, tls: {servername: host}}); this.client = client; callback(null); }; IStorageClient.prototype.insertOrReplace = function (partitionKey, rowKey, entity, isCompressed, callback) { console.log("=========Insert IStorageClient===========") var docDbEntity = { id: partitionKey + ',' + rowKey, data: entity, isCompressed: isCompressed }; var host = "MyRedis.redis.cache.windows.net"; var auth = "KEY"; var client = redis.createClient(6380,host , {auth_pass: auth, tls: {servername: host}}); client.set(partitionKey + ',' + rowKey, JSON.stringify(docDbEntity), function(err, reply) { console.log("=========SET==========="); console.log("ID: ",partitionKey + ',' + rowKey); console.log("Result: ",docDbEntity); }); }; IStorageClient.prototype.retrieve = function (partitionKey, rowKey, callback) { console.log("=========Retrieve IStorageClient===========") var id = partitionKey + ',' + rowKey; var host = "MyRedis.redis.cache.windows.net"; var auth = "KEY"; var client = redis.createClient(6380,host , {auth_pass: auth, tls: {servername: host}}); //id client.get(id, function(error, result){ console.log("=========Get==========="); console.log("Search: ",id); console.log("Result: ",result); if (error) { console.log("Error:",error) callback(error, null, null); } else if (result == null) { callback(null, null, null); } else if (result.length == 0) { callback(null, null, null); } else { var finaldoc = JSON.parse(result); callback(null, finaldoc, null); } }); }; IStorageClient.getError = function (error) { if (!error) return null; return new Error('Error Code: ' + error.code + ' Error Body: ' + error.body); }; return IStorageClient; }()); exports.IStorageClient = IStorageClient; 

复制步骤

  1. 下载Microsoft Bot框架MongoDB作为中间层来存储会话状态,数据和上下文
  2. 用我的实现replace/lib/IStorageClient.js
  3. 设置一个Redis帐户并键入/lib/IStorageClient.js
  4. 在机器人模拟器中运行

实际结果

我可以看到JSON保存到Redis,我也可以在控制台中打印检索结果,但问题是在仿真程序中没有收到答案。

您正在寻找botbuilder-redis-storage中间件,请点击这里:

用法示例:

 var redis = require('redis') var RedisStorage = require('botbuilder-redis-storage') var builder = require('botbuilder') // Initialize redis client var redisClient = redis.createClient(process.env.REDIS_URL, { prefix: 'bot-storage:' }); // Create new storage with redis client var storage = new RedisStorage(redisClient) var connector = new builder.ChatConnector() var bot = new builder.UniversalBot(connector) // Configure bot to use the RedisStorage bot.set('storage', storage)