【问题标题】:Alexa SDK V2 StateHandler - Multiple intents sharing the same slot valueAlexa SDK V2 StateHandler - 多个意图共享相同的槽值
【发布时间】:2018-12-06 08:39:52
【问题描述】:

我正在编写一项 Alexa 技能,该技能可以从一个意图中获取年龄,并从不同的意图中获取体重。并且基本上,这两个都是数字类型。

当我尝试输入重量数字时,它被捕获在第一个 Intent 的插槽中。这是我的意图架构。

{
  "intents": [
    {
      "slots": [
        {
          "name": "AGE",
          "type": "AMAZON.NUMBER"
        }
      ],
      "intent": "AgeIntent"
    },
    {
      "slots": [
        {
          "name": "WEIGHT",
          "type": "AMAZON.NUMBER"
        }
      ],
      "intent": "WeightIntent"
    }
  ]
}

我的示例话语是

年龄意图

My age is {AGE}
{AGE}

权重意图

My weight is {WEIGHT}
{WEIGHT}

对话

User : open my test skill
Alexa: what is your age
User: 28
Alexa: what is your weight
User: 68

这里当用户给他的权重输入 68 时,不是匹配 WeightIntent,而是匹配 AgeIntent。我在request.intent.name 中获得了 AgeIntent。

我知道它适用于我的体重是 68;而且我可以让它与 Alexa-SDK-V1 的 StateHandler 功能一起使用,但我使用的是 Alexa-SDK-V2

所以这里的问题是:技能总是从交互模型发送第一个匹配意图(即AgeIntent)的intentName,我希望为我的第二个问题获得第二个匹配的IntentName(即WeightIntent) .

【问题讨论】:

    标签: alexa-skills-kit alexa-sdk-nodejs


    【解决方案1】:

    解决方案很简单,要么您只创建 1 个意图并使用 dialog management 获取您需要的所有插槽值,要么如果您需要将它们设为单独的意图,则在技能会话中使用状态变量并确保您在两个意图中设置和更新状态。你可以这样做:

    const AgeIntentHandler = {
      canHandle(handlerInput) {
        let sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
        
        return handlerInput.requestEnvelope.request.type === 'IntentRequest'
          && handlerInput.requestEnvelope.request.intent.name === 'AgeIntent'
            && sessionAttributes.state !== 'WEIGHT';
      },
      handle(handlerInput) {
      
        let sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
        sessionAttributes.state = 'WEIGHT';
        handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
        
        const speechText = 'Age intent is called!';
    
        return handlerInput.responseBuilder
          .speak(speechText)
          .withSimpleCard('Hello World', speechText)
          .getResponse();
      },
    };
    
    const WeightIntentHandler = {
      canHandle(handlerInput) {
        let sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
        
        return handlerInput.requestEnvelope.request.type === 'IntentRequest'
          && handlerInput.requestEnvelope.request.intent.name === 'WeightIntent'
            && sessionAttributes.state === 'WEIGHT';
      },
      handle(handlerInput) {
      
        let sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
        sessionAttributes.state = '';
        handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
        
        const speechText = 'Weight intent is called!';
    
        return handlerInput.responseBuilder
          .speak(speechText)
          .withSimpleCard('Hello World', speechText)
          .getResponse();
      },
    };

    虽然最好使用 Dialogs 来避免混淆状态,并且以后可以轻松添加更多插槽。

    【讨论】:

    • 感谢您的回答,但我总是收到 handlerInput.requestEnvelope.request.intent.name = 'AgeIntent' 对于第二个问题,如果用户说“68”。 JFI,我将 weightIntent 移动到交互模型的顶部,然后我总是得到 handlerInput.requestEnvelope.request.intent.name = 'WeightIntent'。
    • 在返回 responseBuilder 时是否设置了 withShouldEndSession 标志或添加了 reprompt() 方法?
    猜你喜欢
    • 1970-01-01
    • 2022-01-15
    • 2020-09-21
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 2018-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多