Alexa 请求拦截器 - 无法终止会话
Alexa Request Interceptor - Cant kill the session
是否可以根据 RequestInterceptor 内部的某些逻辑让 RequestInterceptor 中止会话?
我编写了一个验证用户有效性的例程。如果有效性检查失败,我想向用户播放消息并中止会话。
我看到发生的情况是 LaunchRequest 仍在运行,即使我尝试终止 RequestInterceptor 中的 seesion
简化版如下
const Alexa = require('ask-sdk-core');
const requestInterceptor = {
var isUserOk = false;
if(!isUserOk){
process(handlerInput) {
return handlerInput.responseBuilder
.speak("You are inside the Request Interceptor")
.withShouldEndSession(true)
.getResponse();
}
}
}
const launchRequestHandler = {
canHandle(handlerInput) {
return (handlerInput.requestEnvelope.session.new &&
handlerInput.requestEnvelope.request.type === 'LaunchRequest');
},
handle(handlerInput) {
return handlerInput.responseBuilder
.speak("Hi, welcome to the test skill. What is your name?")
.reprompt("You did not respond. Please tell me your name")
.getResponse();
}
};
const skillBuilder = Alexa.SkillBuilders.custom();
exports.handler = skillBuilder
.addRequestHandlers(
launchRequestHandler,
)
.addRequestInterceptors(
requestInterceptor
)
.lambda();
谁能告诉我是否可以让 RequestInterceptor 中的逻辑终止会话并阻止 LaunchRequest 处理程序 运行?
谢谢
-BC
它不起作用,因为拦截器不return响应(注意拦截器方法被称为process()
而不是handle()
)在拦截器中,而不是试图关闭会话,使用 handlerInput
设置会话属性,例如validUser
:
let attributes = handlerInput.attributesManager.getSessionAttributes();
attributes.validUser = isUserOk(); //call your user check function here
handlerInput.attributesManager.setSessionAttributes(attributes);
现在创建一个 launchRequestBadUserHandler,其中 canHandle()
您要求会话属性为 false:
const attributes = handlerInput.attributesManager.getSessionAttributes();
const isUserOk = attributes.validUser;
canHandle(handlerInput) {
return (handlerInput.requestEnvelope.session.new &&
handlerInput.requestEnvelope.request.type === 'LaunchRequest' &&
!isUserOk);
在此处理程序中 handle()
函数发送您最初为拦截器计划的响应。不要忘记在 before 中调用此处理程序 addRequestHandlers()
中的原始启动请求处理程序
是否可以根据 RequestInterceptor 内部的某些逻辑让 RequestInterceptor 中止会话?
我编写了一个验证用户有效性的例程。如果有效性检查失败,我想向用户播放消息并中止会话。
我看到发生的情况是 LaunchRequest 仍在运行,即使我尝试终止 RequestInterceptor 中的 seesion
简化版如下
const Alexa = require('ask-sdk-core');
const requestInterceptor = {
var isUserOk = false;
if(!isUserOk){
process(handlerInput) {
return handlerInput.responseBuilder
.speak("You are inside the Request Interceptor")
.withShouldEndSession(true)
.getResponse();
}
}
}
const launchRequestHandler = {
canHandle(handlerInput) {
return (handlerInput.requestEnvelope.session.new &&
handlerInput.requestEnvelope.request.type === 'LaunchRequest');
},
handle(handlerInput) {
return handlerInput.responseBuilder
.speak("Hi, welcome to the test skill. What is your name?")
.reprompt("You did not respond. Please tell me your name")
.getResponse();
}
};
const skillBuilder = Alexa.SkillBuilders.custom();
exports.handler = skillBuilder
.addRequestHandlers(
launchRequestHandler,
)
.addRequestInterceptors(
requestInterceptor
)
.lambda();
谁能告诉我是否可以让 RequestInterceptor 中的逻辑终止会话并阻止 LaunchRequest 处理程序 运行?
谢谢
-BC
它不起作用,因为拦截器不return响应(注意拦截器方法被称为process()
而不是handle()
)在拦截器中,而不是试图关闭会话,使用 handlerInput
设置会话属性,例如validUser
:
let attributes = handlerInput.attributesManager.getSessionAttributes();
attributes.validUser = isUserOk(); //call your user check function here
handlerInput.attributesManager.setSessionAttributes(attributes);
现在创建一个 launchRequestBadUserHandler,其中 canHandle()
您要求会话属性为 false:
const attributes = handlerInput.attributesManager.getSessionAttributes();
const isUserOk = attributes.validUser;
canHandle(handlerInput) {
return (handlerInput.requestEnvelope.session.new &&
handlerInput.requestEnvelope.request.type === 'LaunchRequest' &&
!isUserOk);
在此处理程序中 handle()
函数发送您最初为拦截器计划的响应。不要忘记在 before 中调用此处理程序 addRequestHandlers()