Alexa Skills Dialog Management:如何在不再次指定其话语的情况下重复上一个意图

Alexa Skills Dialog Management: how to repeat last intent without specifing its utterances again


我正在开发我的第一个 Alexa 技能,我想尝试改进它的对话管理。
我的技能有几个意图:一个是获取室内温度,一个是获取湿度等等。
每个意图都有一个代表我家 floor/room 的插槽,所以对 Alexa 的典型问题是"What's the temperature on the first floor?"

每次执行 Intent 时,它都会将插槽存储在会话属性中,这样我就可以处理这样的对话:

我:“Alexa 一楼的温度是多少?”
Alexa:“一楼的温度是 24 度”
我:“还有湿度?”
Alexa:“一楼的湿度是 50%”


我要实现的下一步是这种类型的对话框:

我:“Alexa 一楼的温度是多少?”
Alexa:“一楼的温度是 24 度”
我:“在二楼?”
Alexa:“二楼的温度是 26 度”

在实践中,我需要在不说出它的话语的情况下启动最后执行的意图。
我正在考虑创建一个新的通用意图,它只接收插槽,然后将请求分派给最后执行的意图。
我可以跟踪最后执行的意图,将其 ID 保存在会话属性中。

有更好的方法吗?
欢迎提出每一个建议,因为我从上周一开始就在开发 Alexa 技能! :-)

非常感谢。

你走在正确的轨道上。需要记住的是,您可以对多个插槽有一个意图,而不是全部都需要它们。

下面介绍了如何为所有这些创建一个 Intent。

样本话语是:

How are things on the {floor}
And on the {floor}
What is the {condition}
What is the {condition} on the {floor}

然后创建“condition”和“floor”插槽类型,用适当的示例值填充它们,例如“temperature”表示条件,“first floor”表示楼层。然后确保将这些插槽类型分配给您意图中的插槽。

那么您的处理程序代码如下所示...

    const conditionIntentHandler = {
      canHandle(handlerInput) {
            return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
                && Alexa.getIntentName(handlerInput.requestEnvelope) === 'conditionIntent';
        },
        handle(handlerInput) {
          var speakOutput = "";
          var condition = "";
          var floor = "";
          const attributesManager = handlerInput.attributesManager;
          const attributes = attributesManager.getSessionAttributes();
          
          if (handlerInput.requestEnvelope.request.intent.slots.condition.hasOwnProperty('value')) {
            condition = handlerInput.requestEnvelope.request.intent.slots.condition.value;
          } else if (attributes.hasOwnProperty('condition')) {
            if(attributes.condition !== "") condition = attributes.condition;
          }    
            
          if (handlerInput.requestEnvelope.request.intent.slots.floor.hasOwnProperty('value')) {
            floor = handlerInput.requestEnvelope.request.intent.slots.floor.value;
          } else if (attributes.hasOwnProperty('floor')) {
            if(attributes.floor !== "") floor = attributes.floor;
          }    
        
          if (floor !== "" && condition === ""){
              speakOutput = "Here's the conditions for the " + floor; 
          }  else if (floor === "" && condition !== ""){
              speakOutput = "Here's the " + condition + " throughout the house";
          } else if (floor !== "" && condition !== ""){
              speakOutput = "Here's the " + condition + " on the " + floor;
          } else {
              speakOutput = "I have no idea what you're saying. Are you okay?"
          }

          attributes.floor = floor;
          attributes.condition = condition;
          attributesManager.setSessionAttributes(attributes);
          
            return handlerInput.responseBuilder
                .speak(speakOutput)
                .reprompt('What else can I tell you?')
                .getResponse();
        }
    };

我没有编写实际代码来显示这些值,只是占位符响应,但您应该明白了。添加更多包含一种或两种槽类型的运营商短语,以使其处理人们可能要求此信息的更多方式。