Search code examples
typescriptdialogbotframeworkstate-management

dialogContext.activeDialog always undefined when using waterfall dialog


I don't know why the activeDialog field of the dialogContext is always undefined. I need to use it to see if the user is in the middle of a waterfall dialog. Here is what my bot code looks like (in typescript):

export class MyBot{
    constructor(){
        this.dialogState = this.conversationState.createProperty("dialog-state");
        this.dialogs = new DialogSet(this.dialogState);
        this.dialogs.add(new ChoicePrompt("choice-prompt"));

        const steps = [
            step => step.prompt("choice-prompt", "What browser are you currently using?", ["!", "1"]),
            step => step.prompt("choice-prompt", "And what device are you using?", "!", "1")
        ];

        this.dialogs.add(new WaterfallDialog("something", steps));
    }

    public async onTurn(context: TurnContext) {
        const dc = await this.dialogs.createContext(context);
        console.log(dc.activeDialog); // always logs undefined
        return dc.beginDialog("something");
    }
}

Solution

  • You need to save the state of the conversation at the end of the turn otherwise the dialog state will start anew on every turn. Try adding this to the (or near the) end of your onTurn method:

    this.conversationState.saveChanges(context);
    

    Also, I would point out that there's no point in return the result of the dc.beginDialog call from the onTurn method because onTurn is technically not expected to return any value.