4

I have been following the youtube tutorial : https://www.youtube.com/watch?v=HkMi5xPyz1g&t=1533s

      public Object handleRequest(Map<String,Object> input, Context context) {
    LexRequest lexRequest= LexRequestFactory.createLexRequest(input);
    String orgbotcommand= lexRequest.getCommand()+" "+lexRequest.getOld_variable();

    String content = String.format("command recieved by %s is %s",
            lexRequest.getBotName(),
            orgbotcommand);

    Message message = new Message("Plain text",content);
    DialogueAction dialogueAction = new DialogueAction("Close", "Fulfilled or Failed", message );
    System.out.println(dialogueAction);

    return new LexRespond(dialogueAction);
}

Above is the java code i am using.

it is giving me the desired output while testing with lambda function test events, but when i try to call this lambda function from my lex bot , it throws the error below:

An error has occurred: Invalid Lambda Response: Received invalid response 
from Lambda: Can not construct instance of IntentResponse, problem: The
validated object is null at [Source: {"dialogueAction":
{"type":"Close","fulfillmentState":"Fulfilled or Failed","message":
{"contentType":"Plain text","content":"command recieved by OrgBot is Delete asd"}}}; line: 1, column: 168]

Output in lambda test event is :

{
  "dialogueAction": {
   "type": "Close",
   "fulfillmentState": "Fulfilled or Failed",
   "message": {
   "contentType": "Plain text",
  "content": "command recieved by OrgBotchatbot is delete asd"
   }
 }
}

I am new to Amazan lex and lambda. Please tell me what i am doing wrong

Kanika Agarwal
  • 191
  • 1
  • 4
  • 9

4 Answers4

4

It is probably just your response formatting. Check out the Response Format Docs.

Firstly, contentType needs to be either 'PlainText' or 'SSML'.

So change 'Plain text' to be 'PlainText'

    Message message = new Message("PlainText",content);

Secondly, the fulfillmentState needs to be either 'Fulfulled' or 'Failed'.

So remove 'or Failed' from your DialogueAction line to be:

    DialogueAction dialogueAction = new DialogueAction("Close", "Fulfilled", message );

Thirdly, dialogAction. Lex must be American because it only accepts the response when you spell 'Dialogue' as 'Dialog'. So change what you need to in your code so that the response returns this:

 {  
    "dialogAction": {
        "type": "Close",
        "fulfillmentState": "Fulfilled",
        "message": {
            "contentType": "PlainText",
            "content": "command recieved by OrgBotchatbot is delete asd"
        }
    }
};
Jay A. Little
  • 3,239
  • 2
  • 11
  • 32
3

If you are using Lex v2, the expected response format is different from v1.

See https://docs.aws.amazon.com/lexv2/latest/dg/lambda.html#lambda-response-format

{
    "sessionState": {
        "activeContexts": [
            {
                "name": "string",
                "contextAttributes": {
                    "key": "value"
                },
                "timeToLive": {
                    "timeToLiveInSeconds": number,
                    "turnsToLive": number
                }
            }
        ],
        "sessionAttributes": {
            "string": "string"
        },
        "dialogAction": {
            "slotToElicit": "string",
            "type": "Close | ConfirmIntent | Delegate | ElicitIntent | ElicitSlot"
        },
        "intent": {
            "confirmationState": "Confirmed | Denied | None",
            "name": "string",
            "slots": {
                "string": {
                    "value": {
                        "interpretedValue": "string",
                        "originalValue": "string",
                        "resolvedValues": [
                            "string"
                        ]
                    }
                },
                "string": {
                    "shape": "List",
                    "value": {
                        "originalValue": "string",
                        "interpretedValue": "string",
                        "resolvedValues": [
                            "string"
                        ]
                    },
                    "values": [
                        {
                            "shape": "Scalar",
                            "value": {
                                "originalValue": "string",
                                "interpretedValue": "string",
                                "resolvedValues": [
                                    "string"
                                ]
                            }
                        },
                        {
                            "shape": "Scalar",
                            "value": {
                                "originalValue": "string",
                                "interpretedValue": "string",
                                "resolvedValues": [
                                    "string"
                                ]
                            }
                        }
                    ]
                }
            }
        },
        "state": "Failed | Fulfilled | InProgress | ReadyForFulfillment"
    },
    "messages": [
        {
            "contentType": "CustomPayload | ImageResponseCard | PlainText | SSML",
            "content": "string",
            "imageResponseCard": {
                "title": "string",
                "subtitle": "string",
                "imageUrl": "string",
                "buttons": [
                    {
                        "text": "string",
                        "value": "string"
                    }
                ]
            }
        }
    ],
    "requestAttributes": {
        "string": "string"
    }
}
Karn Ratana
  • 166
  • 4
2

With the new V2, you don't need to fill out all the examples, here is a minimal version of the response in Python (say that you want to send a final message):

return {
    "sessionState": {
        "dialogAction": {
            "type": "Close",
        },
        "intent": {
            "name": "IntentName",
            "state": "Fulfilled"
        }
    },
    "messages": [
        {
            "contentType": "PlainText",
            "content": "Thank you for doing business with me!"
        }
    ]
}
George Ogden
  • 596
  • 9
  • 15
0

The format of your output must follow specific minimal layouts. I use the following two functions to make it easy.

Just call them from any function when you are ready to tell Lex

//SessionAttributes any session variables
//fulfillmentState - 'Fulfilled' or 'Failed' depending on if successful or not
//message - the actual response text you want lex to say/type


function close(sessionAttributes, fulfillmentState, message) {
    return {
        sessionAttributes,
        dialogAction: {
            type: 'Close',
            fulfillmentState,
            message,
        },
    };
}

function delegate(sessionAttributes, slots) {
    return {
        sessionAttributes,
        dialogAction: {
            type: 'Delegate',
            slots,
        },
    };
}