Before reading this article, I highly recommend reading my previous article:
Building a very basic interactive Bot
I would like my bot to ask the name of the user who is interacting with this bot. Once the user communicates their user name, bot will actually greet them with the name the user has provided.
- Modify our file botserver.js, using the below code, under bots folder:
- var builder = require('botbuilder');
- var connector = new builder.ConsoleConnector().listen();
- var bot = new builder.UniversalBot(connector);
- bot.dialog('/', [
- function(session)
- {
- builder.Prompts.text(session, 'Hi there! What is your name?');
- },
- function(session, results) {
- session.send('Hello %s! How are you today?', results.response);
- }
- ]);
In the above code, we are using Prompts. Bot builder comes with many built-in prompts to collect user input. Here, we are collecting user’s name and then, we are making our conversation more interactive.
Just noticed the change at bot.dialog function? This is now accepting an array of functions, as below:
- bot.dialog('/', [
- function(session){},function (session, results){}
- ]);
This will help us to carry out the conversation in a waterfall mode, where response of one function will be passed to another function.
Now, we are going back to our code at bot.dialog - In the first function, bot is prompting a user to input a string, using Prompt.text function here. Once the user enters the string, it is used by the next function to greet the user.
Let’s run our updated botserver.js file and see how our conversation is made interactive.
Running an updated Bot
Open a new Node.js command prompt and navigate to your bots folder, using command cd and running the below written command. Our botserver.js has run successfully and our conversation has begun with our bot, through this command line channel.
Just type ‘Hi’ on command prompt to see how bot prompts user for a name, as shown below:
Provide a name and hit enter to see how bot greets the user with his/her name in it.
Asking bot to do something – knowing User Intents
These are very basic examples. Let’s complicate it more. Now, I would like to ask some questions to the bot and then, bot should do something and respond accordingly.
Now, to do this, bot framework provides us IntentDialog class. This allows us to know what the user’s intent is, from the conversation. This can be done using regular expressions and by the use of IntentDialog class.
- Again, Modify your file botserver.js, using below code under bots folder:
- var builder = require('botbuilder');
- var connector = new builder.ConsoleConnector().listen();
- var bot = new builder.UniversalBot(connector);
- var intents = new builder.IntentDialog();
- bot.dialog('/', intents);
- intents.matches(/^Hi/i, [
- function(session)
- {
- builder.Prompts.text(session, 'Hi there! How are you today?');
- },
- function(session, results)
- {
- session.send('%s! How can I help you?', results.response);
- }
- ]);
- intents.matches(/^need version/i, builder.DialogAction.send('The Latest Bot version is 1.1'));
- intents.onDefault(builder.DialogAction.send('Hi there! How can I help you today?'));
In our code above, we are actually looking at what the user is saying and then, the bot is prompting. E.g. if user says ‘Hi’, then bot prompts as,
If user says ‘Hello’ then bot interacts in a different way, as below,
Above response is as intent default …'Hi there! How can I help you today?'. Meaning, when patterns are not matched, above message will be sent by bot.
Now, let’s ask a question to bot -- I need to know the version of this bot. So, ask ‘need version’. With this, user’s intent is matched with ‘need version’ and bot will reply with latest version, as shown below:
This way, we can make our bots interactive and provide end users great conversational experience. In our next article, we will see some intelligence built into conversations with our bot.