Technical Articles
Demystifying Node.js for Building SAP Conversational AI Chatbots (Part 2)
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!
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:
“app.post” just needs the name used in the bot’s webhook to know which bot it’s talking with.
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:
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.
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):
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).
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.
The app selects a text response at random from a list:
And that’s it. I hope you found this blog post helpful. If you have any questions or comments, please leave them below.
For more information about SAP Conversational AI:
- Visit our web page
- Create your account on our platform
- Engage with our SAP Community
- Read our blogs, follow our tutorials and earn your badge on openSAP
- Ask your questions on SAP Answers
- Read our product documentation
- What our users say
- Follow us on LinkedIn, Twitter, and YouTube