Skip to Content
Technical Articles

Build a chatbot on SAP Conversational AI which communicates with S/4HANA Cloud

Recast.AI (meanwhile renamed to SAP Conversational AI), a company acquired by SAP, has created a development environment for conversational user experience technology. It uses natural language processing via chatbots and enables SAP to include conversational intelligence into their solutions.

In the meantime, we wanted to enable and test the usage of an intelligent chatbot with SAP S/4HANA Cloud, specifically how a bot on SAP Conversational AI can retrieve data from a S/4HANA Cloud system.

This blog post provides an overview how to build a basic bot on SAP Conversational AI and connect it to a S/4HANA Cloud system using standard APIs. To implement the ‘business logic’ of the bot, SAP Function-as-a-service (beta) is used. Finally, the bot is connected to the third-party messaging platform Telegram.

 

Let’s get started

The use case foresees an SAP S/4HANA Cloud user who wants to provide their customers with a simple solution to gather information on their order’s delivery status. Therefore, we create a simple bot on SAP Conversational AI.

Follow along this tutorial to learn how to create a simple bot and choose a name for your bot, e.g. ‘S/4HANA Cloud Delivery-Bot’. Choose ‘Greetings’ as a predefined skill.

Once your bot is set up and understands greetings, feel free to add additional intents. We have created the following intents for our example (‘@joke’ is of course optional 🙂 ):

We’ve used the following expressions for the intent ‘@get-help’:

(You get the idea. Feel free to add additional expressions.)

For ‘Actions’ we have created the following message:

At this point, our bot should understand a basic greeting, should respond to it and we should be able to ask how it can help us.

The conversation should look something like this:

 

Me: Hi

Bot: Hey. What can I do for you?

Me: What can you do?

Bot: I can retrieve the order status for you. Just say e.g. “What is the order status?”.

 

Now to the most important part and key-skill of our bot. Delivery-status.
Let’s start by creating the intent ‘@delivery-status’.

Create a few expressions such as “What is my order status?” or “Where is my order?”.

Go to the Build tab and click on “+ Create skill” additionally to your already available “greetings”, “get-help” skills (if you have not added “get-help” as a skill, you have to do so).

Choose the “Business”-tag for the “delivery-status”-skill.

Click on the created “delivery-status”-skill and define when the skill is triggered:

Go to “Requirements” and define what the skill requires in order to be completed. We define the order-number (“#number”) as “orderID”.

Click on the triangle-arrow on the right and go to “Edit Replies”.

Under “Edit Replies” we have created the following response:

Next, go to “Actions” and “Add New Message Group” by choosing “Add Condition to trigger messages” (see condition below) and add the message “Let me check the status for you…”.

Now we have to choose “Call Webhook” where we will call the function’s http-trigger. Let’s jump to the SAP Cloud Platform to implement the business logic for our chatbot. We will come back to the SAP Conversational AI-platform later.

Follow this tutorial to set up your SAP Cloud Platform function-as-a-service environment.
Once your Function – Instance was created, click on it.

Choose “Open Dashboard”.

Go to “Create Function”, choose a “Function Name” and click on “Create”.

Use the following code and paste it into an index.js – file in the editor:

module.exports = {
	handler: function (event, context) {		
		console.log('event data: ' + JSON.stringify(event.data));		

		var replyMsg;


		var intent = event.data['intent'];
		console.log('intent detected: ' + intent);

		if (intent == 'delivery-status') {
			var orderNumber = event.data['orderID'] || false;
			
			if (orderNumber) {
				orderNumber = parseInt(orderNumber, 10);
				
				var request = require('sync-request');
				var res = request('GET', 'https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/API_SALES_ORDER_SRV/A_SalesOrder(\'' + orderNumber + '\')?$select=OverallSDProcessStatus&$format=json', {
					headers: {
						'APIKey': '<YOUR API KEY HERE>'
					}
				});

				console.log('SalesOrder service response: ' + res.body.toString('utf8'));
                var response = JSON.parse(res.body.toString('utf8'));     
				
			
				if (response.error) {
					replyMsg = 'I could not find your order, please double check the order number';				
				} else {
				
					orderStatus = response.d.OverallSDProcessStatus; 
					if (orderStatus == 'C') {
						replyMsg = 'Your order was handed over to shipping already.';				
					} else {
						replyMsg = 'Your order is still in processing.';				
					}					
				}
				
				
			} else {
				replyMsg = 'Could not understand your order number, please double check';				
			}	
		} 
		var response = {
			"replies": [{
					"type": "text",
					"content": replyMsg
				}
			]
		};
		console.log('Send reply to user: ' + JSON.stringify(response));
		return response;
	}
}

Now we log on to the SAP API Business Hub with our user, search for “Sales Order” and choose the following API: https://api.sap.com/api/API_SALES_ORDER_SRV/resource

Click on “Show API Key” and copy it.

Paste it into the ‘APIKey’: <‘YOUR API KEY HERE’> – placeholder in our code.

Please note, the above sample coding uses the API Business Hub’s sandbox system. In case you want to connect it to your S/4HANA Cloud tenant, you’d have to create a communication arrangement by following this guide.

Use the following code and paste it into the “Dependencies” – field below the editor:

{
  "dependencies": {
    "sync-request": "6.0.0"
  }
}

Click on “Create Trigger”, choose a name and click on “Create”.

Once you click on “Create” you can see the “Trigger Details” and choose “Ok”.

Copy the “Trigger-URL” (https://YOUR-TENANT.ingress.live.faas-live.shoot.live.k8s-hana.ondemand.com/delivery-bot) and paste it into your SAP Conversational AI account where we have left off.

In order to only send the required data to our function, go to “Body” in edit-mode, choose “Create custom body” and type in the following code:

{
	"intent": "{{nlp.intents[0].slug}}",
	"orderID":"{{memory.orderID.raw}}"
}

Once you have clicked on “Save and Deploy” your chatbot should be able to retrieve the order-status from S/4HANA Cloud.

You can test your chatbot by clicking on the button in the bottom-right-corner.


Congratulations! You have created a working chatbot which can retrieve a delivery status from S/4HANA Cloud! 🙂

 

As a last step you can connect your chatbot to a third-party messaging platform. SAP Conversational AI offers a connection to the following platforms:

Choose the platform you would like to use and follow the set-up guide.

You can find the original chatbot on SAP Conversational AI:
https://cai.tools.sap/christoph-2/delivery-bot/train/intents

Chat with our bot on Telegram:
https://telegram.me/s4hc_delivery_bot

For questions or comments, feel free to reach out to us or leave a comment below. 🙂

Mathias Rup:
Twitter: @der_mathias

Christoph Ruehle:
Twitter: @chrisruehle 
Linkedin: https://www.linkedin.com/in/chrisruehle/

2 Comments
You must be Logged on to comment or reply to a post.