Skip to Content
Author's profile photo Craig Cmehil

Amazon Echo and the SAP HANA Cloud Platform

For awhile now I’ve explored possibilities with the Amazon Echo and SAP HANA, and over time I’ve received a lot of requests on how to do that or how to do that. Therefore I decided to put together a little blog here to describe the “how” of some of the demos I have done and how you can do it using SAP HANA for free inside the SAP HANA Cloud Platform trial system.

I’ve been a fan of the device since it came out and have done dozen of demos, dozens of skills and explored many ways of working with the device.

I also realized that with some time, patience and a little research everything you need to complete your own SAP HANA based Amazon Echo demo is already online in our Tutorial Catalog.

I actually started vacation yesterday so any comments or questions may be slightly delayed but since I decided I would share a personal favorite in terms of projects I thought that as the year starts to come to a close perhaps you may want to find the time for a new project as well and why not give you a helping hand – even if you only have access to the simulator and not the device itself.

The following project details efforts to implement an SAP HANA XS application that interacts with an Amazon Echo via OData services. The purpose behind the original application was as a virtual assistant during the SAP TechEd event in 2015 followed by several modifications to the base for additional projects. The following should outline the basics necessary for the creation of the application, data tables and OData services. Followed by the linking and interaction needed within the Alexa Skill.

The following will outline what is necessary to repeat this process:

  • Create your MDC instance
  • Create your first XS application.
  • Enable your XSODATA service
  • Posting to your HANA application

Now to get going with the steps and where to find the help!

  1. The first step necessary for the project will be to Sign up for an free trial account on SAP HANA Cloud Platform
  2. Once you have signed into the system you can follow the steps in the following tutorial to create your first MDC instance.
  3. Once the instance is created you can follow the steps in this series to create your first application and enable your XSODATAservice.
  4. Once you have your data tables created and your services enabled you can follow the steps in this tutorial on how to “submit” data to your tables via the service as well.
  5. For the purposes of interacting with the Amazon Echo I created two tables, the first table is the primary table containing the data for the Echo responses while the second table is there to enable a feedback mechanism for visualizing the requests via a web page in real time.
    The table for data responses is as follows.

    @Catalog.tableType : #COLUMN
    Entity Details {
        key ID: Integer;
        TIMESTAMP: SDate;
        TITLE: LString;
        CATEGORY: SString;
      };

    The table for the logging those requests is as follows.

    @Catalog.tableType : #COLUMN
    Entity Details {
        key ID: Integer;
        TIMESTAMP: SDate;
        CATEGORY: SString;
      };

    Be sure to also include the necessary hdbsequence the posting of data. This is also explained in the other tutorials.

  6. To make things a bit easier and since this was purely a demo I also enabled an anonymous connection on my services package for the posting and requesting of data via the individual services. To do so can be found via the following how to guide.
  7. Now that the general data tables and services are in place. You can also follow these tutorials to create a UI interface to be able to enter new entries that later your Alexa Skill set would be able to use. 
  8. Now that you have a basic interface as well as your data tables and services, the next step would be to interface it with an Alexa Skill which you can use the following tutorials to understand better how to do that.
  9. In the Alexa Skill I added the following function in order to “post” to the created services. 
    var setHANALog = function(itemName){ 
        var body = JSON.stringify({ "ID" : 1, "CATEGORY" : itemName }) 
        var request = new https.request( { 
             hostname: host, path: "/packagename/services/echo.xsOData/newlog", 
             method: "POST", 
             headers: { 
                  "Content-Type": "application/json; charset=utf-8",
                  'Accept': '*/*' } 
            }) 
       request.end(body) 
          request.on('response', function (response) { 
              console.log('STATUS: ' + response.statusCode); 
              console.log('HEADERS: ' + JSON.stringify(response.headers)); 
              response.setEncoding('utf8'); 
              response.on('data', function (chunk) { 
                   console.log('BODY: ' + chunk); 
          }); 
       }); 
    };
  10. I also added a function for accessing the entries.
    var getResponseFromHANA = function(itemName, callback){
      var options = {
              method : 'GET',
              host : host,
              path : "/packagename/echo.xsOData/InputParams(LV_CATEGORY=%27"+itemName+"%27)/Results?$format=json",
              headers:{
                          'Authorization': authStrIoT,
                          'Content-Type': 'application/json;charset=utf-8',
                          'Accept': '*/*'
                      }
          };
    
      https.get(options, function(res){
          var body = '';
    
          res.on('data', function(data){
              body += data;
          });
    
          res.on('end', function(){  
              var result = JSON.parse(body);
              var items = result.d.results;
              if(items.length>0){
                callback(items[0].TITLE);                
              }else{
                  callback("");
              }
          });
    
      }).on('error', function(e){
              console.log('Error: ' + e);
      });
    };
  11. Remember to also include the additional parameters being used in both functions. For example my server is hanatrial and my account is d045495trial
    var host = 'iotmmsmdc<ACCOUNT>.<SERVER>.ondemand.com';
    var authStrIoT = 'basic_authorization("SYSTEM", "<PASSWORD OF YOUR MDC INSTANCE>")';
  12. With that in place it was a matter of testing. From AWS you have the ability to enter your utterance and test the result.
  13. Or you can access the logs to view your console log outputs or other outputs. 
  14. The final step was to visualize in real time the requests as they were coming in. 
  15. To accomplish that it was a quick HTML page created using some basic JavaScript and HTML with a automatic reload of the data. Very similar to how this tutorial describes it.

I figure the whole process should take you anywhere from 45 to 90 minutes and will highly depend on your experience with SAP HANA XS applications and your experience with Javascript – that being mainly because I used the Node.js version of the Alexa Skill Kit.

Using this baseline demo I’ve been able to create close to a dozen different demos and have even been able to integrate and interact with other data within the system, it’s really just a matter of determining how best to interact with the device and how to grab data from the system. Some of the more advanced stuff uses XSJS libraries for more complicated interactions.

Enjoy working with this and I hope it can help you on your way to making some interesting demos as well!

Assigned Tags

      30 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Sergio Guerrero
      Sergio Guerrero

      Craig,
      such an awesome post!

      on step 8, was there any cost associated with developing against the Alexa API?

      Thank you again for sharing this great blog.

      Author's profile photo Craig Cmehil
      Craig Cmehil
      Blog Post Author

      So far no costs but these are usually short term demos...

      Author's profile photo Sergio Guerrero
      Sergio Guerrero

      yes, I wanted to know about how much it would cost for your demo, but i also saw on the AWS site, there is cost after a few million voice requests... it makes sense.. thanks again

      Author's profile photo Craig Cmehil
      Craig Cmehil
      Blog Post Author

      I thought ABAP was missing so here you go: https://blogs.sap.com/2017/02/11/amazon-echo-and-sap-abap-as-7.5/

      Author's profile photo Ralph Oliveira
      Ralph Oliveira

      Hi Craig, great post!
      I would like to use HCP to also host the Alexa Skill function, currently hosted on AWS Lambda. Anyway to achieve that or we must wait for cloud foundry to be adopted, and therefore NodeJS runtime?

      Months ago I also published a Skill for SAP Business One, you could take a look: https://blogs.sap.com/2016/10/10/b1-assistant-retire-keyboard-sap-business-one-alexa/

      Author's profile photo Santosh Singh
      Santosh Singh

      Hi Craig,

      I am trying to build an Alexa skill using Python which gets the data from an OData service exposed through OData provisioning service. The problem I am running to is how do I authenticate in Python to the HANA trial account. I tried passing basic_authorization in the header but seems like that does not work. Any ideas here.

       

      Author's profile photo Santosh Singh
      Santosh Singh

      I was able to authenticate. The issue was with passing the credentials in the wrong format.

      Author's profile photo Gagan Pareek
      Gagan Pareek

      Craig Cmehil

      Can you share the libraries that you imported in your Alexa code to make it work?

       

      I am getting error message.

      {
        "errorMessage": "Handler 'handler' missing on module 'index'"
      }
      Author's profile photo Craig Cmehil
      Craig Cmehil
      Blog Post Author

      You have to be sure to use the Alexa Skill kit.

      Author's profile photo Former Member
      Former Member

      Hey Craig,

       

      I am working on a similar skill that requests data from xsjs service rather than an XSOData as you do here.

      I have a httprequest call in my index.js file that is very similar to yours. I have the Headers as you do including the Authorization one. And I think the http request URL that I provide is correct as the Amazon Alexa skill test did not raise an endpoint error or anything. However, what does happen, is that my return data text is blank.

      So instead of having the data that I inputted in my table I have a blank. Do you know what could be happening? Maybe you can refer me to some other blogs that work with xsjs service?

       

      Edit: It is also worth mentioning that my xsjs returns a json object. One idea is that I have not correctly set up the anonymus connection? When I remove the Authorization header, the outcome is the same, a blank return data. 

       

       

      -Diana 

      Author's profile photo Craig Cmehil
      Craig Cmehil
      Blog Post Author

      I've found the same thing hence my use of the stored procedure and xsodata. I've not had the time, even though it is on my list to trying and determine why the XSJS is not happening ....

      Author's profile photo Raghavendra AY
      Raghavendra AY

      Hi Craig,

      Great Blog .

      I followed the steps mentioned in the blog, i was able to POST and GET data through  POSTMAN.

      However from AWS Lambda, i get Address not found Error.I have set host and Path name as below,

      I'm trying to POST  directly to the tables via XSO Data without creating a UI5 Interface.

      var host = 'pxxxxxxtrial.hanatrial.ondemand.com/';
      var authStrIoT = 'basic_authorization("USERID", "PASSWORD")';

      Path: Package/myecho.xsodata/DATA

      Any pointers would be helpful.

      Thanks,

      Raghav

      Author's profile photo Craig Cmehil
      Craig Cmehil
      Blog Post Author

      Does it work in POSTMAN when you are not logged in? Like Incognito mode or before even logging into the Cloud Platform system?

      Author's profile photo Raghavendra AY
      Raghavendra AY

      Hi Craig,

      I logged into HCP started database server, logged out from HCP account completely and ran POSTMAN request,i was able to Get the data .

       

      Thanks,

      Raghavendra

      Author's profile photo Former Member
      Former Member

       

      Hi Craig,

      This is an excellent blog! I would like to have some idea from you for the below integration which I am planning

      We have an audio/video smart device that has an inbuilt Wi-Fi chip and runs on Android 6.0. We also have HXE (Hana Express) running on AWS cloud. We want to live stream the audio/video data/signals into our AWS HXE tables and then see it in SAPUI5 application in real-time.

      Is this possible and what functions/configurations/hardware we can use to build this? I just need a very high level guidelines from you.

       

      Author's profile photo Craig Cmehil
      Craig Cmehil
      Blog Post Author

      In general yes it would be possible.

      Author's profile photo Former Member
      Former Member

      Hi Craig,

      thank you for your post.

      I have problems with the HTTPS Post csrf token authorization. I make a https post via node.js in an amazon lambda function triggered by an alexa skill I created. The post request itself is valid and works. But the problem is: I always get a 403 error as a response because the header in the response shows for x-csrf-token: "required" although a valid token is set in the request header with "X-CSRF-Token": token.

      I also created a question in the sap community (https://answers.sap.com/questions/388885/problem-403-error-x-csrf-token-validation-failed-a.html)

      Do you know a possible solution?

      This would be very nice. Thank you for your answer!

      BR

       

      Author's profile photo Craig Cmehil
      Craig Cmehil
      Blog Post Author

      Under XS Admin did you make the appropriate entries?

       

       

      Author's profile photo Former Member
      Former Member

      Do you mean the Admin user of my HANAdatabase? Yes, he has all the rights for posting data.

      Author's profile photo Craig Cmehil
      Craig Cmehil
      Blog Post Author

      No under XS Admin you have to add the different headers to the package.

      Author's profile photo Former Member
      Former Member

      Okay. I am sorry for the late answer.

      Yes, I followed your instructions in the tutorial with regard to the XS Admin role settings. Or do you mean an important detail which is only neccessary for csrf token validation?

       

      Author's profile photo Craig Cmehil
      Craig Cmehil
      Blog Post Author

      In the XS Admin tool for your XSAPP you need to set the CORS and Custom Headers there.

      Author's profile photo Former Member
      Former Member

      In my .xsaccess file I defined this:

      "cors": [{
      "enabled" : true,
      "allowMethods": ["GET","POST","DELETE","PUT"]
      }]

      You mean this?

       

      Author's profile photo Craig Cmehil
      Craig Cmehil
      Blog Post Author

      You need to do it in XS Admin - when you select the root package it's an icon in the menu.

      Author's profile photo Former Member
      Former Member

      Thanks for your comment. Can I find XS Admin within SAP HANA Web-based Development Workbench of the SAP Cloud Platform in Security, Catalog, Traces or Editor?

       

      Author's profile photo Craig Cmehil
      Craig Cmehil
      Blog Post Author

      Web Workbench

      Author's profile photo Former Member
      Former Member

      I could not solve the problem, but I will look further. Nevertheless, thank you very much for assistance!

      Author's profile photo Craig Cmehil
      Craig Cmehil
      Blog Post Author

      Want to get on a call next week? email me first.lastname at sap

      Author's profile photo Former Member
      Former Member

      Hello Craig.

      Thank you for this great Tutorial.

      I am trying to simply connect to a Multi Tenant Database Container.

      i have setup an OData File and activated it, i can also access the Database and POST or GET from it using  the Browser.

       

      const https = require('https');
      var getResponseFromHANA = function(callback){
      var options = {
      method : 'GET',
      url: "https://blexap*****acc***trial.hanatrial.ondemand.com/blexa/SIMPLETABLE.xsodata/SIMPLE_TABLE/",
      headers:{
      'Authorization': authStrIoT,
      'Content-Type': 'application/json;charset=utf-8',
      'Accept': '*/*'
      }
      
      };
      console.log("A");
      https.get(options, function(res){
      var body = '';
      console.log("B");
      res.on('data', function(data){
      body += data;
      });
      
      res.on('end', function(){
      var result = JSON.parse(body);
      var items = result.d.results;
      if(items.length>=0){
      callback(items[0]);
      console.log("C");
      }else{
      console.log("D");
      callback("");
      }
      });
      
      }).on('error', function(e){
      console.log('Error: ' + e);
      });
      };
      
      getResponseFromHANA(function(obj){
      console.log(obj);
      });

      RETURNS:
      A
      Error: Error: connect ECONNREFUSED 127.0.0.1:443

      Author's profile photo Craig Cmehil
      Craig Cmehil
      Blog Post Author

      Are you running it from Lambda or locally? Connection refused usually just means where you are running it from can't find the server/url.