Dialogs keep their variable values in new conversation in MS BotFramework v4
NickName:MrToast Ask DateTime:2020-11-12T17:47:46

Dialogs keep their variable values in new conversation in MS BotFramework v4

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

More about “Dialogs keep their variable values in new conversation in MS BotFramework v4” related questions

Dialogs keep their variable values in new conversation in MS BotFramework v4

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 is...

Show Detail

ResumptionCookie not in MS BotFramework v4

I am porting some code into the newer Microsoft Bot Framework V4 using C#. The original code was a POC in v3 (still c# though). I am trying to copy the authentication aspect whereby the user us

Show Detail

Start conversation with a PromptDialog in MS BotFramework

Is there a proper way for bot to start a conversation with PromptDialog.Choice in the Direct Line channel? I am trying an ugly hack with catching the fist ConversationUpdate activity and creating a

Show Detail

Botframework (V4-preview) updated conversation state values not persisted across conversations

I'm using MS Botframework V4(preview) testing via emulator. I'm using conversation-state to maintain few configurations.In startup.cs I have declared the state as(for local testing) options.

Show Detail

PromptStyler equivalent for BotFramework v4

I'm porting a project from Microsoft BotFramework SDK v3 to v4 and I'm sending prompts like so: var message = Activity.CreateMessageActivity(); var prompt = new PromptStyler(PromptStyle.Auto); prompt.

Show Detail

Microsoft BotFramework v4 Task schedule and States

I'am using the botframework v4 from Microsoft to build a bot. I have implemented a Task with a delay to check if the user has not answered the last 2 hours. If the 2 hours timeout is reached, the

Show Detail

Botframework v4 resume conversation outside of the waterfall dialog flow

Botframework V4 webchat My bot displays a SigninCard to the user, which when clicked user is redirected to an external website where the user will enter login credentials. This external website will

Show Detail

Botframework V4: changing dialog upon sending proactive message

I've been working on a bot build with the V4 botframework SDK and we already have created mulitple dialogs for various purposes. One of our dialogs is used to capture data that is used to create a

Show Detail

Loading Dialog from JSON - BotFramework V4

I was working on Chatbots based on the Microsoft BotFramework of late 2016 to early 2017 and was wondering if there is a new way of creating dialogs from a static source like a JSON with the new

Show Detail

Propagate states to dialogs in MS BotFramework

In Microsoft BotFramework v4 you normally propagate the states (UserState, ConversationState, PrivateConversationState) to a dialog by passing them as parameters to its constructor. This way: Start...

Show Detail