Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 

Introduction


In this blog post, I will show how to include business partner information in a chatbot built using the SAP Conversational AI. The SAP Conversational AI supports webhooks that are web service calls made by the chatbot. The webhooks can be used to retrieve additional information to present to the user. I will show how to build a webhook that makes an oData call to retrieve business partner information.

Steps



  1. Create a chatbot using the SAP Conversational AI

  2. Create a Node.js express server for hosting the webhook

  3. Add oData calls to the express server to retrieve business partner information

  4. Test the chatbot using the SAP Conversational AI


Create SAP Conversational AI chatbot


To create the conversation for use in this example I have logged on to the SAP Conversational AI tools (https://cai.tools.sap/login) and created a new chatbot by clicking the New Bot button.



I then choose the greetings skill.



This will then create a new conversation and I then add the @greetings intent to the conversation which will return a greeting when the user types Hello or similar.



Next, I add a skill to the chatbot to prompt the user for their customer ID or email address.



This prompt will redirect to a getid skill and the getid skill will perform a call to a webhook that I will define in the next section.

Create Node.js Express Server


The webhook which will be used to look up the business partner is built using Node.js and Express and to create the initial project I have followed the Get Started with SAP Cloud SDK for Javascript tutorial resulting in the following structure.



The business-partner-route.ts file contains the code for making the oData calls to retrieve the business partner information.  To create this file I have added an import to the BusinessPartner SAP Cloud SDK library as specified in the tutorial Build OData Queries with the SAP Cloud SDK's Virtual Data Model. The import is shown below
import { BusinessPartner } from "@sap/cloud-sdk-vdm-business-partner-service";
import { Request, Response } from "express";
import { Filter } from "@sap/cloud-sdk-core";

This import provides the ability to make abstracted calls to business partner oData service using the SAP Cloud SDK's Virtual Data Model.

Add oData Calls


To perform the oData lookup I have added two functions to the business-partner-route.ts file.

The first is to retrieve the business partner by the business partner ID.
function getBusinessPartnerById(id: string): Promise<BusinessPartner[]> {
return BusinessPartner.requestBuilder()
.getAll()
.select(
BusinessPartner.BUSINESS_PARTNER,
BusinessPartner.BUSINESS_PARTNER_FULL_NAME
)
.filter(BusinessPartner.BUSINESS_PARTNER.equals(id))
.execute({
url: 'http://localhost:3000'
});
}

The second is to retrieve the business partner by email address
function getBusinessPartnerByEmail(email: string): Promise<BusinessPartner[]> {
return BusinessPartner.requestBuilder()
.getAll()
.select(
BusinessPartner.BUSINESS_PARTNER,
BusinessPartner.BUSINESS_PARTNER_FULL_NAME
)
.filter(new Filter('MiddleName', 'eq', email))
.execute({
url: 'http://localhost:3000'
});
}

Both functions make an oData call using the SAP Cloud SDK's Virtual Data Model and both functions return a promise as the call is asynchronous.

I have also created an orchestration function called businessPartnersForBot to handle the request from the bot returning either a greetings message with the name of the business partner if the business partner could be found or an error message if the business partner was not found or a technical error occurred.
export async function businessPartnersForBot(req: Request, res: Response) {
console.log(req.body)
if (!req.body.nlp) {
return res.send(resultAsText('Sorry not enough information provided'));
}
const idEmail = req.body.nlp.source;
if (!idEmail) {
return res.send(resultAsText('Sorry not enough information provided'));
}
if (isNaN(idEmail)) {
getBusinessPartnerByEmail(idEmail)
.then(businessPartners => {
if (businessPartners && businessPartners.length > 0) {
return res.send(resultAsText(`Hello ${businessPartners[0]['businessPartnerFullName']}`));
} else {
return res.send(resultAsText('Sorry I was not able to find your details'));
}
})
.catch(error => {
console.log(error.message);
return res.status(500).send(resultAsText('Sorry an error has occurred'));
})
} else {
getBusinessPartnerById(idEmail)
.then(businessPartners => {
if (businessPartners && businessPartners.length > 0) {
return res.send(resultAsText(`Hello ${businessPartners[0]['businessPartnerFullName']}`));
} else {
return res.send(resultAsText('Sorry I was not able to find your details'));
}
})
.catch(error => {
console.log(error.message);
return res.status(500).send(resultAsText('Sorry an error has occurred'));
})
}
}

Finally, I have added a utility function called resultAsText to return the message to the user in the format that the chatbot requires.
const resultAsText = (message: string) => {
return {
replies: [{
type: 'text',
content: message
}],
conversation: {
memory: { key: 'value' }
}
}
}

Testing the Chatbot


As an oData service for the business partner lookup I have installed the SAP mock data service for oData as found here: https://sap.github.io/cloud-s4-sdk-book/pages/mock-odata.html and I am running the mock data service on http://localhost:3000.

I have also started the express server for the business-partner-route.ts file by using npm run start:local to start a web service on http://localhost:8080 for the webhook.

To allow the chatbot to use the web service on http://localhost:8080 I am using ngrok (see https://ngrok.com/) to forward a https internet facing URL to http://localhost:8080 on my laptop.



In order to test the chatbot I have copied the https://0dce4ac0.ngrok.io url into the Actions tab for the getid chatbot skill in the SAP Conversational AI.  This means that whenever this skill is invoked the webhook will be called.



I can now test the conversational chatbot using the SAP Conversational AI tools.  The web based user interface to the chatbot tools has a button for chat with your bot which I have used to test the bot.

First by checking that the customer details can be retrieved by ID.



Then by Email



Finally confirming that the webhook returns a user friendly message if the customer was not found.


Conclusion


The SAP Conversational AI provides powerful tools to build an interactive customer experience which can be enhanced to retrieve meaningful customer information by the use of webhooks.  The example above has shown how to set up a conversation using the SAP Conversational AI tools and how to create a webhook for retrieving customer information using oData calls.

 

 

 
1 Comment
Labels in this area