如何在AWS Lex中添加多个响应

时间:2018-09-25 08:21:22

标签: python amazon-web-services aws-lambda chatbot amazon-lex

我试图使用AWS Lambda函数为AWS Lex添加多个响应,但是我遇到了这个错误。

我正在尝试
fig 1

但是我被消息困住了

  

发生错误:无效的Lambda响应:从Lambda接收到无效的响应:无法构造Message实例,问题:contentType在[Source:{“ dialogAction”:{“ type”:“ ConfirmIntent”, “ message”:{“ messages”:[{“ contentType”:“ PlainText”,“ group”:1,“ content”:“ Hello”},{“ contentType”:“ PlainText”,“ group”:2,“ content“:” My“},{” contentType“:” PlainText“,” group“:3,” content“:” Friend“}]},” intentName“:” CardsI“,” slots“:{” CardsB“ : 空值}}};行:1,列:252]

在Lambda函数中,我们使用以下代码来打印多个响应

return {
    "dialogAction": {
        "type": "ConfirmIntent",
        "message": {
            "messages": [{
                    "contentType": "PlainText",
                    "group": 1,
                    "content": "Hello"
                },
                {
                    "contentType": "PlainText",
                    "group": 2,
                    "content": "My"
                },
                {
                    "contentType": "PlainText",
                    "group": 3,
                    "content": "Friend"
                }
            ]
        },
        "intentName": "CardsI",
        "slots": {
            "CardsB": ""
        }
    }
}

我们甚至浏览了

之类的文档。
  1. https://docs.aws.amazon.com/lex/latest/dg/lambda-input-response-format.html#using-lambda-response-format

  2. https://docs.aws.amazon.com/lex/latest/dg/howitworks-manage-prompts.html#message-groups

  3. https://docs.aws.amazon.com/lex/latest/dg/context-mgmt.html#special-response

但我们仍然面临问题。有帮助吗?

3 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,文档没有任何建议。但是,检查来自lex的网络响应时,我们可以看到,在有多个消息的情况下,消息数组是作为字符串而不是作为对象传递的。

lambda的响应应采用以下格式。

return {
"dialogAction": {
    "type": "ConfirmIntent",
    "message": {
        "contentType": "CustomPayload",
        "content": "{\"messages\":[{\"type\":\"PlainText\",\"group\":1,\"value\":\"Hello\"},{\"type\":\"PlainText\",\"group\":2,\"value\":\"Hey\"}]}"
    },
    "intentName": "CardsI",
    "slots": {
        "CardsB": null
    }
}

答案 1 :(得分:0)

我建议尝试一些操作:

  1. 由于收到的错误,在contentType对象中包含message
  2. 文档将messages显示为转义的JSON对象,因此请转义引号并将所有messages包裹在{ }中。
  3. 基本的message需要contentTypecontent,因此请尝试在messages中设置转义的JSON对象(content
  4. messages内,使用value代替content

将所有文档放在一起时,文档的确切格式有些含糊。我已经在下面的建议中完成了上述所有操作,但可能只需要一两个即可。因此,请尝试一些组合。

return {
    "dialogAction": {
        "type": "ConfirmIntent",
        "message": {
            "contentType": "PlainText",
            "content":{
                \"messages\": [{
                        \"contentType\": \"PlainText\",
                        \"group\": 1,
                        \"value\": \"Hello\"
                    },
                    {
                        \"contentType\": \"PlainText\",
                        \"group\": 2,
                        \"value\": \"My\"
                    },
                    {
                        \"contentType\": \"PlainText\",
                        \"group\": 3,
                        \"value\": \"Friend\"
                    }
                ]}
        },
        "intentName": "CardsI",
        "slots": {
            "CardsB": null
        }
    }
}

答案 2 :(得分:0)

您可以执行以下操作以将Amazon lambda的多个响应添加到您的Amazon lex。

您可以在lambda函数的响应中添加会话属性。

{
  "dialogState": "Fulfilled",
  "intentName": "myIntent",
  "message": "Hi",
  "messageFormat": "PlainText",
  "responseCard": null,
  "sessionAttributes": {
    "sessionAttribute1": "FirstAttribute",
    "SessionAttribute2": "secondAttribute"
  },
  "slotToElicit": null,
  "slots": {
    "customerId": "1419"
  }
}

这些SessionAttributes可以从lambda函数返回给lex,并且可以根据需要进行自定义。

希望有帮助!

相关问题