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

Alexa Skill Lambda Node.js dynamic intents in handler

是否可以在 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' 意图。我该怎么做?

是的,这是可能的。您唯一需要的是创建所有处理程序的辅助函数。然后使用这个辅助函数 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();
};

免责声明:未经测试

编辑:

但我认为覆盖所有处理程序并不是最佳做法。您绝对应该使用内置意图,而不是您的 "rule them all" 意图。因此,你应该在 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;
};