嗨,目前正在考虑一个良好的设计来管理Alexa技能中的多个输出链。例如,如果我从一个名为“昨天”的意图和另一个名为“今天”的意图开始。我想将这些信息(是“昨天”我用“或今天”开始链接)传递给NextIntent链。
什么是在意图之间传递信息的最佳方式?
我正在搜索会话属性
这些可以像这样使用
1)我打电话给第一个意图:
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'BesterAutorIntent';
},
handle(handlerInput) {
try {
const speechOutput = "Ich könnte dir sagen wer der beste Autor ist, aber ich muss erst HR fragen ob ich darf";
const attributes = handlerInput.attributesManager.getSessionAttributes();
attributes.kaesebrot = "kaesebrot"
handlerInput.attributesManager.setSessionAttributes(attributes)
return handlerInput.responseBuilder
.speak(speechOutput)
.reprompt()
.withSimpleCard(defaulttext.SKILL_NAME, speechOutput)
.getResponse();
} catch (error) {
console.error(error);
}
},
};
在那里,您可以将名为kaesebrot的属性设置为会话属性:
const attributes = handlerInput.attributesManager.getSessionAttributes();
attributes.kaesebrot = "kaesebrot"
handlerInput.attributesManager.setSessionAttributes(attributes)
稍后在另一个函数中,你可以像这样得到它
let counter = 0;
const NextIntentHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest'
&& request.intent.name === 'AMAZON.NextIntent';
},
handle(handlerInput) {
try {
counter = counter + 1;
const attributes = handlerInput.attributesManager.getSessionAttributes();
speechOutput = counter + " TEST " + attributes.kaesebrot;
return handlerInput.responseBuilder
.speak(speechOutput)
.reprompt()
.withSimpleCard(defaulttext.SKILL_NAME, speechOutput)
.getResponse();
} catch (error) {
console.error(error);
}
},
};
托比的答案很好。
我正在使用sdk v1和代码来保持dynamoDB上的属性是这样的:
exports.handler = function( event, context, callback ) {
const alexa = Alexa.handler( event, context );
alexa.dynamoDBTableName = "alexaTable";
alexa.registerHandlers( handlers );
alexa.execute();
};
...
const handlers = {
...
"AMAZON.NextIntent": function () {
console.log( "AMAZON.NextIntent: " + this.attributes.index );
}
...
}
将在第一次调用lambda函数时创建dynamodb表“alexaTable”,并自动存储和检索属性。在上面的示例中,“索引”是从先前意图中携带的属性。
表模式由键和字段组成。
SessionAttributes是一种在整个意图中保持状态的方法,但是在烤箱中有一个新功能,可以专门链接意图并传递插槽值。
它叫做Intent Chaining,你可以在这里看到更多:https://developer.amazon.com/blogs/alexa/post/9ffdbddb-948a-4eff-8408-7e210282ed38/intent-chaining-for-alexa-skill
Intent链接允许您的技能代码从任何意图开始对话管理,包括LaunchRequest。只要交互模型具有对话模型,您就可以链接到任何自定义意图。
例如,从YesterdayIntent,在响应构建器中,您将执行以下操作:
handlerInput.responseBuilder.addDelegateDirective({
name: 'TodayIntent',
confirmationStatus: 'NONE',
slots: {} // the slot values to pass go here
})