Lambda Endpoint

In the previous chapter we connected our Skill with a Lambda function. This Lambda function takes the payload request from the Skill invocation and routes it to the appropriate API path. All of the resources have been setup for you via the CloudFormation template deployed, however we still need to finish a few configurations.

Configure Lambda Endpoint

  1. Navigatge to API Gateway
  2. Copy your API Invoke URL from Stages > smart-hotel-api:
    Chapter page
  3. Navigate to Lambda
  4. Paste it into the environment variable section of your SmartHotelBackend Lambda function with api as the key:
    Chapter page

SmartHotelBackend Function

This function is called whenever a user invokes the Smart Hotel Alexa Skill. It will route the command to the appropriate API method (either /set-temp or /set-room). As your skill becomes more complex you would add more resources and/or methods to your API to accommodate additional functionality.

This is already configured for you, however you can look through the code if you wish:

'use strict';
const Alexa = require('alexa-sdk');
const req = require('request');
const APP_ID = undefined;

const SKILL_NAME = 'SmartHotel';
const GET_FACT_MESSAGE = "";
const HELP_MESSAGE = 'Please repeat.';
const HELP_REPROMPT = 'What can I help you with?';
const STOP_MESSAGE = 'Goodbye!';


var AWS = require('aws-sdk');
const apiLink = process.env.api;

const handlers = {
    'LaunchRequest': function () {
        this.emit('SetTemperature');
    },
    'SetTemperature': function () {
        var temp = this.event.request.intent.slots.temp.value;
        var url = apiLink + '/set-temp?temp=' + temp
        req.post(url, function(err, res, body) {
            if(err){
                console.log('error', err);
            } else{
                console.log('success', body);
            }
        });
        const speechOutput = 'Sounds good! I\'ve set your temperature to ' + temp;
        this.response.cardRenderer(SKILL_NAME, speechOutput);
        this.response.speak(speechOutput);
        this.emit(':responseReady');
    },
    'ResetRoom': function () {
        var room = this.event.request.intent.slots.roomNumber.value;
        var postBody = {url: apiLink + '/set-room?room=' + room, 
                        headers: {Authorization: this.event.context.System.apiAccessToken}}
        req.post(postBody, function(err, res, body) {
            if(err){
                console.log('error', err);
            } else{
                console.log('success', body);
            }
        });
        const speechOutput = 'Okay! Resetting room ' + room + ' now.';
        this.response.cardRenderer(SKILL_NAME, speechOutput);
        this.response.speak(speechOutput);
        this.emit(':responseReady');
    },
    'AMAZON.HelpIntent': function () {
        const speechOutput = HELP_MESSAGE;
        const reprompt = HELP_REPROMPT;

        this.response.speak(speechOutput).listen(reprompt);
        this.emit(':responseReady');
    },
    'AMAZON.CancelIntent': function () {
        this.response.speak(STOP_MESSAGE);
        this.emit(':responseReady');
    },
    'AMAZON.StopIntent': function () {
        this.response.speak(STOP_MESSAGE);
        this.emit(':responseReady');
    },
};

exports.handler = function (event, context, callback) {
    const alexa = Alexa.handler(event, context, callback);
    alexa.appId = APP_ID;
    alexa.registerHandlers(handlers);
    alexa.execute();
};