Skip to Content
Author's profile photo Simen Huuse

Chat with your SAP system using SCP and Google’s api.ai

Have you talked with IKEAS’ chat-bot Anna, and maybe you have asked her about stock information for one of their products, or the opening hours of the nearest warehouse? Did you know that it’s not so hard to build the same functionality using SAP on-prem or cloud solutions? This blog article will give you an idea of how to build a chat bot using SAP Cloud Platform and Google’s api.ai.

 

Natural Language Processing

Chat bots can be built using a combination of the SAP Cloud Platform (SCP) as well as a provider of natural language processing (NLP). In this example, we use services from the Google acquired company api.ai originated in Palo Alto. NLP supports the interpretation of user’s intention and passes it forward to the SCP. It uses machine learning to interpret variations of a topic, so that the user does not have to say exactly what the bot is expecting, but can talk in a way that feels normal to the user.

 

Set up your api.ai agent

This part has been quite well explained in blogs and videos from the nice people at api.ai. Here’s a few guides that should help you through the process:

After following the instruction, preferably with your own idea in mind, you should be able to test it out using api.ai’s own user interface simulator.

Don’t be too fancy in your initial prototype agent. Just make sure that you can do some basic communication with some defined intent, action and a parameter.

 

Prepare your SAP Cloud Platform.

Great news: Your brand new api.ai agent speaks JSON. That makes everything quite easy in terms of handling the messages being passed to and from the platform.

 

On the SAP Cloud Platform side, you would want to build a webhook in Java that can interpret this JSON stream and enrich it with whatever business related data you desire. I will not go into details about creating Java applications in Eclipse and deployment on SCP – so many great people have done that better than I ever can. Do a quick search or take the openSAP course SCP Essentials.

Remember to use the following annotation:

response.setContentType("application/json");
response.setCharacterEncoding("utf-8");

In your return JSON you should at least fill in the parameters:

  • “speech”
  • “displayText”
  • “source”

As a starter you may want to just enter dummy values in the parameters test your application in Postman or some other tool of your choice to see that a JSON is generated.

Connecting the agent to SAP Cloud Platform

By now you have your agent running on api.ai and a webhook running on SCP. Now, let’s get your agent to talk to SCP. For that to happen, you need your webhook URL. You get that in your SCP cockpit.

In your api.ai you need to go to your agent “Fulfillment” page. Make sure that it is enabled, and add your webhook URL is placed in the URL parameter.

If your webhook isn’t publicly available, this is also where you would add username/password or header keys. Make sure you remember to hit “Save” before you continue.

…this is where you would add username/password or header keys.

Then, still in your api.ai cockpit, you should be able to to back to the intent that you wish to pass on to SCP and check the “Use webhook” checkbox at the very bottom. What will happen now is that the JSON will be passed on from your agent to the URL you provided in the fulfillment section.

This can be a bit hard to debug, so again you should do this in baby steps. Try to alter the JSON with adding to it’s response first, without caring about the input intents or parameters. When you get that working, use your JSON weapon of choice to interpret the intent and parameters.

 

Add business value

When you have managed to connect the dots and are able to pass a reply back to your agent, you can start using the intent and the parameters on the SCP side. That is where the good bits are, because now you can use all the super powers of your cloud products or backend system of record to enrich your agents response. This is really where your business value is. For instance, you can create a simple destination in SCP that reaches our to your SAP backend system, pass the intent and parameter back to an ABAP function that returns some business data. Your SCP application can transform the return into a meaningful sentence and pass it on to your agent. Confused? Hope not. Just try it out!

 

That’s it?

Add a nice user interface using the many options of api.ai and voila!

Ok, it may not be that simple to get business value from your creation. First of all, finding the perfect use case within your organization can be quite hard. In addition to that, setting up the logic on your agent can be somewhat of a hassle. If you want to make a full scale production bot I would recommend reading up on (and testing) the CVS or JSON based upload of entities on api.ai. It will make your life easier, as your data sets probably are big and the worst of it all: They change over time. Intents can also be uploaded with JSON, and you can train your agent using text files.

Think big – but start small. Thanks for reading and good luck!

(ps. Check this out to avoid the major bloopers: https://chatbot.fail/ )

 


About the author:

Simen Huuse is a Norwegian native, educated with a MSc in neuroinformatics from the University of Edinburgh. He has been passionate about SAP since 2005. He currently lives in Palo Alto, California with his wife and two kids.

Twitter: https://twitter.com/simenhuuse

LinkedIn: https://www.linkedin.com/in/simenhuuse/

 

 

 

 

Assigned Tags

      7 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Hi Simen,

      Really good article.

      Do you have any document which we can follow to create a webhook on SCP. It will be very helpful.

       

      Cheers

      Author's profile photo Simen Huuse
      Simen Huuse
      Blog Post Author

      Hi Sumeet,

      First I think you should go through this article http://www.infoworld.com/article/3122987/application-development/modernize-your-api-with-webhooks.html , to get some overview.

      Then you can look at this example:

      https://github.com/ashwanthkumar/slack-java-webhook

       

      Best wishes,

      Simen

       

      Author's profile photo vijaykumar gundeti
      vijaykumar gundeti

      Hi Simen,

      Nice article,Great work.

      Simen, even I am very excited to build such kind of bots on API.ai with SCP.

      Just I am confused now is basically what is Webhook which we need to give in the fulfillment section, is it a SAP OData service link or something else.

      If not SAP OData, then how to create that Webhook in SCP as you mentioned that Webhook can be created in SCP, which application it will be of ?

       

      Do you have some step by step procedure to achieve this.

      Thanks in Advance.

      Vijaykumar

      Author's profile photo Simen Huuse
      Simen Huuse
      Blog Post Author

      Hi Vijaykumar,

      The fulfillment is using a webhook, and the webhook is java running on SAP CP (or anywhere else in the web for that mather). The beauty about doing it on SAP CP is that you can easily integrate with the SAP backend or other SAP related services, as long as you have a destination in your SAP CP. It could be OData, it could be JSON, it could be anything your webhook JAVA app will chew.

      I do not have a step-by-step for creating webhooks, sorry.

      All the best and good luck!

      Simen

      Author's profile photo vijaykumar gundeti
      vijaykumar gundeti

      Hi Simen,

       

      Thank you Sir for your valuable reply,

      I was been able to successful create a bot, but what I did is directly I used the oData URL in WebHook  by making that ECC system's firewall open to the outside.

      But in the real time scenarios we can't make that (security reasons).

      Now only my concern is to create a Webhook with help of comments you mentioned above, I have a created a destination of my ECC system on SAP CP.

      If possible can you please share me some example JAVA code of webhook creation as I don't have much idea about JAVA language. It would be very great help for me.

      Author's profile photo harish vyas
      harish vyas

      Dear Vijaykumar ,

       

      In the above post, you were able to create chatbot using ODATA URL directly.

      I have created an ODATA service, and have maintained SERVICE TYPE usename/password for same service in SICF. Because of this, I am able to access the OData link directly through internet explorer i.e. it is not asking for username/password.

       

      But now when I entered same OData URL in dialogFlow fulfillment section and testing my application, it is gving error: "webhook call failed. error webhook response was empty".

       

      Since I am on client network and able to run the ODATA URL indenpendently (through Internet Explorer), and so I think no need to do anything on ECC Firewall.

      Also using HTTP or HTTPS make any difference.

      Can you suggest the things you did to achieve this requirement.

       

      Regards,

      Harish

      Author's profile photo harish vyas
      harish vyas

      Dear Simen,

       

      Firstly will like to thank you for posting this blog. Real helpful in understanding the concepts.

       

      For learning purpose, I am trying to integrate XSJS (created in trial version of SAP HANA CP) with API.AI (by providing URL to XSJS file). Additionally I have also entered Username and Password in fulfillment section. Inside XSJX file I am not writing any code to trigger ODATA, but populating the JSON structure with some dummy data in XSJS file only.

       

      XSJS file link: 

      https://XXXXXtrial.hanatrial.ondemand.com/CHATBOT_1/myLibrary.xsjs

       

      Content of XSJS file: 

      $.response.status = $.net.http.OK;
      $.response.contentType = “application/json”;
      $.response.setBody(JSON.stringify({
      “speech”: “Test”,
      “displayText”: “Test”
      }));

       

      Output of XSJS file, when run independently:

      {“speech”:”Test”,”displayText”:”Test”}

       

      Inside my FULFILLMENT section in API.AI, when I entered  below link along with username/password:

      • httpS://XXXXXtrial.hanatrial.ondemand.com/CHATBOT_1/myLibrary.xsjs

      I am getting below error in JSON

      “status”: {
      “code”: 206,
      “errorType”: “partial_content”,
      “errorDetails”: “Webhook call failed. Error: 403 Forbidden“,
      “webhookTimedOut”: false
      },

      I am getting below error in JSON

      “status”: {
      “code”: 206,
      “errorType”: “partial_content”,
      “errorDetails”: “Webhook call failed. Error: Webhook response was empty.“,
      “webhookTimedOut”: false
      },

       

      Can the issue be because of authentication level that needs to be set for our service using the XS Admin tool.

      Request you to kindly suggest what can be the issue behind this.

       

      Regards,

      Harish