是否可以动态地填充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”意图。我该怎么办?
答案 0 :(得分: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;
};