Sunday 1 September 2019

WhatsApp Integration with SAP S/4HANA

I have added Part II is here, which will discuss the how we can handle the security aspect.

But How this integration make sense?


Answer is very simple think about a situation where you want to know your order status, either you would login to system or you ask someone. Now instead of doing this if you could ask whatsapp bot which could tell you insight of your order. Much simpler isn’t’ it.

Now most of the SAP CAI developer started thinking oh! this guy is just talking about building a bot in SAP CAI and expose this bot thorough another channel. But here is trick there is no whatsapp channel available in SAP CAI just like messenger, alexa, slack etc.

Then how this integration is possible?


This answer is also very simple its Twilio which will help us to achieve this.

What is Twilio?


Twilio is a cloud communications platform as a service. 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

Discussing  about twilio more  won’t make sense here because people are smart enough to googling and do r&d on it. But what most important thing i am going to discuss is how twilio enabling this integration.

Lets look at the technical architecture

SAP S/4HANA, SAP HANA Tutorials and Materials, SAP HANA Learning, SAP HANA Certifications, SAP HANA Online Exam

Business Story

SAP S/4HANA, SAP HANA Tutorials and Materials, SAP HANA Learning, SAP HANA Certifications, SAP HANA Online Exam

Bestrun is a US based company who use S/4HANA to run their business. Brooke is newly on-boarded employee in Bestrun and she is eagerly waiting to get her new laptop to start work. As many employee joined in last seven days, Bestrun had to place an order for Brooke which take approximate two day to deliver the order. Other side Brooke is quite excited like others new employee to start working, so she is always wondering when she will get the laptop. In the meantime she remembered her on boarding training  where she was introduced to Sarah a Whatsapp bot who can help employee to know different Business insight and information as Sarah is integrated with their core system S/4HANA. So Brooke add her in whatsapp account and ask about her order status and Sarah checked the order status in S/4HANA and  reply that order is delivered and you can see the smile on her face :-).

Enough of story let’s get into main business which is how we are going to connect the dots. Lets break this integration into smaller pieces so that it would make more easier to understand.

1. CDS and Odata service creation for querying order status. (In this example Purchase Requisition and Purchase order Status will be queried). 

2. Exposing the odata service through cloud connector and creating proxy api using SAP Api management.

3. Designing the skill of chatbot in SAP Cai. 

4. Creating  node.js app which will be interacting with S/4HANA and provide json response the way SAP CAI  understand.

5. Deploying the node.js app we created in last step into SAP Cloud Platform CF account.

6. Use the deployed application URL with proper path as webhook of skill in SAP CAI.

7. Test the bot using SAP CAI.

8. Create a free account in twilio.

9. Enable the whatsapp channel.

10. Create a twilio function to interact with SAP CAI using SAP CAI SDK.

11.Deploy the function and use function Url as Webhook of WhatsApp channel.

Among 11 step till 7 there are numerous blog people have written so i am not going to write the same stuff again rather provide you reference link  which you can use. Lets discuss on last four points one by one.

8. Create free account in twilio using this link, you can get free trial account where all the beta services are enabled. Registration process is quite straight forward.

SAP S/4HANA, SAP HANA Tutorials and Materials, SAP HANA Learning, SAP HANA Certifications, SAP HANA Online Exam

9. Enable the WhatsApp channel

Twilio provide WhatsApp Sandbox account, you have to setup the sandbox account in your mobile. The process is pretty straight forward. You have to add your sand box number in your WhatsApp and send the code (E.G join say-rays) as first message from your WhatsApp like below.

(log-in to your twilio account and click on programmable sms, you will find this option)

SAP S/4HANA, SAP HANA Tutorials and Materials, SAP HANA Learning, SAP HANA Certifications, SAP HANA Online Exam

On successful connection you will get Message received like below.

SAP S/4HANA, SAP HANA Tutorials and Materials, SAP HANA Learning, SAP HANA Certifications, SAP HANA Online Exam

Now leave it like this, we will come to this later. Now we have to create twilio function which will interact with SAP CAI using SAP CAI SDK.

10. Create a twilio function to interact with SAP CAI using SAP CAI SDK.

Now click on runtime in your twilio dashboard, you will find function (beta) option like below.

SAP S/4HANA, SAP HANA Tutorials and Materials, SAP HANA Learning, SAP HANA Certifications, SAP HANA Online Exam

Now explore the function, you will find manage and configure option, first click on configure option we have to add npm module for SAP CAI SDK.

SAP S/4HANA, SAP HANA Tutorials and Materials, SAP HANA Learning, SAP HANA Certifications, SAP HANA Online Exam

Now explore the function, you will find manage and configure option, first click on configure option we have to add npm module for SAP CAI SDK.

SAP S/4HANA, SAP HANA Tutorials and Materials, SAP HANA Learning, SAP HANA Certifications, SAP HANA Online Exam

After saving this now go to manage function option and add function with blank template.

SAP S/4HANA, SAP HANA Tutorials and Materials, SAP HANA Learning, SAP HANA Certifications, SAP HANA Online Exam

Now give a function name, path and add the below code and save it like below.

SAP S/4HANA, SAP HANA Tutorials and Materials, SAP HANA Learning, SAP HANA Certifications, SAP HANA Online Exam

After pasting the below code and before saving replace the request token with your request token which is used in 4th line. You can get easily the request token from your bot like below.

SAP S/4HANA, SAP HANA Tutorials and Materials, SAP HANA Learning, SAP HANA Certifications, SAP HANA Online Exam

Twilio Function code.

exports.handler = function(context, event, callback) {
global.twiml = new Twilio.twiml.MessagingResponse();
    var sapcai = require('sapcai')
    
let build = new sapcai.build('a1c27748c4cf50844f134b8e0590d61c', 'en')
  build.dialog({ type: 'text', content: event.Body}, { conversationId: '918884211139' })
  .then(function(res) {
      'debugger';
    dta = res.messages[0].content;
    twiml.message(dta);
    callback(null, twiml);
  })
//callback(null, twiml);
};

Now Save it, after successful save copy the function path which has to be Webhooked in WhatsApp channel.

SAP S/4HANA, SAP HANA Tutorials and Materials, SAP HANA Learning, SAP HANA Certifications, SAP HANA Online Exam

11. Deploy the function and use function Url as Webhook of WhatsApp channel.

Now go to programmable sms and then go to WhatsApp Sandbox to add the function path as Webhook like below.

SAP S/4HANA, SAP HANA Tutorials and Materials, SAP HANA Learning, SAP HANA Certifications, SAP HANA Online Exam

Save it and thats it. You can test it now. 

Here i have attached a video of this Integration testing, hope everyone would like it.

No comments:

Post a Comment