How to ADD Multiple Responses in AWS Lex

只愿长相守 提交于 2019-12-12 18:33:55

问题


I was trying to add Multiple Responses for AWS Lex using AWS Lambda Functions but I am facing this error.

I was trying for

But I am stuck at the message

An error has occurred: Invalid Lambda Response: Received invalid response from Lambda: Can not construct instance of Message, problem: contentType must not be null at [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": null}}}; line: 1, column: 252]

In Lambda Function we are using the following code for printing multiple responses

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": ""
        }
    }
}

We even went through the documentations such as

  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

but we still are facing issue. Any help ?


回答1:


I had the same problem, The docs don't suggest anything. But when inspecting the network response from the lex we can see that in case of multiple messages the array of messages passed in as a string, not as an object.

The response from the lambda should be in the below format.

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
    }
}



回答2:


A few things I suggest trying:

  1. Include contentType in the message object, because of the error you are receiving.
  2. The docs show messages as an escaped JSON object, so escape the quotation marks and wrap all of messages in { }.
  3. The basic message needs contentType and content so try setting the escaped JSON object (messages) in content
  4. Within messages, use value for the text instead of content

The docs are a little vague with the exact format when putting it all together. I've done all of the above in my suggestion below, but one or two may be all that's necessary. So try some combinations.

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
        }
    }
}



回答3:


You can do the following things to add multiple response from Amazon lambda to your amazon lex.

You can add session attribute in the response from your lambda function.

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

These SessionAttributes can be returned from your lambda function to lex and it can be customized as per your need.

Hope it Helps!



来源:https://stackoverflow.com/questions/52493642/how-to-add-multiple-responses-in-aws-lex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!