Skip to Content
Technical Articles
Author's profile photo Somnath Paul

Integrate your SAP Bot with Google Assistant

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 Ghosh 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%20of%20our%20Integration

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

.

Assigned Tags

      3 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Vignesh Subramanian
      Vignesh Subramanian

      Hi Somnath,

      I have built a Bot using SAP CAI. I wanted to consume the OData service of RAP application which is developed in SAP S/4 On-Premise system.

      For connecting chatbot with OData of RAP app, still Node.js coding is required for OAuth authentication ?

      Or directly entering the credentials will work ?

      Thanks & Regards,

      Vignesh.

      Author's profile photo Somnath Paul
      Somnath Paul
      Blog Post Author

      Hello Vignesh,

      I would say get this tested in POSTMAN using a basic authentication method if this works it will work from SAP CAI as well.

      OAuth primarily being used for cloud / mobile app for providing limited access. So ideally this should work for on-premise with user id/password approach.

      A reference tutorial you may check here where I integrated Alexa echo dot with SAP ES5 (GW Demo system) using the user credential approach. Hope this helps.

      Integrate ALEXA with SAP | Integrating Alexa Echo dot with SAP using Node JS and SAP Chatbot - https://www.youtube.com/watch?v=Ob7miG6UE5g

      • Thanks, Somnath
      Author's profile photo Vignesh Subramanian
      Vignesh Subramanian

      Hi Somnath,

      Thank you for the video link. As you said in the above comment, if the OData service is accessible in POSTMAN with basic authentication - it can be exposed directly through NodeJS and once deployed the API url can be attached to the SAP CAI and that will work.

      Following the approach, below are the steps, I tried:

      1. Checked the OData service of my OnPrem system through POSTMAN with basic authentication -> Worked perfectly fine.
      2. Created the NodeJS app in local system, following the youtube video which you gave.
      3. Deployed the NodeJS app to SAP CF. While executing the CF deployed NodeJS app, first screen of app works fine. Once submitting the details(where POST call happens to OData service through NodeJS app), I'm getting "502: Bad gateway: Registered endpoint failed to handle the request".

      The OData service which I'm using is specific to my project and it connects through VPN. Do I need to follow a different approach compared to above one ? As the basic authentication when connected with VPN works well in local NodeJS app, but not with CF deployed NodeJS app.

      Please help to provide suggestion.

      Thanks & Regards,

      Vignesh.