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: 


 

Serverless computing has opened a wide range of possibilities. With SAP Cloudfoundry FaaS enabled by Kubeless, it's not only a huge value-add to the end users but also to the developer community! I would recommend my readers to check this post written by Former Member to get an in-depth understanding of SAP CF FaaS. Also, if you have missed my previous posts on FaaS, make sure you check them as well over here and here.
TL;DR: You can create a WhatsApp bot with FaaS that can get stuff done in your cloud ERP ! Although it may not be able to offer end-to-end capabilities, it can solve common problems.



Let's get into action with our MVP WhatsApp bot 😉 This is the high level architecture for the same. But don't worry we can cover that in a jiffy! By the end of this tutorial, you will be able to create a simple WhatsApp bot leveraging the power of FaaS.

High Level Architecture




In this post, I've used CF Functions along with Recast.AI, Twilio & SAP Business ByDesign (ByD) to build a simple WhatsApp chatbot. The architecture is designed in such a way that the bot can be scaled to perform tasks that it is trained for - using the skills(in Recast.AI) and fallback functions (in CF FaaS). Basically, you can substitute the Cloud ERP with either SAP ByD, Business One or S/4 HANA Cloud ERP by using the appropriate APIs. You can find them in the SAP API Business Hub.
Currently, the intelligence of the bot is powered by Recast.AI where the skills can be modeled and trained with its corresponding set of triggers and actions. Since WhatsApp for Business API is still in beta, it provides limited public preview, however you can use Twilio to instantly provisions a sandbox WhatsApp account. If you have followed my previous posts, you'd now be able to create functions with HTTP triggers and timers and they can be created for free with SAP Cloud Platform trial account as well !

The per-requisite steps involved to achieve this is as follows:

  1. Create a SAP Cloud Platform trial account and allocate quota for Functions (totally free).

  2. Create a Twilio Account and enable WhatsApp Sandbox usage (its free btw).

  3. Create a SAP Recast.AI account. (free again!)

  4. Expose the necessary APIs in ByD/Cloud ERP and configure a technical user for the same(Not mandatory for initial setup though).


Functions(FaaS Setup):


If you have followed my previous blog posts mentioned above, you'd have a working setup and Functions Designer dashboard open already. Create a function and a HTTP trigger for now.

You can use the following snippet for the function:

i. Function : Create a function,say 'fx', with nodejs runtime and paste this. In the below code, you can leave the accountSid,authToken, recast request token & twilio whatsapp bot number empty for now. Basically this function takes user chat as input and sends it to recast for processing. The message from the bot is sent back to the user. This facilitates a two-way communication with the user over WhatsApp. Dont forget the dependencies! make  sure you mention them in the dedicated text box below the function ide.
//To parse the request body
var qs = require("querystring");
//Twilio Account Details
const accountSid = '<your_twilio_account_sid>';
const authToken = '<your_twilio_account_authtoken>';
const client = require('twilio')(accountSid, authToken);
var recastai = require('recastai').default;
//Recast Request Token
var build = new recastai.build('<your_recast_request_access_token>', 'en');

module.exports = {
handler: function (event, context) {
var req = event.extensions.request;
var res = event.extensions.response;
var qp = qs.parse(req.body.toString('utf8'));

build.dialog({ type: 'text', content: qp.Body}, { conversationId: '<countrycode_and_your_num>' })
.then(function(res) {
console.log(res);
client.messages
.create({
body: '' + res.messages[0].content ,
from: 'whatsapp:+<countrycode_and_twilio_num>',
to: 'whatsapp:+<countrycode_and_your_num>'
})
.then(message => console.log(message.sid))
.done();
});
event.extensions.response.send("OK");
}
}

Dependencies:
{
"dependencies":{
"twilio":"latest",
"querystring":"latest",
"recastai":"latest"
}
}

Note that since the Twilio webhook call sends a application/x-www-form-urlencoded payload, I've converted it to UTF8 and parsed the query string using querystring package. I will cover the possibility of using Express.js or Koa in the upcoming blog posts. In such scenarios, body-parser package will take of the parsing!

ii. Create a HTTP Trigger for function 'fx' and copy this URL. We will paste this in the Twilio webhook URL section. This means that whenever we text the Whatsapp bot number, it will forward the content to our FaaS function for processing.


WhatsApp for Business sandbox account:


Login to Twilio to get a free WhatsApp for Business sandbox account. You can apply directly to WhatsApp for the access as well. However it might take some time for approval and hence the sandbox would be of great help meanwhile.

Immediately, when you login to Twilio, you can find the accountSid and authToken. Copy them and paste in the function.



Navigate to 'Programmable SMS' tab and under the section WhatsApp, you will find a place(Sandbox configuration) where you can specify the webhook URL. This is where you paste the FaaS HTTP trigger URL!



Under the sandbox participant section you can find the WhatsApp bot number to which the users can message. However, you need to join by sending join <xxx-yyy> text to that number from your own number so that it is added as a participant. You can invite your colleagues in a similar fashion.

When you register successfully, you get this confirmation:



Now you can add the twilio number and your number in the function as well.

Recast.AI account:


Now, the only remaining setup to complete the MVP is to have a recast.ai or dialogflow or any chatbot platform account that offers you a neat nodejs sdk that can communicate with the chatbot platform from our function. I prefer recast.ai since its already SAP's and also for the reason that it is dead simple! 😄

The good thing is you have pre-defined set of skills from which you can start when you create a bot.



I've named my bot as potato for now. Dont ask me why



You can copy the Recast request token by clicking on the settings button. Paste this in the function.

Save and Deploy the function now.



I've added a skill called byd with a couple of intents. The intents are mapped to the actions. In layman's terms, this means I told the bot what & how to tell when the user says 'Hi' or when the user throws around a specific question.I would recommend you to play around with that a little bit to get a grip on how it works. At this point, you can start testing the bot from WhatsApp directly ! You can also test locally in the RecastAI chatbox as well.





Optional: I've added another function with a HTTP trigger to get the leads from byd system or to create new ones. You can map this to an action and paste the http trigger of the respective function by clicking on Call Webhook button. This does the talking to the ByD system/Cloud ERP. However, at this point, your bot would be capable of responding to basic messages. It's a matter of training now.

Training the bot & Testing.


Initially, the bot didn't have proper intents,skills or the training needed for proper functioning.

 



After a bit of training and defining the right intents & skills, TADAA!



 

In my next blog post, I have covered in-depth on how functions can communicate with REST/SOAP APIs. Fow now, with minimal effort, you can setup a functioning WhatsApp chatbot with the help of SAP CF FaaS and Recast.AI's NLP. With a bit of hacking, you can train the bot to create leave requests, leads, contacts, query KPIs etc. I would leave it to your imagination ! 🙂

Let me know if you have any queries and please share your interesting findings as well! You can contact me on LinkedIn or Twitter or through any of the profiles mentioned in my site regarding the same! Until then stay tuned!

 

 

 
5 Comments
Labels in this area