Azure QnA Chat Bot with Waterfall Forms Flow

Qna-Waterfall-2

In my previous articles about Azure Chat bot, I talked about how to create a QnA chat bot using MS QnA maker service and use forms flow for a waterfall like conversation within the chat bot. I found many fellow ‘cloudizens’  looking for a simple solution based on QnA template which starts with a greeting to the user and then continues as a default QnA chat bot, like this one.

Problem

With the default QnA template, you get something like this

[code lang=”js”]bot.dialog(‘/’, basicQnAMakerDialog);[/code]

So, essentially a QnA Chat bot is just an unending sequence of your default dialog, which is expressed as ‘/’. This results in the greeting message also getting displayed again, if you just want to put that first in a waterfall like sequence.

[code lang=”js”]
bot.dialog(‘/’, [
function (session) {
session.beginDialog(‘/welcome’);
},
function (session) {
session.beginDialog(‘/qna’);
}
]);
[/code]

In this case, after the first question is answered by qna dialog, the control will start again from ‘/’, repeating the welcome message.

Qna-Waterfall-1

Solution

The solution is simple though, use persistent storage and save some state information, so that next time the control comes at default dialog, it knows that it’s not the first call and skip the greeting message. Take a look at the below code –

[code lang=”js”]
var bot = new builder.UniversalBot(connector, [
function (session) {
if (!session.privateConversationData.greetingsDone) {
session.send("Welcome to Helpdesk.");
session.privateConversationData.greetingsDone = true;
session.beginDialog(‘QnADialog’);
}
else {
session.beginDialog(‘QnADialog’);
}
}
]);
[/code]

What essentially it is doing is storing a flag in privateConversationData.  This Contains data that is saved for the user within the context of a particular conversation on the specified channel. This data is private to the current user and will persist for the current conversation only. The property is cleared when the conversation ends or when endConversation is called explicitly.

And the QnADialog can be configured similar to how the default one gets configured

[code lang=”js”]bot.dialog(‘QnADialog’, basicQnAMakerDialog);[/code]

You would argue, where is the waterfall in this, it’s just an if-else condition! Well, now that we have made sure that we can call a different dialog without showing the greeting, we can put all waterfall steps in that dialog. Let’s put another dialog in between

[code lang=”js”]
bot.dialog(‘DelveDialog’, [
function (session) {
if (!session.privateConversationData.start) {
builder.Prompts.text(session, ‘Welcome to Delve QnA. Please enter your query’);
session.privateConversationData.start = true;
}
else {
session.beginDialog(‘QnADialog’);
}
},
function (session, results) {
if (session.privateConversationData.start) {
session.beginDialog(‘QnADialog’, results.response);
}
},
]);
[/code]

And modify our default dialog to call this one

[code lang=”js”]
var bot = new builder.UniversalBot(connector, [
function (session) {
if (!session.privateConversationData.greetingsDone) {
session.send("Welcome to Helpdesk.");
session.privateConversationData.greetingsDone = true;
//Calling the intermediate dialog
session.beginDialog(‘DelveDialog’);
}
else {
//If not the first call, go directly to QnADialog
session.beginDialog(‘QnADialog’);
}
}
]);
[/code]

Now, when the bot gets started, it will show the greeting message first and then initiate a new dialog ‘DelveDialog’, which can have multiple waterfall steps to capture user information or anything else you want, as long as the state is captured to ensure those are not repeated in same conversation and then call the final QnA dialog.

Qna-Waterfall-2

Hope this helps.

Enjoy,
Anupam

You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *