Search code examples
node.jsbotframework

How do I handle "cancel" intent when already in a dialog?


In Microsoft Bot Framework, I already started a conversation and have some intent running, let's say "login", but then while I ask the user for username or password, he might say "cancel that" or "cancel login", how do I get this intent: "cancel" and how do I handle it by cancelling out of the current dialog?

// Login Dialog
bot.dialog('login', [
    (session:Builder.Session) => {
        Builder.Prompts.text(session, 'What\'s your account email?');
    },
    (session:Builder.Session, results: any) => {
        session.dialogData.email = results.response;
        Builder.Prompts.text(session, 'What\'s your password?');
    },
    (session:Builder.Session, results: any) => {
        session.dialogData.passWord = results.response;
        CheckAccountLogin(session, session.dialogData.email,session.dialogData.passWord)
            .then((result:boolean)=>{
                if(result === true){
                    session.send('Login Successful');
                    session.userData.loginSuccessful = true;
                    session.userData.email = session.dialogData.email;
                }else{
                    session.userData.loginSuccessful = false;
                }
                session.endDialog();
            });
    },
]);

Solution

  • Another way to do it is to use the built in cancel event. Luis would still work. This is based on Javier's answer:

    // Setup Luis Recognizer first:
    const LuisModelUrl = 'https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/' + LuisID + '?subscription-key=' + LuisKey;
    var recognizer = new Builder.LuisRecognizer(LuisModelUrl);
    bot.recognizer(recognizer);
    
    // Dialog definition
    bot.dialog('login', [
        (session:Builder.Session) => {
            // Your dialog stuff here
        }
    ]).cancelAction(
        "Cancel", "What would you like to do next?",
        {
            matches: "Cancel",
            confirmPrompt: "This will cancel your dialog.  Are you ure?"
        }
    );