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 not handle()
) In the interceptor, instead of trying to close the session, use the handlerInput
to set a session attribute named e.g. validUser
:
let attributes = handlerInput.attributesManager.getSessionAttributes();
attributes.validUser = isUserOk(); //call your user check function here
handlerInput.attributesManager.setSessionAttributes(attributes);
Now create a launchRequestBadUserHandler where in the canHandle()
you require that session attribute to be false:
const attributes = handlerInput.attributesManager.getSessionAttributes();
const isUserOk = attributes.validUser;
canHandle(handlerInput) {
return (handlerInput.requestEnvelope.session.new &&
handlerInput.requestEnvelope.request.type === 'LaunchRequest' &&
!isUserOk);
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 in addRequestHandlers()