下面是一个使用Alexa SDK将重定向到设置会话属性的代码示例:
const Alexa = require('ask-sdk-core');
const RedirectIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'RedirectIntent';
},
handle(handlerInput) {
const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
sessionAttributes.redirected = true; // 设置会话属性
return handlerInput.responseBuilder
.speak('您已经被重定向到设置会话属性。')
.getResponse();
}
};
const GetRedirectedValueIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'GetRedirectedValueIntent';
},
handle(handlerInput) {
const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
const redirectedValue = sessionAttributes.redirected ? '是' : '否'; // 获取设置的会话属性
return handlerInput.responseBuilder
.speak(`重定向标志设置为 ${redirectedValue}`)
.getResponse();
}
};
const skillBuilder = Alexa.SkillBuilders.custom();
exports.handler = skillBuilder
.addRequestHandlers(
RedirectIntentHandler,
GetRedirectedValueIntentHandler
)
.lambda();
以上代码示例包括两个意图处理程序,一个用于重定向到设置会话属性,另一个用于获取设置的会话属性的值。
要测试这个代码示例,您可以创建两个自定义的意图,即"RedirectIntent"和"GetRedirectedValueIntent",并在Alexa Developer Console中构建和部署您的技能。然后,您可以与设备上的Alexa进行交互,触发这些意图并查看返回的响应。