Alexa请求拦截器-无法终止会话

时间:2018-11-18 02:16:15

标签: alexa alexa-skills-kit alexa-skill alexa-slot alexa-app

是否有可能让RequestInterceptor根据RequestInterceptor内部的某种逻辑来中止会话?

我有一个例程来验证用户的有效性。如果有效性检查失败,我想向用户播放一条消息并中止会话。

我看到的是,即使我尝试杀死RequestInterceptor中的连接,LaunchRequest仍然运行

简化版本如下

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中的逻辑杀死Session并阻止LaunchRequest处理程序运行吗?

谢谢

-bc

1 个答案:

答案 0 :(得分:1)

这是行不通的,因为拦截器不会返回响应(请注意,拦截器方法称为process()而不是handle())在拦截器中,不要尝试关闭会话,而应使用{ {1}}设置名为例如的会话属性handlerInput

validUser

现在创建一个launchRequestBadUserHandler,在let attributes = handlerInput.attributesManager.getSessionAttributes(); attributes.validUser = isUserOk(); //call your user check function here handlerInput.attributesManager.setSessionAttributes(attributes); 中,您要求该会话属性为false:

canHandle()

在此处理程序const attributes = handlerInput.attributesManager.getSessionAttributes(); const isUserOk = attributes.validUser; canHandle(handlerInput) { return (handlerInput.requestEnvelope.session.new && handlerInput.requestEnvelope.request.type === 'LaunchRequest' && !isUserOk); 函数中,发送您最初为拦截器计划的响应。不要忘记在handle()

中将您的原始启动请求处理程序称为之前