虽然Alexa Conversations并不直接支持上下文的延续,但可以使用sessionAttributes来保存上下文信息,并在后续交互中使用它。
以下是一个使用sessionAttributes来实现上下文传递的示例:
在第一个intent handler中设置sessionAttributes:
//Intent handler code
const speechText = 'Welcome to my skill!';
const repromptText = 'Please tell me how can I help you?';
const sessionAttributes = {
'favoriteColor': 'blue'
};
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(repromptText)
.withShouldEndSession(false)
.addElicitSlotDirective('mySlot', {
'name': 'UserSlotConfirmationIntent',
'confirmationStatus': 'NONE',
'slots': {}
})
.withSessionAttributes(sessionAttributes)
.getResponse();
在后续的intent handler中获取sessionAttributes并使用它来提供上下文信息:
//Intent handler code
const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
const favoriteColor = sessionAttributes['favoriteColor'];
const speechText = `I remember your favorite color is ${favoriteColor}.`;
return handlerInput.responseBuilder
.speak(speechText)
.withShouldEndSession(true)
.getResponse();
该示例中,第一个intent handler设置了favoriteColor作为sessionAttributes的键值,并将其添加到responseBuilder中。在后续intent handler中,获取sessionAttributes并使用它来提供相关的上下文信息。
通过使用sessionAttributes来保存和提供上下文信息,可以在Alexa Conversations中实现上下文的延续。
上一篇:Alexa插槽值未被捕获。
下一篇:Alexa错误地发出了唤醒词。