从Lambda函数访问意图插槽

时间:2019-03-06 21:06:44

标签: python lambda amazon-lex

我创建了一个具有简单lex意图的简单bot。这个词法意图只有一个插槽slotTwo

然后我将其与python lambda函数链接。我想从lambda函数内部访问该插槽的值。

python lambda函数接收参数contextevent。以下链接显示了参数event的结构。 https://docs.aws.amazon.com/lex/latest/dg/lambda-input-response-format.html

I copy it here too:


{
  "currentIntent": {
    "name": "intent-name",
    "slots": {
      "slot name": "value",
      "slot name": "value"
    },
    "slotDetails": {
      "slot name": {
        "resolutions" : [
          { "value": "resolved value" },
          { "value": "resolved value" }
        ],
        "originalValue": "original text"
      },
      "slot name": {
        "resolutions" : [
          { "value": "resolved value" },
          { "value": "resolved value" }
        ],
        "originalValue": "original text"
      }
    },
    "confirmationStatus": "None, Confirmed, or Denied (intent confirmation, if configured)"
  },
  "bot": {
    "name": "bot name",
    "alias": "bot alias",
    "version": "bot version"
  },
  "userId": "User ID specified in the POST request to Amazon Lex.",
  "inputTranscript": "Text used to process the request",
  "invocationSource": "FulfillmentCodeHook or DialogCodeHook",
  "outputDialogMode": "Text or Voice, based on ContentType request header in runtime API request",
  "messageVersion": "1.0",
  "sessionAttributes": { 
     "key": "value",
     "key": "value"
  },
  "requestAttributes": { 
     "key": "value",
     "key": "value"
  }
}

但是,当我打印出该参数的内容时,我只能看到插槽,并且可以直接访问其值,即第一级。

START RequestId: 60a541d8-b3c8-48b0-a7a3-6b3f65d96482 Version: $LATEST
{
"slotTwo": "some text here"
}

从lambda控制台进行的测试工作正常。它能够检索值并继续逻辑。 但是,当我从Lex测试bot时,它不起作用。 知道为什么吗? 非常感谢 酯

1 个答案:

答案 0 :(得分:0)

我的坏。 我错过了lamdda测试中指示的值必须与整个JSON结构相对应的情况。所以现在我将其添加为事件测试:

{
  "currentIntent": {
    "name": "intent-name",
    "slots": {
      "slotTwo": "Hi from the sendmessagetest"
    }
  }
}

然后我以这种方式访问​​lambda中的插槽:

 messagetext = event['currentIntent'] ['slots'] ['slotTwo']

如果发现我的帖子令人困惑,请随时删除。 谢谢大家