Microsoft Bot Framework And Node.JS - Part Three

Building a BookBot using REST API

In my previous articles, we have seen how we can make our Bot more interactive. Based on users’ intent, Bot can perform certain actions and provide whatever information is needed.

Having said, I would like to build a BookBot, now. Below is the typical use-case for our BookBot.

BookBot Use-case,

  • BookBot will first greet the user.
  • BookBot will ask user to enter the topic name by which books can be searched.
  • User will be presented with top 5 book titles based on what user has entered.


  • Our BookBot will prompt user to capture the intent of a user.
  • When user’s intent is of searching a book by entering a topic, BookBot will capture the keyword and call a REST API.
  • REST API will return data to BookBot.
  • BookBot will display the list of books to the end-user, based on the topic that user has entered.

Now, let’s start building this use-case. For this, I will be using Google Books APIs to search the books.

REST API considered,

REST API in Node.JS,

We will call above API in our NodeJS with the help of https module by using,

  1. var https = require('https');  
  2. Our updated botserver.js code implementation can be seen as below,  
  3.     var builder = require('botbuilder');  
  4. var https = require('https');  
  5. var connector = new builder.ConsoleConnector().listen();  
  6. var bot = new builder.UniversalBot(connector);  
  8. function getBooksData(key) {  
  9.     https.get("" + key + "&maxResults=5"function(res) {  
  10.         var d = '';  
  11.         var i;  
  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.             }  
  20.         });  
  21.     });  
  22. }  
  23. var intents = new builder.IntentDialog();  
  24. bot.dialog('/', intents);  
  25. intents.matches(/^Hi/i, [  
  26.     function(session) {  
  27.         builder.Prompts.text(session, 'Hey, I am a BookBot. Welcome to Book Searching through Chat!.To start, which books you would like to search?');  
  28.     },  
  29.     function(session, results) {  
  30.         session.send('Here are books for topic - %s.', results.response);  
  31.         getBooksData(results.response);  
  32.     }  
  33. ]);  
  34. intents.onDefault(builder.DialogAction.send('Hi there! How can I help you today?'));  
Based on user’s intention, BookBot will call Google Books API. Code implementation for this REST API call can be seen in the function getBooksData (). From performance perspective, I have called only top 5 books from the REST API, using maxResults=5.

Running a BookBot

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

Just type ‘Hi’ on command prompt to see how bot prompts user for a name.

BookBot greets and asks user to enter a topic for book search.

Asking BookBot to get the information of top 5 books on the given topic

Now, as a user, I would like to search books by the topic SharePoint. So, I entered ‘SharePoint’ in the command prompt.


Once you hit enter, BookBot will capture the keyword entered and search for the book using Google Books API. As soon as the results for books have arrived, these results will be shown to the user, in below format:


This way, we can make our bots intelligent to search the information from the cloud, and can provide the great conversational experience to end users.

Similar Articles