0

是否可以让 RequestInterceptor 根据 RequestInterceptor 内部的某些逻辑中止会话?

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

我看到发生的是,即使我尝试杀死 RequestInterceptor 中的 Seesion,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 处理程序运行?

谢谢

-公元前

4

1 回答 1

1

它不起作用,因为拦截器不返回响应(请注意,拦截器方法被调用process()而不是handle())在拦截器中,而不是尝试关闭会话,而是使用handlerInput设置名为 eg 的会话属性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()函数中发送你最初为拦截器计划的响应。不要忘记在原始启动请求处理程序之前调用此处理程序addRequestHandlers()

于 2018-11-18T20:32:11.437 回答