Once a session.userData is set,it persists even if I delete the conversation data. I tried "New conversation", "Delete User Data", Refresh Button, Reconnecting to the endpoint URL but nothing worked. Only restarting the emulator seems to do the trick. Also, this is an anomalous behavior and doesn't happen always. Am I missing some trick here?
Hi @PadmanavAgarwal,
The "Delete User Data" menu item doesn't do what it suggests. What it actually does is send an activity to your bot of type deleteUserData. It is up to your bot to handle this message in a way that makes sense to your application. The emulator's UI doesn't make that clear, but we'll improve this in the next release.
Now, when you go to implement a handler for the deleteUserData activity type, you will discover that the code necessary seems more complex than it ought to be. The designers of the Node SDK are aware of this and looking at ways to make it simpler. I'll paste an implementation that works today at the bottom of this message.
The C# SDK has a built-in command called /deleteprofile that will clear UserData and PrivateConversationData. Alas, the Node SDK doesn't have this, but in the code below I've included a Node implementation. The SDK designers are working together to iron out a more consistent story on this and other aspects of the two SDKs.
Here is some code to implement handling of the deleteUserData activity, and also implements a global /deleteprofile command (just for fun :))
// Clears userData and privateConversationData, then ends the conversation
function deleteProfile(session) {
session.userData = {};
session.privateConversationData = {};
session.endConversation("User profile deleted");
}
// Handle activities of type 'deleteUserData'
bot.on('deleteUserData', (message) => {
// In order to delete any state, we need a session object, so start a dialog
bot.beginDialog(message.address, '/deleteprofile');
});
// A dialog just for deleting state
bot.dialog('/deleteprofile', function(session) {
// Ok, now we have a session so we can delete the state
deleteProfile(session);
});
// Creates a middleware to handle the /deleteprofile command
function deleteProfileMiddleware() {
return {
botbuilder: (session, next) => {
if (/^\/deleteprofile$/i.test(session.message.text)) {
deleteProfile(session);
} else {
next();
}
}
};
}
// Install middleware
bot.use(deleteProfileMiddleware());
Finally, while investigating this issue I discovered a bug in the emulator: It turns out that session.userData state is getting shared between all bots! It should be per-user, per-bot. We'll get new emulator out this week with a fix for this.
Thank you for taking the time to report this!
Thanks for the detailed explanation. The provided fix seems to be working well to refresh user data.
Eagerly awaiting the session.userData state sharing bug fix !
A new emulator build is out with a fix for this (v3.5.25). If this doesn't address the issues you're seeing, please drop a comment here and we'll reopen.
If I don't use some deleteProfile middleware, the session.userData still persists across conversations.
For example, if I set the userData during a conversation like below,
session.userData.name = "Some-Name"
And then, check session.userData.name at the beginning of a "new conversation", it still has that value!
So, the "New conversation" probably refers to a new conversation with the same user (without refreshing the userData) and the custom delete profile middleware is the only way to reset the user data?
I confirm this issue is disturbing for new node bot SDK users
This is the correct behavior for session.userData. It should persist across conversations. You might try using session.privateConversationData instead.
The "new conversation" behavior is ok but the "deleteUserData" emulator button is not. The Emulator UI is misleading about what the expected behavior is.
If I see a button named "deleteUserData" build in the Emulator, I expect this to be a built in feature in the server/framework. Otherwise make it explicit that clicking on the button won't do anything by default unless I implement some code, or just remove the button and rely on users typing commands such as /deleteUserData and implementing ad hoc handlers.
@remydavid, I can see how deleteUserData can also sound like a built-in command. What actually happens is the emulator will send an activity to your bot of type deleteUserData. It is up to your bot to handle this activity type and delete user data in a way that makes sense for your application.
The different activity types are enumerated here:
https://docs.botframework.com/en-us/csharp/builder/sdkreference/activities.html
Is there an example of how to handle these activities in NodeJS?
@bondz Sample code to do this in NodeJS appears earlier in this thread. Have you tried it?
https://github.com/Microsoft/BotFramework-Emulator/issues/76#issuecomment-274636337
Oh. Replied via email. Seen it now, thanks.
I am facing the same issue. Any solution?