1

This is my first Alexa skill and I'm just trying to understand basic workflow here.

In the following code if you use FoodPointsIntent it works, but the TestIntent just returns (and says) "triggered TestIntent". I'm confused as they are the same thing, just called from different names.

Index.js

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

const LaunchRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
    },
    handle(handlerInput) {
        const speakOutput = 'Welcome to food points! What food would you like to know about?';
        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt(speakOutput)
            .getResponse();
    }
};
const FoodPointsIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'FoodPointsIntent';
    },
    handle(handlerInput) {
        console.log("THIS.EVENT = " + JSON.stringify(this.event));
        var speakOutput = 'Sorry, there was an error';


        return handlerInput.responseBuilder
            .speak(speakOutput)
            //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
            .getResponse();
    }
};
const TestIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'TestIntent';
    },
    handle(handlerInput) {
        console.log("THIS.EVENT = " + JSON.stringify(this.event));
        var speakOutput = 'Sorry, there was an error';


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

/****************************REMEMBER TO UPDATE THIS*************************/
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();
    }
};

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();
    }
};

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();
    }
};

exports.handler = Alexa.SkillBuilders.custom()
    .addRequestHandlers(
        LaunchRequestHandler,
        FoodPointsIntentHandler,
        HelpIntentHandler,
        CancelAndStopIntentHandler,
        SessionEndedRequestHandler,
        IntentReflectorHandler, // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers
    )
    .addErrorHandlers(
        ErrorHandler,
    )
    .lambda();

IntenSchema.json

{
    "interactionModel": {
        "languageModel": {
            "invocationName": "food points",
            "intents": [
                {
                    "name": "FoodPointsIntent",
                    "slots": [
                        {
                            "name": "FoodQuery",
                            "type": "AMAZON.Food"
                        }
                    ],
                    "samples": ["tell me about {FoodQuery}"]
                },
                {
                    "name": "TestIntent",
                    "slots": [
                        {
                            "name": "TestQuery",
                            "type": "AMAZON.Food"
                        }
                    ],
                    "samples": ["lets try {TestQuery}"]
                },
                {
                    "name": "AMAZON.YesIntent",
                    "slots": [],
                    "samples": []
                },
                {
                    "name": "AMAZON.NoIntent",
                    "slots": [],
                    "samples": []
                },
                {
                    "name": "AMAZON.HelpIntent",
                    "slots": [],
                    "samples": []
                },
                {
                    "name": "AMAZON.StopIntent",
                    "slots": [],
                    "samples": []
                },
                {
                    "name": "AMAZON.CancelIntent",
                    "slots": [],
                    "samples": []
                },
                {
                    "name": "AMAZON.NavigateHomeIntent",
                    "samples": []
                }
            ],
            "types": []
        }
    }
}
schnondle
  • 121
  • 14

1 Answers1

2

You Should Add TestIntentHandler in the exports so that it can be accessible.Exports Code goes as below.

exports.handler = Alexa.SkillBuilders.custom()
    .addRequestHandlers(
        LaunchRequestHandler,
        TestIntentHandler,
        FoodPointsIntentHandler,
        HelpIntentHandler,
        CancelAndStopIntentHandler,
        SessionEndedRequestHandler,
        IntentReflectorHandler, // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers
    )
    .addErrorHandlers(
        ErrorHandler,
    )
    .lambda();

Hope This Works!!

sharan kenam
  • 195
  • 1
  • 11
  • 1
    Only intents included in the exports of the request handlers will be reached in code. The check list is "is it listed in request handlers" and then "can it's canHandle be true". It is also good to note that the intent handlers are tried in the order listed in the export. This is why similar to your ErrorHandler you can have a Fallback or handler that has a more open canHandle at the bottom as a catch all. – AhDev May 12 '20 at 15:44