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: 
somnath
Active Participant

Introduction:


This blog post is a part of my continuous explorations on SAP Chatbot and in this particular blog post, I would like to share how you can integrate your Google Mini Home with your SAP Chatbot.

My previous blog post on CAI explorations can be found here.

I was also interested to make my bot internationalized by adding Google Translation capabilities.

Now, the first thing is we don't have any direct integration possibilities from the SAP CAI console, and looks like something additional steps I need to do here.

Let's Start...

Implementation Steps:


Before I continue with technical detailing, there is already a brilliant blog post by sudip.ghosh4 which is almost the same thing!

Google Assistant Integration with SAP S/4HANA


Still, I thought to share my explorations mainly  for two reasons:

  • I faced a little challenge while implementing the concepts as looks like both SAP CAI and Google Translation can't work together!!

  • Sharing my experience while enjoying this journey with technology integrations


I know what you are thinking, then the above blog post information is incorrect?

Of course Not 😄 rather this blog post better to call out blog post date actually helped me to fix the issue 😛

Let's continue, to understand, what I had to do to make my POC successful.


Components for our Integration



Step 1: Dedicated Node JS application for connecting the dots (SAP Chatbot & Google Mini Home)


To achieve the integration I had to create another Node JS application to enable Google Translation as well as connecting my SAP Chatbot using CAI SDK.

Quickly check the code piece below:

 
const express = require("express");
const bodyParser = require("body-parser");
const translate = require("@k3rn31p4nic/google-translate-api");
var sapcai = require("sapcai").default;
const app = express();
const port = process.env.PORT || 3000;
// const port = 3000

app.use(bodyParser.json());
app.use(express.urlencoded({ extended: false }));

app.get("/", (req, res) => {
res.send(`

<form action="/sapcai" method="POST">
<p>Enter your Query in any language</p>
<input name="queryText" autocomplete=off>
<button>Fetch Details</button>
</form>
`);
});

app.post("/sapcai", (req, res) => {
var response = res;
var msg = req.body.queryText || req.body.queryResult.queryText;

var detectlang, transtext, cairesponse;

if (req.body.session) {
var sessionID = req.body.session.substr(46, 115);
} else {
sessionID = "a12345bn1235xyz";
}

var build = new sapcai.build("<YOUR BOT TOKEN>", "en");

translate(msg, { to: "en" }).then((res) => {
detectlang = res.from.language.iso;
transtext = res.text;

build
.dialog(
{ type: "text", content: transtext },
{ conversationId: sessionID }
)
.then((res) => {
console.log(res.messages);

if (res.messages[0].content.title) {
cairesponse = res.messages[0].content.title;
} else {
cairesponse = res.messages[0].content;
}

translate(cairesponse, { to: detectlang })
.then((res) => {
response.send({ fulfillmentText: res.text });

console.log(res.text);
})
.catch((err) => {
console.error(err);
});

// Do your code
})
.catch((err) => console.error("Something went wrong", err));
});
});

app.listen(port, () => {
console.log("Server is running on port " + port);
});

Let's understand the above block of codes.

Below two lines of required functions enabling my node js with Google Translation & SAP CAI respectively.
const translate = require("@k3rn31p4nic/google-translate-api");
var sapcai = require("sapcai").default;

Below GET call is optional as I wanted to test the application from localhost: port (3000) before deploying the node js application to the cloud.
app.get("/", (req, res) => {
res.send(`

<form action="/sapcai" method="POST">
<p>Enter your Query in any language</p>
<input name="queryText" autocomplete=off>
<button>Fetch Details</button>
</form>
`);
});

Session ID / Conversation ID:


  if (req.body.session) {
var sessionID = req.body.session.substr(46, 115);
} else {
sessionID = "a12345bn1235xyz";
}

Conversation ID / Session ID is very important and mandatory for SAP CAI to work fine. I didn't put much effort into unique session-id generation while did testing from the local machine and just kept it some hardcoded string value as

sessionID = "a12345bn1235xyz";





As while it would from my google mini then the session id generation will be auto taken care of and hence good to go with
var sessionID = req.body.session.substr(46, 115);​

Cool...

  • Now let's first start with the translation once receiving the user's request and detect the source language.

  • In my case, the target language should be 'EN' as SAP Chatbot can understand English only.

  • Before delivering the response back to Google Mini Home, the information needs to be translated back to the original language.


Quite simple and straight forward isn't it?
translate(msg, { to: "en" }).then((res) => {
detectlang = res.from.language.iso;
transtext = res.text;

build
.dialog(
{ type: "text", content: transtext },
{ conversationId: sessionID }
)
.then((res) => {
console.log(res.messages);

if (res.messages[0].content.title) {
cairesponse = res.messages[0].content.title;
} else {
cairesponse = res.messages[0].content;
}

translate(cairesponse, { to: detectlang })
.then((res) => {
response.send({ fulfillmentText: res.text });

console.log(res.text);
})
.catch((err) => {
console.error(err);
});

Test the Node JS Application:


I was quite excited to test my application from the local machine, but unfortunately got stuck as started getting connection refuse error from the Google Translate API call.

After a while, I realized if I remove SAPCAI and just test the google Translation part, then my application is working fine but that is not my goal of course 😞


Scratching my head with no clue and finally came across the earlier mentioned blog post by Sudip.
 I realized there was some version update on Google Translate API in NPM and as a workaround, I downloaded the previous version which is  1.1.0 and it worked!!! 


I recorded the steps which I performed to achieve what I intend for and excited to share with you for your feedback.

- Video Tutorials

Integrate your SAP BOT with Google Assistant- EP4


Step 2: Creating a Google Action Project and integrating with Node JS Application


Well described by Sudip in his blog post and I also recorded my steps specific to my project and can be found in the below video tutorial.

Integrate your SAP BOT with Google Assistant - EP5


Conclusion:


The journey to integrate with Google Mini Home / Smartphone was quite good technology fun for me and of course, will encourage the beginners to experience similar integrations by following a few simple steps as mentioned above.

But make sure the correct version is downloaded to make this integration works 🙂

Many more things yet to be explored and will come back shortly to share some new experience with you.

Stay tuned and don't forget to share your feedback.

Happy Learning!

-BR, Somnath

.
3 Comments
Labels in this area