This time around, Voice is realGoogle Home andAmaon’sAlexa have started pervading our lives with meaningful experiences. As most of you are aware, Alexa is an intelligent personal assistance device developed by Amazon. Alexa does many useful things such as playing music, operating your bank account, getting latest traffic or weather udpates etc. All you need to provide a command and it does rest.

For example; you say: “Alexa, reorder baby food” and an order can be placed.

Amazon released Alexa voice service API for developers. Now, developers can extend their applications or create new applications that can accept voice commands, execute relevant tasks and respond with a message. In the above example, Alexa takes reorder command and pass the details to an order capture application installed on the device.

There are many articles available online to setup and get started with a basic application.  I have added few references links at the end of the article for the same. 

As part of our enterprise innovation efforts, we are working on supporting StoryPulse, our enterprise-grade assesment platform and learned few early lessons. In this article, I will elaborate on few issues that I faced during application development.

Our intent is for StoryPulse to ask a user various questions and save the response.  The Alexa code essentially captures the responses and pushes the responses to a server through a NodeJS  Lambda function (for a later date)  to interact with a server.

Below is the process: 

  • Download data from a server – Very first when the application is launched, it makes a rest call the server and pull the data.
    • Imported http, request libraries.

                         var http = require(‘http’);

                          var request = require(‘request’);

  • Call to download the data

                   exports.handler = function(event, context, callback) {

                      downloadData(function requestCallback(err) {

                     var alexa = Alexa.handler(event, context);

                      alexa.appId = APP_ID;

                     alexa.resources = languageString;

                     alexa.registerHandlers(x, b, c, d);

                    alexa.execute();

            }

         }

  • Download function –

           var options = {

              host:’hostname’,

              path: ‘service url’,

             method: ‘GET’,

              headers: {

             ‘Content-Type’: ‘application/json’

           }}; 

         http.get(options, function(response) {

         var responseData = ”;

         response.on(‘data’, function(d) {

         responseData += d;

            });

          response.on(‘end’, function() {

                        // Data is ready to use.

            });});

  • Push responses to the server – All responses are constructed in a JSON object and POSTed to the server.

function pushResponses (requestCallback,reff)

{

            var post_data = [];

            post_data.push(userResp);

            var reqData = JSON.stringify(post_data);

            var options = {

            host: ‘hostname’,

            path: ‘service uri’,

            method: ‘POST’,

            headers: {

                ‘Content-Type’: ‘application/json’,

                ‘Content-Length’: reqData.length

            }};

            var post_request = http.request(options, function(res) {

                var body = ”;

                res.on(‘data’, function(chunk)  {

                    body += chunk;           

                });

                res.on(‘end’, function() {

                    requestCallback(“success”,reff);

                });

                res.on(‘error’, function(e) {

                    console.log(“error: ” + e.message);

                });

            });

            // post the data

            post_request.write(reqData);

            post_request.end();

}

This is a plain JavaScript code that pulls and pushes data from and to the server. The only question we need to consider here is when do we initiate the server call?

Every time an intent emits a message, it closes the context, in that case, the server interaction calls may not get completed.

In my case, once all questions are completed and user interaction is completed, we wanted to emit a “Thank You” message to the user and push the data to the server. However, I realized that it does not work if I do in that order or in the reverse order. Also, the intent context is not available in other functions other than the intent function. 

So, here is a trick I followed:

pushResponses (function requestCallback(err,reff) {

            if (err) {

                console.log(‘HTTP Error: request not sent’);

                }

            var speechOutput = “Thank you for your feedback. Good by.”;

            reff.emit(“:tell”, speechOutput);           

        },this);

As you can see, I am passing the intent context to the pushResponses function. Once the data is pushed to the server and receives a response, then the call back function is called with intent context. The call back function will emit the message to the user.

References –