【发布时间】:2018-05-08 12:32:11
【问题描述】:
机器人信息
- 应用 ID:776ba3b4-38e5-4582-809d-7c8d773cfe9b
- SDK 平台:Node.js
- SDK 版本:
- 有效渠道:直线
- 部署环境:Auzure Bot Service
问题描述
我需要帮助实施 Redis 来保存 Bot 状态。我正在一个项目中工作,该项目确实要求我们尽可能减少延迟。没错,我们使用的是 DocumentDB,但由于 Redis 使用内存,这可能会更快。
我已使用 mongo DB Microsoft Bot framework MongoDB as middle layer to store conversational states, data and context 遵循教程,并且正在编辑文件 /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;
复制步骤
- 下载Microsoft Bot framework MongoDB as middle layer to store conversational states, data and context
- 用我的实现替换
/lib/IStorageClient.js - 设置一个 Redis 账户并输入
/lib/IStorageClient.js - 在机器人模拟器中运行
实际结果
我可以看到 json 保存到 Redis,我也可以在控制台中打印检索结果,但问题是机器人模拟器没有收到答案。
【问题讨论】:
标签: node.js azure redis bots botframework