Using Dialogs and Prompts to build a conversational BOT on Azure

A bot communicates with a user via conversations. To manage conversational flow we use dialogs.

According to Microsoft’s Azure BOT Builder SDK for Node.js Dialogs can contain waterfall steps, and prompts.

They will be able to switch between dialogs according to the requirements during the conversation. To build a meaning full conversational bot we have to use dialogs smartly.

Here is a quick example,


// Create your bot with a function to receive messages from the user
var bot = new builder.UniversalBot(connector);
bot.set('storage', tableStorage);

bot.dialog('/', [
function (session) {
builder.Prompts.text(session, 'Hi! What is your name?');
},
function (session, results) {
session.endDialog(`Hello ${results.response}!`);
}
]);

It would produce the following coversation in result,

dialog1

Some of  things you should know before using dialogs,

  1. To start a new dialog (and push it onto the stack), use session.beginDialog().
  2. To end a dialog (and remove it from the stack, returning control to the calling dialog), use either session.endDialog() or session.endDialogWithResult().
  3. prompt is used whenever a bot needs input from the user like builder.Prompts.text(session, ‘Hi! What is your name?’);

Waterfall model – Using Prompts to add features in the BOT

Adding choices as Buttons


bot.dialog('/', [
function (session) {
session.send("Welcome to the Pizza Place.");
builder.Prompts.choice(session, "What would you like to order today?", "Margherita|Cheese Pizza|Chicken Pizza", { listStyle: builder.ListStyle.button });
},
function (session, results) {
session.dialogData.pizzaOrder= results.response;
builder.Prompts.choice(session, "Which size do you prefer?", "Large|Medium|Small", { listStyle: builder.ListStyle.button });
},
function (session, results) {

session.dialogData.pizzaSize = results.response;
builder.Prompts.text(session, "Who's name will this order be under?");
},
function (session, results) {
session.dialogData.customerName = results.response;
session.send(`Order confirmed. Order details:

Pizza: ${session.dialogData.pizzaOrder}

Pizza size: ${session.dialogData.pizzaSize}

Customer name: ${session.dialogData.customerName}`);
session.endDialog();
}
]);

Choices as buttons would look like below, click them to choose:

dialog2dialog3dialog4

Other Prompt Types:

Prompt type Description
Prompts.text Asks the user to enter a string of text.
Prompts.confirm Asks the user to confirm an action.
Prompts.number Asks the user to enter a number.
Prompts.time Asks the user for a time or date/time.
Prompts.choice Asks the user to choose from a list of options.
Prompts.attachment Asks the user to upload a picture or video.

For more details, visit the official docs here.

Advertisement

One thought on “Using Dialogs and Prompts to build a conversational BOT on Azure

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s