Royjad
Royjad

Reputation: 99

How to save MS bot state data and conversations ?

I am using node js and Microsoft Bot builder sdk to write a BOT. Currently, I am saving everything in data bags (conversationData, userData etc) and using CosmosDb to store the state. I followed this article to configure CosmosDB & made changes as per nodejs. https://azure.microsoft.com/en-in/blog/bot-conversation-history-with-azure-cosmos-db/

There are few concerns with this approach,

  1. conversationData bag is cleared when we call endConversation() in dialog. Thats expected by sdk design but we would like to persist this data for multiple conversation flows with same user (same conversation id.) Now, json cosmosDb in db gets replaced with new keys on conversationData when user start new intent. Ex: schedule a meeting with {name} for {day} at {place}. we save conversationData.name , conversationData.day , and conversationData. place. same user starts over schedule a meeting with {name2} for {day2} at {place2}. documentDb entry gets replaced with conversationData.name1 , conversationData.day2 , and conversationData. place2

Ideally, we would like to keep everything.

Is there a better way to save chat history & conversationData, userData databags in MS BOT ?.

Upvotes: 0

Views: 1259

Answers (1)

Catalyst
Catalyst

Reputation: 3247

All storage implementations just write getData and saveData, they internally have a key:value store where key is typically userId + conversationId, but you can make it whatever you want as long as you can reliably derive it from the arguments passed to getData and setData.

Looking at a sample in redis https://github.com/suttna/botbuilder-redis-storage - https://github.com/suttna/botbuilder-redis-storage/blob/master/src/storage.ts for an example storage implementation that's pretty easy to follow.

You would use a custom implementation like this

// Create new storage with redis client
var storage = new YourStorage()

// this is just here for the sake of initializing a `bot`
var connector = new builder.ChatConnector()
var bot = new builder.UniversalBot(connector)

// Configure bot to use YourStorage
bot.set('storage', storage)

bot.set('persistConversationData', true);

storage is just an object that implements

public saveData(context: IBotStorageContext, data: IBotStorageData, callback?: (err: Error) => void)

public getData(context: IBotStorageContext, callback: (err: Error, data: IBotStorageData) => void)

I totally just copied those signatures from the linked redis module, but they are the same in the BotBuilder source for the default storage - https://github.com/Microsoft/BotBuilder/blob/5cf71c742f27d89e9dbc4076f850122bd6edac11/Node/calling/src/storage/BotStorage.ts

The samples are in typescript. If you are unfamiliar, ignore the bit right after : which indicates the type of a thing.

Upvotes: 1

Related Questions