Microsoft Bot Framework And Node.JS - Part Two

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.
  1. Modify our file botserver.js, using the below code, under bots folder:
    1. var builder = require('botbuilder');  
    2. var connector = new builder.ConsoleConnector().listen();  
    3. var bot = new builder.UniversalBot(connector);  
    4. bot.dialog('/', [  
    5.     function(session)  
    6.     {  
    7.         builder.Prompts.text(session, 'Hi there! What is your name?');  
    8.     },  
    9.     function(session, results) {  
    10.         session.send('Hello %s! How are you today?', results.response);  
    11.     }  
    12. ]);  

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:

  1. bot.dialog('/', [  
  2. function(session){},function (session, results){}  
  3. ]);  
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:

command

Provide a name and hit enter to see how bot greets the user with his/her name in it.

command

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.

 

  1. Again, Modify your file botserver.js, using below code under bots folder:
    1. var builder = require('botbuilder');  
    2. var connector = new builder.ConsoleConnector().listen();  
    3. var bot = new builder.UniversalBot(connector);  
    4. var intents = new builder.IntentDialog();  
    5. bot.dialog('/', intents);  
    6. intents.matches(/^Hi/i, [  
    7.     function(session)  
    8.     {  
    9.         builder.Prompts.text(session, 'Hi there! How are you today?');  
    10.     },  
    11.     function(session, results)  
    12.     {  
    13.         session.send('%s! How can I help you?', results.response);  
    14.     }  
    15. ]);  
    16. intents.matches(/^need version/i, builder.DialogAction.send('The Latest Bot version is 1.1'));  
    17. 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,

command

If user says ‘Hello’ then bot interacts in a different way, as below,

command

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:

command

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.

Up Next
    Ebook Download
    View all
    Learn
    View all