Saturday, 15 April 2017

AWS Lex / Alexa and Lambda : How does the Lex app call my Lambda function? event.request.type is undefined

I'm extremely new to Node.js and the Lex service, but after searching online for 2 hours I'm amazed I cannot find an answer to my problem.

I'm trying to create an Alexa / Lex app which simply prompts for variables used to generate a call to a RESTful URL and then read the string reply from that URL.

For my first effort, I'm simply asking for a text file and prompting for the subdomain. This is my Lambda code:

'use strict';

var https = require('http');

const handlers = {

"LaunchRequest": function () {
    // Launch Request
    console.log(`LAUNCH REQUEST`)
    context.succeed(
      generateResponse(
        buildSpeechletResponse("Welcome to an Alexa Skill, this is running on a deployed lambda function", true),
        {}
      )
    )
},

"GetCustomerVersion": function() {
    var endpoint = "https://"+{customer}+".services.com/sample.txt"
    var body = ""
    https.get(endpoint, (response) => {
      response.on('data', (chunk) => { body += chunk })
      response.on('end', () => {
        context.succeed(
          generateResponse(
            buildSpeechletResponse(`Customer ${customer} has info `+body, true),
            {}
          )
        )
      })
    })
},

'SessionEndedRequest': function () {
    this.emit(':tell', this.t('STOP_MESSAGE'));
}

}

// Helpers
function buildSpeechletResponse (outputText, shouldEndSession) {

  return {
    outputSpeech: {
      type: "PlainText",
      text: outputText
    },
    shouldEndSession: shouldEndSession
  }

}

function generateResponse (speechletResponse, sessionAttributes) {

return { version: "1.0", sessionAttributes: sessionAttributes, response: speechletResponse }

}

exports.handler = (event, context, callback) => {
try {
    if (event.request.type === 'LaunchRequest') {
        handlers['LaunchRequest'](event.request,
            event.session,
            (sessionAttributes, speechletResponse) => {
                callback(null, buildResponse(sessionAttributes, speechletResponse));
            });
    } else if (event.request.type === 'IntentRequest') {
        handlers['GetCustomerVersion'](event.request,
            event.session,
            (sessionAttributes, speechletResponse) => {
                callback(null, buildResponse(sessionAttributes, speechletResponse));
            });
    } else if (event.request.type === 'SessionEndedRequest') {
        handlers['SessionEndedRequest'](event.request, event.session);
        callback();
    }
} catch (err) {
    callback(err);
}
};

I'm pretty sure the way I'm building the endpoint inGetCustomerVersion is wrong, but the real problem is that when I test the lambda function itself I get this error:

{
  "errorMessage": "Cannot read property 'type' of undefined",
  "errorType": "TypeError",
  "stackTrace": [
    "exports.handler.err (/var/task/index.js:86:26)"
  ]
}

I know it's yelling about the event.request.type and either event or event.request is undefined, but I have no clue what causes them to be undefined -- I thought they were being populated by the call itself.

Is there some documentation or tutorial I can read / watch to see more about this? Everything I've seen seems to have a different interface to the Alexa app (where I'm using the Lex portal in AWS), but I wouldn't expect the LAMBDA code to differ.... but maybe that's my confusion? It's an issue of "I don't know what I don't know" so any guidance is welcome.

Thank you!



via Bing

No comments:

Post a Comment