Amazon lex根据上一个广告位的解析值跳过广告位

问题描述 投票:0回答:1

我的lex lex机器人意图中有7个位置。

{
  slot1: null,
  slot2: null,
  slot3: null,
  slot4: null,
  slot5: null,
  slot6: null,
  slot7: null
}

slot2具有2个分辨率值。 slot3具有3个分辨率值。

我想要实现的是,如果选择,然后在slot3中显示值并完全满足意图。

如果选择,则跳过slot3并继续其他的插槽。

下面是用于初始化和验证代码挂钩的lambda代码,请阅读注释:

exports.handler = async(event) => {
  if (event.currentIntent.slots.slot2 != null) {
    if (event.currentIntent.slots.slot4 == null) {
    // handle slot2 "yes" or "no"
      switch (event.currentIntent.slots.slot2) {
        case 'No':
          return {
            dialogAction: {
              type: "ElicitSlot",
              intentName: event.currentIntent.name,
              slots: event.currentIntent.slots,
              slotToElicit: "slot4"

            }
          };
      }
    }

    // If slot2 value is yes and any one of the value from slot3 is selected then fulfill the intent. Alo if you can tell me that we can add some call to action on slot3 value clicks like mailto: or tel: it will be really helpful.
    if (event.currentIntent.slots.slot2 === "Yes" && event.currentIntent.slots.slot3 != null) {
      return {
        dialogAction: {
          type: "Close",
          fulfillmentState: "Fulfilled",
          message: {
            contentType: "PlainText",
            content: "Fullfillment text"
          }
        }
      }
    }

    // here I am trying to remove slot3 from the intent when slot2 value is "no"
    if (event.currentIntent.slots.slot2 === "No" && event.currentIntent.slots.slot4 != null) {
      delete event.currentIntent.slots.slot3;
    }

  }

// default return dialog action
  return {
    dialogAction: {
      type: "Delegate",
      slots: event.currentIntent.slots
    }
  };
};
amazon-web-services aws-lambda amazon-lex
1个回答
0
投票

看起来不错。

首先,不要删除slot3。基本上,只要您不需要使用插槽,就将其保留或将其设置为null,并确保未在Lex控制台中将其选中即可。如果删除它,则您的响应将不再与Lex期望的所有位置匹配。

您正在使用DialogAction:“ Delegate”,它使Lex可以确定接下来要引发的插槽。因此,在Lex引出slot2之后,我相信Lex只会选择下一个广告位。但是,Lex可以选择下一个required插槽作为相对于非必需插槽的优先级,因此请再次确保未按要求勾选slot3

为了更好地控制对话,您可能根本不想使用Delegate,而是完全按照自己的喜好构建对话逻辑,仅使用ElicitSlot来确保在对话的正确点处引出正确的位置。

如果我简化您的代码,它将看起来像这样:

if slot2 != null
    if slot4 == null
        if slot2 == No 
             elicit slot4

    if slot2 == Yes and slot3 != null
        fulfill intent
else
    delegate

有些事情您可以解决,以这种方式看待可能会为您解决这个问题,但是也许您有扩展它的计划,并且有一个原因,例如为什么您只使用一个开关盒案例(现在?)。

考虑一种没有委托的更直接,受控的方法:

if slot1 == null
    elicit slot1
else                             // slot1 filled
    if slot2 == null
        elicit slot2
    else                         // slot2 filled
        (validate slot2 value)
        if slot2 == Yes
            if slot3 == null
                elicit slot3 
            else                  // slot3 filled
                (validate slot3 value)
                fulfill intent
        if slot2 == No 
             elicit slot4
        ...
© www.soinside.com 2019 - 2024. All rights reserved.