0
votes

I'm very new to Alexa Skills, and just finished connecting Alexa skill Interaction Model and the Lambda function on AWS.

I set up a simple node.js program on AWS lambda developer console, and I'm testing it on the AWS console using their test function. But I can't figure out why this is giving me an error. Also, I created this project from the sample repo - serverlessrepo-alexa-skil-alexaskillskitnodejsfact

Response:
{
  "version": "1.0",
  "response": {
    "outputSpeech": {
      "type": "SSML",
      "ssml": "<speak>Sorry, I had trouble doing what you asked. Please try again.</speak>"
    },
    "reprompt": {
      "outputSpeech": {
        "type": "SSML",
        "ssml": "<speak>Sorry, I had trouble doing what you asked. Please try again.</speak>"
      }
    },
    "shouldEndSession": false
  },
  "userAgent": "ask-node/2.0.0 Node/v8.10.0",
  "sessionAttributes": {}
}

Request ID:
"18b2bd6c-a4c1-4934-bcc8-a78fa2bb3ab1"

Function Logs:
START RequestId: 18b2bd6c-a4c1-4934-bcc8-a78fa2bb3ab1 Version: $LATEST
2019-10-22T20:44:11.141Z    18b2bd6c-a4c1-4934-bcc8-a78fa2bb3ab1    ~~~~ Error handled: TypeError: Alexa.getRequestType is not a function
    at Object.canHandle (/var/task/index.js:8:22)
    at DefaultRequestMapper.<anonymous> (/var/task/node_modules/ask-sdk-core/lib/dispatcher/request/mapper/DefaultRequestMapper.js:74:61)
    at step (/var/task/node_modules/ask-sdk-core/lib/dispatcher/request/mapper/DefaultRequestMapper.js:44:23)
    at Object.next (/var/task/node_modules/ask-sdk-core/lib/dispatcher/request/mapper/DefaultRequestMapper.js:25:53)
    at /var/task/node_modules/ask-sdk-core/lib/dispatcher/request/mapper/DefaultRequestMapper.js:19:71
    at new Promise (<anonymous>)
    at __awaiter (/var/task/node_modules/ask-sdk-core/lib/dispatcher/request/mapper/DefaultRequestMapper.js:15:12)
    at DefaultRequestMapper.getRequestHandlerChain (/var/task/node_modules/ask-sdk-core/lib/dispatcher/request/mapper/DefaultRequestMapper.js:63:16)
    at DefaultRequestDispatcher.<anonymous> (/var/task/node_modules/ask-sdk-core/lib/dispatcher/DefaultRequestDispatcher.js:104:60)
    at step (/var/task/node_modules/ask-sdk-core/lib/dispatcher/DefaultRequestDispatcher.js:44:23)
END RequestId: 18b2bd6c-a4c1-4934-bcc8-a78fa2bb3ab1
REPORT RequestId: 18b2bd6c-a4c1-4934-bcc8-a78fa2bb3ab1  Duration: 376.64 ms Billed Duration: 400 ms Memory Size: 128 MB Max Memory Used: 73 MB  Init Duration: 209.77 ms

Here's the code:

/* eslint-disable  no-console */

const Alexa = require('ask-sdk');

const LaunchRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
    },
    handle(handlerInput) {
        const speakOutput = 'Hello! Welcome to Happy Expresso. Which Expresso type are you in the mood for?';
        const repromptText = 'I like Latte. What do you prefer?';    
        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt(repromptText)
            .getResponse();
    }
};

const CaptureCoffeeIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'CaptureCoffeeIntent';
    },
    handle(handlerInput) {
        const coffee = handlerInput.requestEnvelope.request.intent.slots.coffee.value;

        const speakOutput = `Thanks, I'll find a cafe nearby that has the best ${coffee}!`;
        return handlerInput.responseBuilder
            .speak(speakOutput)
            //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
            .getResponse();
    }
};
const HelpIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.HelpIntent';
    },
    handle(handlerInput) {
        const speakOutput = 'You can say hello to me! How can I help?';

        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt(speakOutput)
            .getResponse();
    }
};
const CancelAndStopIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && (Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.CancelIntent'
                || Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.StopIntent');
    },
    handle(handlerInput) {
        const speakOutput = 'Goodbye!';
        return handlerInput.responseBuilder
            .speak(speakOutput)
            .getResponse();
    }
};
const SessionEndedRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'SessionEndedRequest';
    },
    handle(handlerInput) {
        // Any cleanup logic goes here.
        return handlerInput.responseBuilder.getResponse();
    }
};

// The intent reflector is used for interaction model testing and debugging.
// It will simply repeat the intent the user said. You can create custom handlers
// for your intents by defining them above, then also adding them to the request
// handler chain below.
const IntentReflectorHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest';
    },
    handle(handlerInput) {
        const intentName = Alexa.getIntentName(handlerInput.requestEnvelope);
        const speakOutput = `You just triggered ${intentName}`;

        return handlerInput.responseBuilder
            .speak(speakOutput)
            //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
            .getResponse();
    }
};

// Generic error handling to capture any syntax or routing errors. If you receive an error
// stating the request handler chain is not found, you have not implemented a handler for
// the intent being invoked or included it in the skill builder below.
const ErrorHandler = {
    canHandle() {
        return true;
    },
    handle(handlerInput, error) {
        console.log(`~~~~ Error handled: ${error.stack}`);
        const speakOutput = `Sorry, I had trouble doing what you asked. Please try again.`;

        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt(speakOutput)
            .getResponse();
    }
};

// The SkillBuilder acts as the entry point for your skill, routing all request and response
// payloads to the handlers above. Make sure any new handlers or interceptors you've
// defined are included below. The order matters - they're processed top to bottom.
exports.handler = Alexa.SkillBuilders.custom()
    .addRequestHandlers(
        LaunchRequestHandler,
        CaptureCoffeeIntentHandler,
        HelpIntentHandler,
        CancelAndStopIntentHandler,
        SessionEndedRequestHandler,
        IntentReflectorHandler, // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers
    )
    .addErrorHandlers(
        ErrorHandler,
    )
    .lambda();

It looks like the Alexa.getRequestType is not defined. Is there anything I'm missing in this process?

Thanks!

1

1 Answers

0
votes

I looked at the code and it seems you are including the ask-sdk package instead of the ask-sdk-core.

Also make sure you are using the latest versions of both the package. Here's the package.json file from the Sample Hello World Repository. You can use the code base here as well

Apart from that, I used the Serverless Package for the NodeJS fact skill and it doesn't use Alexa.getRequestType(handlerInput.requestEnvelope) and instead handles the request like handlerInput.requestEnvelope.request.type. Can you confirm which Serverless resource you used?