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,

Approach

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);
  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. res.on('data', function(chunk) {
  12. d += chunk;
  13. });
  14. res.on('end', function() {
  15. var e = JSON.parse(d);
  16. for (i = 0; i < e.items.length; i++) {
  17. console.log(i + 1 + ":" + e.items[i].volumeInfo.title);
  18. }
  19. });
  20. });
  21. }
  22. var intents = new builder.IntentDialog();
  23. bot.dialog('/', intents);
  24. intents.matches(/^Hi/i, [
  25. function(session) {
  26. builder.Prompts.text(session, 'Hey, I am a BookBot. Welcome to Book Searching through Chat!.To start, which books you would like to search?');
  27. },
  28. function(session, results) {
  29. session.send('Here are books for topic - %s.', results.response);
  30. getBooksData(results.response);
  31. }
  32. ]);
  33. 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.
command

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.

command

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:

command

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.