当前位置: 代码迷 >> java >> 解决Alexa自定义技能重现响应
  详细解决方案

解决Alexa自定义技能重现响应

热度:118   发布时间:2023-07-17 20:13:50.0

我正在构建自定义的Alexa技能,可以在其中询问用户的名称并重复输入。 (工作正常)。 现在,下一部分是确认用户名称。

Alexa: "Please confirm your name!"<br>
User:"-"

Alexa: "Please confirm your name!"<br>
User: "No"

Alexa: "Please confirm your name!"<br>
**User: "Yes I confirm"**  
End.

现在,我正在尝试实现上述行为,因为Alexa应该提示"Please confirm your name!" 每10秒一次,直到用户回复

"Yes, I confirm".

我已经检查了API文档,但找不到与此案例相关的任何Intent。

请分享一些信息或解决方案。

恐怕直到用户确认后,Alexa才能每10秒问一次问题。

我想您正在使用confirmSlotDirective来确认用户名。

ConfirmSlotDirective或仅Alexa中的任何确认指令均使用仅同意不同意确认消息的单词。 例如是/否

解:

1 )如果您已经询问过用户名(例如“ John”),则无需再询问。 只需让Alexa说:

Alexa :“你叫约翰,对吗?” 还是“你说约翰?请确认吗?”

用户 :可以说是/否进行确认。

注意 :Alexa仅识别一组有限的国际名称。

2 )获取用户名的更好方法是使用Alexa的 ,因此您不必担心Alexa无法识别姓名,更不用说确认用户名了。

用户没有响应时,每隔10秒重新提示一次,不确定是否可以这样做。

但是我们可以实现是/否部分。 一种方法是使用状态。 在此示例中,我使用模块进行状态管理。

考虑以下名为“ ConfirmationQuestionIntent”的意图。 它将状态设置为“确认名称”。

const ConfirmationQuestionIntentHandler = {
  canHandle(handlerInput) {
    return (
      handlerInput.requestEnvelope.request.type === "IntentRequest" &&
      handlerInput.requestEnvelope.request.intent.name === "ConfirmationQuestionIntent"
    );
  },
  handle(handlerInput) {
    const speechText = "Please confirm your name as 'John'.";
    myCache.set('state','confirm-name');
    return handlerInput.responseBuilder
      .speak(speechText)
      .reprompt(speechText)
      .getResponse();
  }
};

现在,启用/添加两个内置意图,即AMAZON.YesIntent和AMAZON.NoIntent。 考虑下面的AMAZON.NoIntent,在handler函数中。 它检查是否存在名称为“ confirm-name”的状态。 如果存在,它将以“请确认您的姓名为'John'的形式答复”。 如果没有,则使用默认响应进行响应。

const NoBuiltInIntent = {
  canHandle(handlerInput) {
    return (
      handlerInput.requestEnvelope.request.type === "IntentRequest" &&
      handlerInput.requestEnvelope.request.intent.name === "AMAZON.NoIntent"
    );
  },

  handle(handlerInput) {
    const state = myCache.get("state");
    let speechText = "I didn't get this!. Could you please rephrase.";
    if(state === "confirm-name") speechText = "Please confirm your name as 'John'.";
    return handlerInput.responseBuilder
      .speak(speechText)
      .reprompt(speechText)
      .getResponse();
  }
};

考虑下面的AMAZON.YesIntent,在handle函数中,它检查是否存在任何名为“ confirm-name”的状态。 如果是,则它以“感谢确认”进行响应,并从缓存中删除状态。 如果不是,则要求用户重新措辞。

const YesBuiltInIntent = {
  canHandle(handlerInput) {
    return (
      handlerInput.requestEnvelope.request.type === "IntentRequest" &&
      handlerInput.requestEnvelope.request.intent.name === "AMAZON.YesIntent"
    );
  },

  handle(handlerInput) {
    const state = myCache.get("state");
    let speechText = "I didn't get this!. Could you please rephrase.";
    if(state === "confirm-name") speechText = "Thanks for the confirmation.";
    myCache.del('state');
    return handlerInput.responseBuilder
      .speak(speechText)
      .reprompt(speechText)
      .getResponse();
  }
};

因此,您可以使用“状态”来识别用户在哪种情况下做出响应,然后提供正确的响应。