Microsoft Bot Framework And Node.JS - Part Four

In this article, we will learn how to make our BookBot using Node.js and REST APIs, for providing additional information, like book descriptions and price for the book.

In this article, we will learn how to make our BookBot, using Node.js and REST APIs for providing additional information, like book descriptions and price for the book.

Use Case: Users always like to have more information, like the description and price for a selected book.

In my previous article for BookBot, we saw how we can get the list of books based on the topic entered in a chat. Now, I would like to explore the description and cost for a selected book.

Approach

  • Assuming our BookBot is showing a list of books to the end user.
  • While showing book titles, required additional information will be stored in an array.
  • User will ask for additional info.
  • BookBot will present five options to choose from, for which the user is willing to see the description and price.
  • User will select an option.
  • BookBot will display title, description and price for the selected book from the array.

Now, let’s start building this use case. For this, I will be starting with my earlier botserver.js, as explained in article three in the BookBot Series.

  1. Microsoft Bot Framework And Node.JS - Part One
  2. Microsoft Bot Framework And Node.JS - Part Two
  3. Microsoft Bot Framework And Node.JS - Part Three

REST API Output Structure.

For additional information, like description and pricing information, you should first know how the API is sending us the result in a JSON format. Refer to the below JSON which has been returned by the above REST API:

API

We will be capturing our data from the sections highlighted in red, and will add it to our array. This array will be used later to retrieve the required information.

Our updated botserver.js code implementation can be seen below,

  1. var builder = require('botbuilder');  
  2. var https = require('https');  
  3. var connector = new builder.ConsoleConnector().listen();  
  4. var bot = new builder.UniversalBot(connector);  
  5. var arr = [];  
  6.   
  7. function getBooksData(key) {  
  8.     https.get("https://www.googleapis.com/books/v1/volumes?q=" + key + "&maxResults=5"function(res) {  
  9.         var d = '';  
  10.         var i;  
  11.         arr = [];  
  12.         res.on('data'function(chunk) {  
  13.             d += chunk;  
  14.         });  
  15.         res.on('end'function() {  
  16.             var e = JSON.parse(d);  
  17.             for (i = 0; i < e.items.length; i++) {  
  18.                 console.log(i + 1 + ":" + e.items[i].volumeInfo.title);  
  19.                 arr.push({  
  20.                     "description": e.items[i].volumeInfo.description,  
  21.                     "title": e.items[i].volumeInfo.title,  
  22.                     "saleability": e.items[i].saleInfo.saleability,  
  23.                     "price": e.items[i].saleInfo.listPrice  
  24.                 });  
  25.             }  
  26.         });  
  27.     });  
  28. }  
  29. var intents = new builder.IntentDialog();  
  30. bot.dialog('/', intents);  
  31. intents.matches(/^Hi/i, [  
  32.     function(session) {  
  33.         builder.Prompts.text(session, 'Hey, I am a BookBot. Welcome to Book Searching through Chat!.To start, which books you would like to search?');  
  34.     },  
  35.     function(session, results) {  
  36.         session.send('Here are books for topic - %s.', results.response);  
  37.         var b = [];  
  38.         getBooksData(results.response);  
  39.     },  
  40.     function(session) {  
  41.         builder.Prompts.text(session, 'which books');  
  42.     }  
  43. ]);  
  44. intents.matches(/^info?/i, [  
  45.     function(session) {  
  46.         builder.Prompts.choice(session, "Which book's info you need?""1|2|3|4|5");  
  47.     },  
  48.     function(session, results) {  
  49.         var book = arr[results.response.entity - 1];  
  50.         if (book.saleability == 'FOR_SALE') {  
  51.             session.send('Title:' + book.title + " Price:" + book.price.amount + " " + book.price.currencyCode);  
  52.         } else {  
  53.             session.send('Title:' + book.title + " Price: NOT FOR SALE");  
  54.         }  
  55.         session.send('Description:' + book.description);  
  56.     }  
  57. ]);  
  58. intents.onDefault(builder.DialogAction.send('Hi there! How can I help you today?'));  
Please refer to the updated getBooksData() function from the above code. We are now adding additional information into an array, in a JSON format.

Now, let’s run our BookBot.

Running a BookBot

Open a new Node.js command prompt and navigate to our bots folder, using command cd and run below command. Our botserver.js has run successfully and our conversation has begun with our bot through the command line channel.

User has entered ‘SharePoint’ on a command prompt.

command

Books are listed, as shown below:

command

Now, our updated flow starts here. As an end user, I would like to know further details about Book 1. So, I am entering ‘Info’ as my intent. Once you enter ‘Info’ on command prompt and hit enter, the BookBot will present 5 options to choose from.

command

To show user prompts with choice options, the following intent has been used in our code implementation. When user enters ‘Info’, bot throws choices to choose from.
  1. intents.matches(/^info?/i, [  
  2.     function(session) {  
  3.         builder.Prompts.choice(session, "Which book's info you need?""1|2|3|4|5");  
  4.     },  
  5.     function(session, results) {  
  6.         var book = arr[results.response.entity - 1];  
  7.         if (book.saleability == 'FOR_SALE') {  
  8.             session.send('Title:' + book.title + " Price:" + book.price.amount + " " + book.price.currencyCode);  
  9.         } else {  
  10.             session.send('Title:' + book.title " Price: NOT FOR SALE");  
  11.         }  
  12.         session.send('Description:' + book.description);  
  13.     }  
  14. ]);  
Now, user enters the number, 1, to retrieve additional information about Book No. 1 and hits enter. The following information will be shown to the end user.

command

So, for the end user, the book’s additional information, like description and pricing, will be displayed, based on the choice entered by the user.

To see another book’s information, user will again enter ‘Info’ and the other choice, as shown below:
 
command