I'm using MS BotFramework v4. There is a RootDialog
which starts Dialog_A
or Dialog_B
depending on what the user input is.
TL;DR
If a new conversation is started after a conversation and the bot isn't restarted, private variables of dialogs to which a value (which is not the initial value) has already be assigned, are not reseted to their initial value leading to unexpected behavior. How can this be avoided?
Detailed
Let's assume the following scenario:
Each of these dialogs has some private variables to control whether to output a long or a short introduction message. The long one should only be be outputted on the first time this dialog is started. If the conversation again reaches the dialog only the short message should be printed.
Implementation looks like this:
RootDialog.cs
public class RootDialog : ComponentDialog
{
private bool isLongWelcomeText = true;
// Some more private variables follow here
public RootDialog() : base("rootId") {
AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[] {
WelcomeStep,
DoSomethingStep,
FinalStep
});
}
private async Task<DialogTurnContext> WelcomeStep(WaterfallStepContext ctx, CancellationToken token) {
if(isLongWelcomeText) {
await ctx.SendActivityAsync(MessageFactory.Text("A welcome message and some detailed bla bla about the bot"));
isLongWelcomeText = false;
} else {
await ctx.SendActivityAsync(MessageFactory.Text("A short message that hte bot is waiting for input"));
}
}
private async Task<DialogTurnContext> DoSomethingStep(WaterfallStepContext ctx, CancellationToken token) {
// call Dialog_A or Dialog_B depending on the users input
// Dialog X starts
await ctx.BeginDialogAsync("Dialog_X", null, token);
}
private async Task<DialogTurnContext> FinalStep(WaterfallStepContext ctx, CancellationToken token) {
// After dialog X has ended, RootDialog continues here and simply ends
await ctx.EndDialogAsync(null, token);
}
}
Dialog_A
and Dialog_B
are structured the same way.
The problem
If the bot handles its first conversation, everything works as expected (the long welcome text is printed to the user and isLongWelcomeText
is set to false
in WelcomeStep
. When I then start a new conversation (new conversationId and userId) isLongWelcomeText
still is set to false
which leads to the bot outputting the short welcome text in a new conversation to a new user.
In BotFramework v3 dialogs were serialized and deserialized together with all variable values.
If I'm right in BF v4 dialogs aren't serialized anymore.
The question
How can this be fixed? Is there a better way to do this?
Remarks
I'm using UserState
and ConversationState
which are serialized and reset on new conversations. But I don't want to store every private variable value of each dialog in the states. This cannot be the way to go.
Thanks in advace
Copyright Notice:Content Author:「MrToast」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/64801536/dialogs-keep-their-variable-values-in-new-conversation-in-ms-botframework-v4