如何使这种alexa技能同时识别两种意图

问题描述 投票:0回答:1

这是我的第一项Alexa技能,我只是想在这里理解基本的工作流程。

在下面的代码中,如果您使用FoodPointsIntent,则可以使用,但是TestIntent仅返回(并说)“触发的TestIntent”。我很困惑,因为它们是同一件事,只是用不同的名称来调用。

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": []
        }
    }
}
javascript json aws-lambda alexa-skills-kit alexa-app
1个回答
0
投票

您应该在导出中添加TestIntentHandler,以便可以访问它。导出代码如下。

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

希望这项工作成功!

© www.soinside.com 2019 - 2024. All rights reserved.