简体   繁体   中英

Alexa Skill - How to make a voice pin authentication?

I'm new to Alexa skill development. I'm trying to create a voice pin authentication.

This is what I'm trying to achieve:

User: "Turn ON the light"

Alexa: "What is your security pin?"

User: "6456" (Wrong pin)

Alexa: "Authentication failed! Please try again."

User: "1234" (Correct pin)

Alexa: "Turning ON the light!"

If the user tells the correct pin the first time there is no issue, but if the user tells a wrong pin the first time Alexa just says the reprompt message and doesn't take the new pin value, how will I get the new pin value and check that pin once again in the same intent handler?

This is my code:

const RemoteControlIntentHandler = {
canHandle(handlerInput) {
    return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
        && Alexa.getIntentName(handlerInput.requestEnvelope) === 'RemoteControlIntent';
},
handle(handlerInput) {
    var speakOutput = '';
    var userPin = handlerInput.requestEnvelope.request.intent.slots.securityPin.value;
    
    if (userPin !== userSecurityPin) {
        speakOutput = 'Authentication Failed! Please retry!';
        
        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt(speakOutput)
            .getResponse();
            
    } else {
        speakOutput = 'Turning ON the light!';
        
        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt('add a reprompt if you want to keep the session open for the user to respond')
            .getResponse();
        
    }
}

The problem is your RemoteControlIntent is not mapped with pin utterance. so, when you tries with actual pin second time alexa does not know with which intent it should be mapped.

You need a way to again execute RemoteControlIntent with the actual pin.

It would require your intent schema to properly solve the problem, but here is a solution that will work

const RemoteControlIntentHandler = {
  canHandle(handlerInput) {
      return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
      && Alexa.getIntentName(handlerInput.requestEnvelope) === 'RemoteControlIntent';
  },
  handle(handlerInput) {
     var speakOutput = '';
     var userPin =handlerInput.requestEnvelope.request.intent.slots.securityPin.value;
     const request = handlerInput.requestEnvelope.request;
     if (userPin !== userSecurityPin) {
       speakOutput = 'Authentication Failed! Please retry!';
       return handlerInput.responseBuilder
        .speak(speakOutput)
        .addElicitSlotDirective('securityPin') // this line will ask for the pin again against the same intent
        .getResponse();        
     } else {
       speakOutput = 'Turning ON the light!';
       return handlerInput.responseBuilder
             .speak(speakOutput)
             .reprompt('add a reprompt if you want to keep the session open for the 
              user to respond')
             .withShouldEndSession(true)
             .getResponse();
    }
  }
};

Remeber to enable auto delegation for RemoteControlIntent and use What is your security pin? in the securityPin prompt.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM