[State Service] How to save bot chat history ?
Hello,
I am using node js sdk to write ms bot service. Currently, I am saving everything in data bags (conversationData, userData etc) and using documentDb to store the state. I have also implemented middleware logging to intercept user to Bot and bot to user messages and logging them on console. There are few concerns with this approach,
- conversationData is cleared when we call endConversation() in dialog. Thats expected by design but we would like to persist this data for multiple conversation flows with same user (same conversation id.) Now, json document 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.
- Issue 2 is middleware logging is not able to hold session object so I can't save each event.text to any databag? Is this correct approach or there is better way to save chat history ?
const logUserConversation = (event) => { console.log(' ******** BOT/User Message: ' + event.text); //Want to save event.text somewhere ?? };
//Middleware for logging bot.use({ receive: function (event, next) { logUserConversation(event); next(); }, send: function (event, next) { logUserConversation(event); next(); } });
any help would me much appreciated. Thanks in advance.
Also, I am not sure why ConversationData data bag is cleared even though I set to true as follows for persistence.
bot.set('persistConversationData', true);
If you want to data that will persist for the lifetime of a user rather than a single conversation, you can save to userData rather than conversationData.
As for overwriting the data - the data bags are essentially just key-value pair dictionaries. If you store data with the same key, you're going to overwrite previous records, so you need to either use different keys each time, or store some sort of data structure such as a list which would let you persist multiple values within one key/value pair.
Thanks Sam. If I continue to use userData bag instead conversation do you think it will work for multiple users ?Does this bag gets created per user ? In documentDb I could only see a entry 'default-user:userData'
Also, any thoughts/ideas on storing chat history ?
Yes, userData is per-user. There's three data bags with different uses: userData is per-user and will persist/share across multiple conversations, while conversationData is per-conversation, and in a group conversation, all users will share the same conversationData dataBag. There's also privateConversationData, which is per-conversation but also per-user (i.e. it doesn't persist/share beyond the lifetime of the conversation, but each user within a group conversation will have their own privateConversationData).
I'll have a look back over your initial question again when I've got more time later on, and see if I have any other advice about storing chat history. Hope that helps in the meantime, though!
Appreciate your help Sam.
Currently, I'm taking this approach where I create chatData array in main app.js file, keep filling it will text from bot/user, pass it to schedule.js so Calendar.Add dialog and user to store in userData.
//App.js code
//Middleware for logging(RJ-db,logging) bot.use({ receive: function (event, next) { logUserConversation(event); next(); }, send: function (event, next) { logUserConversation(event); next(); } });
var chatData = []
const logUserConversation = (event) => { chatData.push(event.text) console.log('******** BOT/User Message: ' + event.text) //+ ', ***** user: ' + event.address.user.name); };
require('./app/dialogs/schedule.js')(bot,chatData);
// schedule.js code
bot.dialog('Calendar.Add',...... function (session) { session.userData.chatData = [] session.userData.chatData.push(chatData) }
But, with this chatData is available for other user to so when I look at session.userData.chatData for user 2, it has chat history of user 1 + its own.
We have a great blog article about creating your own custom state service here: https://blog.botframework.com/2017/07/21/saving-state-azure-nodejs/
Thank you.
I have gone through the document provided above https://blog.botframework.com/2017/07/21/saving-state-azure-nodejs/ but again it's a same which I had referred earlier. Issues I posted in my thread above still persist.
Just setting DocumentDb isn't helpful as data bags are cleared and also document does not talk able storing chat history.
@rjgmail88 Which channel are you using? "when I look at session.userData.chatData for user 2, it has chat history of user 1 + its own" <-- user 1 and user 2 must have the same userid for this to occur. How are you initiating the chat?
I'm using Cortana channel but this is happing in bot emulator testing. here is code I have
//in server.js
var chatData = [] const logUserConversation = (event) => { chatData.push(event.text) // This will push every message in chatData array. console.log('******** BOT/User Message: ' + event.text) };
//Middleware for logging bot.use({ receive: function (event, next) { logUserConversation(event); next(); }, send: function (event, next) { logUserConversation(event); next(); } });
// In schedule.js I simply do session.userData.chatData.push(chatData) so it will be logged in cosmosDb
Now if I use 2 clients 1) Cortana and 2) MS teams with 2 different users then when I see session.userData.chatData I see both conversations.
There might be better approach to store chat history. I'am trying to find one.
I'm facing the same issue.
Is there any way I could use "session.userData.chatData =[]" instead of "var chatData = []"
@rjgmail88 Based on the code you've shown here, I see why all data is being persisted to every user's .userData. Every message that comes into your bot is being pushed onto chatData. Please review https://blog.botframework.com/2017/07/21/saving-state-azure-nodejs/ as well as https://github.com/Microsoft/BotBuilder-Samples/tree/master/Node/core-CustomState
Add the necessary keys to your .env file. Place var azure = require('botbuilder-azure'); at the top of the .js file. Then, the following:
// Azure DocumentDb State Store
var docDbClient = new azure.DocumentDbClient({
host: process.env.DOCUMENT_DB_HOST,
masterKey: process.env.DOCUMENT_DB_MASTER_KEY,
database: process.env.DOCUMENT_DB_DATABASE,
collection: process.env.DOCUMENT_DB_COLLECTION
});
var botStorage = new azure.AzureBotStorage({ gzipData: false }, docDbClient);
// Set Custom Store
bot.set('storage', botStorage);
// Enable Conversation Data persistence
bot.set('persistConversationData', true);
This will enable logging of incoming and outgoing messages into the documentdb storage.
Hey EricDahlvang, I am already doing what you have mentioned above. When I mentioned every user has same copy of chatData I meant it's in DocumentDb. Sorry if I wasn't clear.
This code will cause every message to be persisted to every user's chatData:
var chatData = [] const logUserConversation = (event) => { chatData.push(event.text) // This will push every message in chatData array. console.log('******** BOT/User Message: ' + event.text) };
//Middleware for logging bot.use({ receive: function (event, next) { logUserConversation(event); next(); }, send: function (event, next) { logUserConversation(event); next(); } });
// In schedule.js I simply do session.userData.chatData.push(chatData) so it will be logged in cosmosDb
When doing session.userData.chatData.push(chatData), you'll need to make sure that you are only adding messages from or to this user.
Hi rjgmail88,
Found a workaround for this using dynamic variables.
In server.js
var chatHistory= [] const logUserConversation = (event) => { chatHistory[session.address.conversation.id]) += (event.text); console.log('******** BOT/User Message: ' + event.text); };
//Middleware for logging bot.use({ receive: function (event, next) { logUserConversation(event); next(); }, send: function (event, next) { logUserConversation(event); next(); }
In dialogs
use chatHistory[session.message.address.conversation.id]
Hi lakmals, thanks for sharing this. I wonder how are you using [* ] in dialogs. At what step ?
- chatHistory[session.message.address.conversation.id]
Your question is not clear. I'm using chatHistory[session.message.address.conversation.id] to trigger a mail with nodemailer.
ex: bot.dialog('DialogTest', [ function (session) { session.send(chatHistory[session.message.address.conversation.id]); } ]);
I would like to know the steps, on how to save this string in a database (like Azure Storage).
If you see in one of my comments I'm doing following
// in app.js var chatData = []
const logUserConversation = (event) => { chatData.push(event.text) console.log('******** BOT/User Message: ' + event.text) //+ ', ***** user: ' + event.address.user.name); };
// in sever.js bot.dialog('Calendar.Add',...... function (session) { session.userData.chatData = [] session.userData.chatData.push(chatData) }
so that how conversation is available in datBag session.userData.chatData. So I was wondering how it's happening in your case for chatHistory. OR I should say how does it look like in document Db.
@rjgmail88 Are you still experiencing this problem, or have you found a resolution?