4

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 ?

sid8491
  • 6,622
  • 6
  • 38
  • 64
Sriharan
  • 67
  • 1
  • 5

3 Answers3

3

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

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
        }
    }
}
Jay A. Little
  • 3,239
  • 2
  • 11
  • 32
  • We tried adding but we are still facing the issue as follows "An error has occurred: Invalid Lambda Response: Received invalid response from Lambda: Can not deserialize instance of java.lang.String out of START_OBJECT token at [Source: {"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": ""}}}; line: 1, column: 95]". – Sriharan Sep 25 '18 at 13:18
  • Might be because slot values need to be either null or a true string, so make CardsB null. – Jay A. Little Sep 25 '18 at 14:10
  • I tried but still, I am getting this error "An error has occurred: Invalid Lambda Response: Received invalid response from Lambda: Can not deserialize instance of java.lang.String out of START_OBJECT token at [Source: {"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": "Hello"}}}; line: 1, column: 95]" – Sriharan Sep 26 '18 at 05:41
  • Alright, not a very helpful error. Have you tried different combinations of my suggestions? #1 and #3 may not be the necessary format. The docs are pretty clear about #2 and #4 though. @sid8491 is another experienced Lex bot developer and may be able to help. – Jay A. Little Sep 26 '18 at 08:01
0

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!

vikash singh
  • 1,479
  • 1
  • 19
  • 29
  • we have trying to print multiple responses not store multiple responses. As we know session attribute is only for storing values not display or UI purpose. – Sriharan Sep 27 '18 at 05:58