首页 > 解决方案 > Alexa Skill - 如何进行语音密码验证?

问题描述

我是 Alexa 技能开发的新手。我正在尝试创建语音密码验证。

这就是我想要实现的目标:

用户:“开灯”

Alexa:“你的安全密码是什么?”

用户:“6456”(错误的引脚)

Alexa:“身份验证失败!请重试。”

用户:“1234”(正确的引脚)

Alexa:“开灯!”

如果用户第一次告诉了正确的 pin 没有问题,但是如果用户第一次告诉了错误的 pin Alexa 只是说 repromppt 消息并且没有采用新的 pin 值,我将如何获得新的 pin 值并在同一个意图处理程序中再次检查该引脚?

这是我的代码:

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();
        
    }
}

标签: javascriptnode.jsalexaalexa-skills-kitalexa-skill

解决方案


问题是你RemoteControlIntent没有用 pin 话语映射。因此,当您第二次尝试使用实际引脚时,alexa 不知道应该映射哪个意图。

您需要一种再次RemoteControlIntent使用实际引脚执行的方法。

它需要您的意图模式才能正确解决问题,但这是一个可行的解决方案

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();
    }
  }
};

请记住启用自动委派RemoteControlIntent使用您的安全密码是什么?securityPin提示中。


推荐阅读