Sriharan
Sriharan

Reputation: 67

How to ADD Multiple Responses in AWS Lex

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

I was trying for
fig 1

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 ?

Upvotes: 4

Views: 3540

Answers (3)

Veekshith Rai
Veekshith Rai

Reputation: 41

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

Upvotes: 3

vikash singh
vikash singh

Reputation: 1509

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!

Upvotes: 0

Jay A. Little
Jay A. Little

Reputation: 3287

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

Upvotes: 0

Related Questions