Alexa Skill Lambda Node.js处理程序中的动态意图

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

是否有可能动态填充alexa技能的lambda函数中的意图?例如:

const handlers = {
var intentName = this.event.request.intent.name;
'LaunchRequest': function () {
    this.emit(':ask', welcomeOutput, welcomeReprompt);
},
'AMAZON.HelpIntent': function () {
    speechOutput = 'Placeholder response for AMAZON.HelpIntent.';
    reprompt = '';
    this.emit(':ask', speechOutput, reprompt);
},
'AMAZON.CancelIntent': function () {
    speechOutput = 'Placeholder response for AMAZON.CancelIntent';
    this.emit(':tell', speechOutput);
},
'AMAZON.StopIntent': function () {
    speechOutput = 'Placeholder response for AMAZON.StopIntent.';
    this.emit(':tell', speechOutput);
},
'SessionEndedRequest': function () {
    speechOutput = '';
    //this.emit(':saveState',true);//uncomment to save attributes to db on session end
    this.emit(':tell', speechOutput);
},
'ci_clothing': function () {
    speechOutput = '';

    speechOutput = "Here is the output";
    this.emit(":ask", speechOutput, speechOutput);
},}


exports.handler = (event, context) => {
   const alexa = Alexa.handler(event, context);
   alexa.appId = APP_ID;
   // To enable string internationalization (i18n) features, set a resources object.
   //alexa.resources = languageStrings;
   alexa.registerHandlers(handlers);
   //alexa.dynamoDBTableName = 'DYNAMODB_TABLE_NAME'; //uncomment this line to save attributes to DB
   alexa.execute();};

例如,如果我想动态地拥有'ci_clothing'意图。我该怎么办?

amazon-web-services amazon alexa-skills-kit alexa-skill alexa-voice-service
1个回答
2
投票

对的,这是可能的。您唯一需要的是辅助函数,它可以创建所有处理程序。然后使用这个辅助函数来registerHandlers

像这样的东西:

const getHandlers = (request) => {
    const intentName = request.intent.name;
    return {
        'LaunchRequest': function () {
            this.emit(':ask', welcomeOutput, welcomeReprompt);
        },
        'AMAZON.HelpIntent': function () {
            speechOutput = 'Placeholder response for AMAZON.HelpIntent.';
            reprompt = '';
            this.emit(':ask', speechOutput, reprompt);
        },
        'AMAZON.CancelIntent': function () {
            speechOutput = 'Placeholder response for AMAZON.CancelIntent';
            this.emit(':tell', speechOutput);
        },
        'AMAZON.StopIntent': function () {
            speechOutput = 'Placeholder response for AMAZON.StopIntent.';
            this.emit(':tell', speechOutput);
        },
        'SessionEndedRequest': function () {
            speechOutput = '';
            //this.emit(':saveState',true);//uncomment to save attributes to db on session end
            this.emit(':tell', speechOutput);
        },
        [intentName]: function () {
            speechOutput = '';

            speechOutput = "Here is the output";
            this.emit(":ask", speechOutput, speechOutput);
        },
    };
};

exports.handler = (event, context) => {
    const alexa = Alexa.handler(event, context);
    alexa.appId = APP_ID;
    alexa.registerHandlers(getHandlers(event.request));
    alexa.execute();
};

免责声明:未经测试

编辑:

但我认为覆盖所有处理程序并非最佳做法。你绝对应该使用内置意图,而不是你的“统治所有”意图。因此,您应该在getHandlers内进行一些小改动:

const getHandlers = (request) => {
    const intentName = request.intent.name;
    const handlers = {
        'LaunchRequest': function () {
            this.emit(':ask', welcomeOutput, welcomeReprompt);
        },
        'AMAZON.HelpIntent': function () {
            speechOutput = 'Placeholder response for AMAZON.HelpIntent.';
            reprompt = '';
            this.emit(':ask', speechOutput, reprompt);
        },
        'AMAZON.CancelIntent': function () {
            speechOutput = 'Placeholder response for AMAZON.CancelIntent';
            this.emit(':tell', speechOutput);
        },
        'AMAZON.StopIntent': function () {
            speechOutput = 'Placeholder response for AMAZON.StopIntent.';
            this.emit(':tell', speechOutput);
        },
        'SessionEndedRequest': function () {
            speechOutput = '';
            //this.emit(':saveState',true);//uncomment to save attributes to db on session end
            this.emit(':tell', speechOutput);
        },
    };

    if (!handlers[intentName]) {
        handlers[intentName] = function () {
            speechOutput = '';

            speechOutput = "Here is the output";
            this.emit(":ask", speechOutput, speechOutput);
        };
    }

    return handlers;
};
© www.soinside.com 2019 - 2024. All rights reserved.