Mike MacDonagh's Blog

Somewhere in the overlap between software development, process improvement and psychology

How to call a webservice api from Amazon Alexa using Javascript in a Lambda function

This wasn’t trivial and I’ve seen a lot of questions about it online so I thought I’d share how I did this to get Alexa to respond to questions about my kids pocket money 🙂

You can call a URL like this:
(note I’m assuming a JSON response, you’d need to modify for non-JSON)

var http = require('http');

function getWebRequest(url,doWebRequestCallBack) {
    http.get(url, function (res) {
        var webResponseString = '';
        //console.log('Status Code: ' + res.statusCode);

        if (res.statusCode != 200) {
            doWebRequestCallBack(new Error("Non 200 Response"));
        }

        res.on('data', function (data) {
            webResponseString += data;
        });

        res.on('end', function () {
            //console.log('Got some data: '+ webResponseString);            
            var webResponseObject = JSON.parse(webResponseString);            
            if (webResponseObject.error) {
                //console.log("Web error: " + webResponseObject.error.message);
                doWebRequestCallBack(new Error(webResponseObject.error.message));
            } else {
                //console.log("web success");
                doWebRequestCallBack(null, webResponseObject);
            }
        });
    }).on('error', function (e) {
        //console.log("Communications error: " + e.message);
        doWebRequestCallBack(new Error(e.message));
    });
}

You can then use it like this from inside an intent handler:

function getPocketMoney(intent, session, callback) {

    let shouldEndSession = false;
    let repromptText = null;
    let speechOutput = '';
    let cardTitle = '';

    let url = "http://mypocketmoneyservice/getpocketmoney?person=mike";

    getWebRequest(url, function webResonseCallback(err, data) {
        if (err) {
            speechOutput = "Sorry I couldn't connect to the server: " + err;
        } else {
            //something like this
            const balance = data.pocketmoney.person.balance;
            speechOutput = `${person} has £${balance} pocket money.`;            
            callback({}, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));                   
        }
    });
}
Advertisements

4 responses to “How to call a webservice api from Amazon Alexa using Javascript in a Lambda function

  1. Andrew Henson February 11, 2017 at 10:32 am

    Thanks for your tutorial. I need to build a webservice that queries a database.

    Are there any special requirements for a webservice thats being called by an alexa skill?

    I am thinking of having the database and web service hosted at amazon which should improve the response performance.

    Thanks

    Andrew

    • mikemacd February 11, 2017 at 5:07 pm

      No special requirements really. If you’re using your own endpoint then it must be https (and not self-signed), otherwise if your in lambda and using AWS then it’s all done for you 🙂

  2. michael budisch September 10, 2017 at 5:26 pm

    could you pls share the whole skill code ? i still dont get it to work with the snipped 😦

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: