Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
former_member606634
Participant
In this blog post, we will learn how to quickly connect an SAP Conversational AI chatbot with a local NodeJS or Springboot service for development purposes.

This blog post is 3rd part of a series with the following parts:

 

So let's get started!

We will follow the following steps:

  1. Create a service in Node.js that can accept post request with cake order details.

  2. Run the service locally.

  3. Expose the local service.

  4. Connect the cake-bot to our service

  5. Place an order for a cake ?


 

What we will need:

  1. Node.js (download here)

  2. Ngrok (download here)


Download and install Node.js.

Download and extract Ngrok executable in any directory.

 

Step 1: Create a service in Node.js


Let's create a small NodeJS service that takes order data from the chatbot and does further processing.

Open your favorite text editor and create a file with name app.js

File: app.js
const express = require("express");
const app = express();
let bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.listen(4000, () => console.log("Server listening on port 4000!"));

app.get("/", (req, res) => res.send("Hello World!"));

app.post('/process_cake_order',function(req,res){
console.log("Order details:");
console.log("Flavor: " + req.body.flavor);
console.log("Weight: " + req.body.weight);
console.log("Message: " + req.body.message);
// Do the processing here
var response = {
"replies": [
{
"type": "text",
"content": "Order placed!"
}
],
"conversation": {
"language": "en",
"memory": {
}
}
};
res.json(response);
});

The code above creates a simple service that can accept post request on /process_cake_order.

The response format is specified in the SAP Conversational AI Docs.

 

Step 2: Run the service locally.


Before running, we need to install a Node.js web application framework: Express


Open terminal and go to the directory where the app.js file is located and enter the following command:
npm install express --save

Now that we have all the required packages, let's run the service.
node .\app.js

If all goes well, you should see the "Server listening on port 4000!" message on your terminal.

 

Step 3: Expose the local service.


Now we need to expose this service so that our chatbot can connect to it.

Open terminal and go to the directory where ngrok executable was extracted. Then run the following command:
./ngrok http 4000

If all goes well, you should see something like this in the terminal:


We can now use the URL's generated here to access our service from anywhere.
Please note: Exposing the service using ngrok should be used only for learning and development purposes.

 

Step 4: Connect the cake-bot to our service


To connect this to the chatbot, we will navigate to the Actions tab.

In part 2, we added a reply where the bot sends the order details.

We will call our service webhook with the order details in the JSON format.
{
"flavor": "{{memory.flavor.value}}",
"weight": "{{memory.weight.value}}",
"message": "{{memory.message.value}}"
}


 

We are all set now. The cake-bot can now send the order details to our service where more processing can take place.

Let us test the bot. Click on the button Chat with you bot on the bottom right corner.


 

Here is the console log from the node service:


 

The reply Order placed! is sent from the backend service.

Hooray! Our bot is finally ready to receive cake orders!

Happy bot building! ?

Cheers.
1 Comment