Start building your own chatbot now!

If you aren’t a developer, but you were able to make it through Demystifying Node.js for SAP Conversational AI Bot Builders, Part ll promises to be no more technical. If a non-programmer like me can learn something about Node.js, so can you!nodejs_for_dwight_large

In Part 1, we looked at what Node.js is and how SAP Conversational AI connects to a Node.js application. In this blog post, we’ll see how a Node.js application can search for information, based on a conversation in an SAP Conversational AI chatbot. Like Part 1, Part 2 takes a high-level glance at the process and leaves how-to details to other SAP Conversational AI tutorials.

First we’ll look at a couple of tasks that most Node.js applications do when working with SAP Conversational AI chatbots:

1. Connect to SAP Conversational AI

2. Access the conversation

Then, continuing with the Movie Bot tutorial example, we’ll see how the movie bot’s Node.js application does the following:

3. Get the search terms using the entities found in the conversation

4. Run the search

  • Get the movie genre ID
  • Connect to the movie database
  • Send the movie genre ID and other search criteria

5. Send the answer to the SAP Conversational AI chatbot

  • Receive and send the movie recommendation
  • Send a text response along with the movie recommendation

Note: To keep it simple, instead of showing full lines of code, the code samples here show just enough to indicate what the Node.js application is doing.

1. Connect to SAP Conversational AI

The Node.js app needs to connect to the SAP Conversational AI chatbot to get the entities and their values that SAP Conversational AI found in the conversation with the user. The app can use them to submit a search to the movie database. In this example from the Movie Bot, SAP Conversational AI finds the entities #RECORDING, #GENRE, #LANGUAGE, and #INTERVAL from a conversation with the user:entities-conversation

Now that it knows where to find the Node.js app, SAP Conversational AI can send it information and request information, as though the app is a web server. The app listens for any requests from SAP Conversational AI by using the statement “app.post.”

“app.post” just needs the name used in the bot’s webhook to know which bot it’s talking with.app.post

2. Access the conversation

To get the information it needs, the app must access the conversation that’s stored in the SAP Conversational AI memory. It uses this line:

In the variable “conversation”, the app now has the entities and their values in a format that it can read and crawl through.test

3. Get the search terms using the entities found in the conversation

The entities in the conversation contain all the information that the Node.js app needs to send a search request to the movie database: “movie”, “Western”, “English”, and “2010-2018“. The following lines of code get each entity and its value:

  • This line gets the “recording“ entity, which contains “movie”:
    conversation.memory[‘movie’]
  • This line gets the “genre” entity, which contains “Western”:
    conversation.memory[‘genre’]
  • This line gets the “language” entity, which contains “English”:
    conversation.memory[‘language’]
  • This line gets the “interval” entity, which contains “2010-2018”:
    conversation.memory[‘interval’]

4. Run the search

Now that the app has the search terms “movie”, “western”, “English”, and “2010-2018”, it’s ready to send a search request to the movie database.

4.1. Get the movie genre ID

In the movie database, all movie genres are organized by ID number. Before sending the search request, the app needs the genre ID for Westerns.

The app uses this line to search through a local table of movie genres and their IDs to find and store the genre ID number for Westerns (“genre” shown here contains the word “Western”):

4.2 Connect to the movie database

The Node.js app now has the genre ID (37) and is ready to connect to the movie database.

The first step is to put the word “movie” into a variable named “kind” (because that’s what the movie database looks for):kind-movie

With “kind” set to contain the word “movie”, the app connects to the movie database:

movie-db

4.3 Send the movie genre ID and other search criteria

The app sends the genre ID to the movie database, the language (English), and the interval (movies made between 2010-2018).movie-api

*In the actual code, the variables genreId, language, and interval have been renamed but contain the same values represented here.

5. Send the answer to the SAP Conversational AI bot

Finally, the Node.js app receives the information from the movie database and sends it to the bot.

5.1 Receive and send the movie recommendation

Inside the variable, ‘movie’ is the movie recommendation received from the movie database. This line sends the movie recommendation to the bot in a carousel format.api-results

5.2 Send a text response along with the movie recommendation

The app selects a text response at random from a list:text-response-chatbot-movie

And then it sends the text response along with the movie recommendation to the bot.

And that’s it. I hope you found this blog post helpful. If you have any questions or comments, please leave them below.

There are currently no comments.

This site uses Akismet to reduce spam. Learn how your comment data is processed.