Is it possible to have a RequestInterceptor abort the Session based on some logic inside the RequestInterceptor?
I have a routine coded that verifies the validity of a User. If the validity check fails, I want to play a message to the USer and abort the session.
What I see happening is that the LaunchRequest still runs even though I try and kill the seesion in the RequestInterceptor
A simplified version is as follows
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();
Can anyone tell me if it is possible to have the logic in the RequestInterceptor kill off the Session and prevent the LaunchRequest handler from running?
Thanks
-bc
It doesn't work because interceptors do not return responses (note that the interceptor method is called
process()
and nothandle()
) In the interceptor, instead of trying to close the session, use thehandlerInput
to set a session attribute named e.g.validUser
:Now create a launchRequestBadUserHandler where in the
canHandle()
you require that session attribute to be false:In this handler
handle()
function send the response you originally planned for the interceptor. Don't forget to call this handler before your original launch request handler inaddRequestHandlers()