Skip to Content
Technical Articles

SAP CAI + S4HANA Cloud + WhatsApp = A Marvelous User Experience!

Hi Guys,

Welcome to Part 3 of the series SAP Conversational AI Overview & Build your first BOT.

In this blog post, I am going to discuss one of the coolest integration in the SAP world ‘SAP S4HANA Cloud and Whatsapp’.

As many of you know, WhatsApp is one of the most popular messaging platforms, and nowadays, it can also be used for business purposes, so why not have your S4HANA system integrated with it.

Before stepping into the integration with WhatsApp, I’ll urge the readers to go through my previous blog SAP Conversational AI + S4HANA Cloud → ‘A Fantabulous Combination’ which explains the integration of SAP CAI with S4HANA Cloud.

Now let’s focus on the topic of the day.

Why such integration with WhatsApp is needed?

Let’s talk about a situation where the end-user wants to know the status of his/her order, or the user needs some details from the SAP system. It requires the user to login to the SAP system and performs the requested operation.

Now how about if we could get all such details in our smartphone right away from SAP System and that too with just having a chat conversation with the bot.

That sounds super cool and convenient…right

This blog here talks about the security aspect of the integration of WhatsApp.

How this integration is going to work?

Now, if you have worked with CAI integrations before, you would already be aware that there are some direct channels for integration with Telegram, Messenger, WebChat, etc.

But for WhatsApp, there is no direct integration channel available. The trick here is to make use of a trendy Cloud Communication Platforms called Twilio.

Twilio is a cloud communications platform as a service (CPaaS). Twilio allows software developers to programmatically make and receive phone calls, send and receive text messages, and perform other communication functions using its web service APIs.

 

Technical Architecture

 

Now let’s get into the main steps involved in achieving this integration.

Step 1. Create a custom business object (CBO) or CDS view or use the SAP whitelisted API as per the requirement. Here, I am using the CBO created in my previous blog.

Step 2. Create the communication scenario and arrangement for the CBO or CDS view. In case you are using whitelisted API, those are already exposed you just need to configure them in your S4HC system.

Step 3. Create a Node.js Application that will act as a middleware between the SAP S4HANA Cloud System and the SAP CAI.

Step 4. Deploy the Node.js application on the SAP Cloud Platform in the Cloud Foundry (CF) environment.

Step 5. Develop and design the bot skills in SAP CAI as per the needs of the use case.

Step 6. Use the URL generated by the deployed application as a webhook in the CAI Bot.

Step 7. Test the bot.

above Steps 1-7 can be checked out in my previous blog.

Step 8. Create a free Twilio account on Twilio. Twilio trial account provides every new user with some credits, so every time you send or receive messages between your BOT via Twilio WhatsApp channel, those credit points will be utilized.

Step 9. Enable the Whatsapp Channel.

Login into your Twilio account, expand the left side panel and click on Programmable SMS.

 

Then click on Whatsapp.

 

Twilio provides the Sandbox environment for Whatsapp. You have to set up the sandbox account on your mobile. You just need to add the number and send a message from your WhatsApp as shown below.

After the connection is successfully established, you’ll get the below message.

 

Step 10. Now we need to create a Twilio function that will interact with the SAP CAI using SAP CAI SDK.

From the Twilio, dashboard click on Functions under Runtime category.

First click on the Configure option, here you need to add the correct version of the npm module for SAP CAI SDK.

Add the npm dependencies and save.

 

Now go to manage functions and create a new function with a blank template.

Now provide function name, path, and the below code. Now Save the function.

 

exports.handler = function(context, event, callback) {
console.log('invoked with', event)
global.twiml = new Twilio.twiml.MessagingResponse();
    var sapcai = require('sapcai')
	let build = new sapcai.build('<CAI API KEY>', 'en')
  build.dialog({ type: 'text', content: event.Body}, { conversationId: event.From })
  .then(function(res) {
      'debugger';
    data = res.messages[0].content+"\n"+res.messages[1].content;
    twiml.message(data);
    callback(null, twiml);
  })	
};

 

Add your BOT’s API token to the code. You can get the token from your bot like below.

 

Once the function is saved and deployed copy the function path which will be used as a webhook in the WhatsApp channel.

Step 11. Now use the deployed function’s URL as webhook in Whatsapp channel.

Navigate to the Whatsapp channel then sandbox, here give the function path which you copied in the previous step.

Save it and now you can test it.

 

 


Parts of this blog post has been referenced from the below sources:

https://blogs.sap.com/2019/10/28/whatsapp-integration-with-sap-s4hana-cloud/

https://blogs.sap.com/2019/07/16/whatsapp-integration-with-sap-s4hana/

Thanks to Sudip Ghosh for the amazing tutorial.


Stay tuned for more upcoming fantastic stuff.

 

I hope you all have enjoyed this blog post, and I’ve been able to make myself clear. Please let me know your thoughts, doubts in the comment section below, and if you enjoyed, please like and share.

Wishing excellent health and safety to all of you and your friends & families.

 

 

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