问题
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
https://docs.aws.amazon.com/lex/latest/dg/lambda-input-response-format.html#using-lambda-response-format
https://docs.aws.amazon.com/lex/latest/dg/howitworks-manage-prompts.html#message-groups
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:
- Include
contentType
in themessage
object, because of the error you are receiving. - The docs show
messages
as an escaped JSON object, so escape the quotation marks and wrap all ofmessages
in{ }
. - The basic
message
needscontentType
andcontent
so try setting the escaped JSON object (messages
) incontent
- Within
messages
, usevalue
for the text instead ofcontent
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