1
votes

Bot Info

  • App ID: 776ba3b4-38e5-4582-809d-7c8d773cfe9b
  • SDK Platform: Node.js
  • SDK Version:
  • Active Channels: Direct Line
  • Deployment Environment: Auzure Bot Service

Issue Description

I Need Help implementing Redis to save Bot State. I'm working in a project that is really a requirement that we reduce as much latency as possibe. Right know we are using DocumentDB but since Redis works with memory this could be faster.

I've followed the tutorial using mongo DB, Microsoft Bot framework MongoDB as middle layer to store conversational states, data and context and I'm editing the file /lib/IStorageClient.js to connect, save and retrieve from redis.

Code Example

This is my implementation of the /lib/IStorageClient.js, instead of using MongoDB connection I've put Redis connection

"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;

Reproduction Steps

  1. Download Microsoft Bot framework MongoDB as middle layer to store conversational states, data and context
  2. Replace /lib/IStorageClient.js with my implementation
  3. Set a Redis account and key in the /lib/IStorageClient.js
  4. Run in the bot emulator

Actual Results

I could see the json saving to Redis, also I could print the retrieve result in the console, but the thing is that the answer is not being received in the bot emulator.

1

1 Answers

0
votes

You are looking for botbuilder-redis-storage middleware, available here:

Usage example:

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)