无法构造IntentResponse的实例,验证的对象为null

时间:2018-02-09 11:52:25

标签: java aws-lambda amazon-lex

我一直在关注youtube教程: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);
}

以上是我正在使用的java代码。

在使用lambda函数测试事件进行测试时,它给了我想要的输出,但是当我尝试从我的lex bot调用这个lambda函数时,它会抛出以下错误:

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]

lambda测试事件中的输出是:

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

我是Amazan lex和lambda的新手。请告诉我我做错了什么

3 个答案:

答案 0 :(得分:1)

可能只是你的回复格式。查看Response Format Docs

首先,contentType需要 &#39; PlainText&#39;或者&#39; SSML&#39;。

因此,将'Plain text'更改为'PlainText'

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

其次,fulfillmentState需要 &#39; Fulfulled&#39;或者&#39;失败&#39;。

因此,请从'or Failed'行删除DialogueAction

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

第三,dialogAction。 Lex必须是美国人,因为它只接受你拼写“对话”时的回应。作为对话&#39;。因此,在代码中更改所需内容,以便响应返回:

 {  
    "dialogAction": {
        "type": "Close",
        "fulfillmentState": "Fulfilled",
        "message": {
            "contentType": "PlainText",
            "content": "command recieved by OrgBotchatbot is delete asd"
        }
    }
};

答案 1 :(得分:0)

输出格式必须遵循特定的最小布局。 我使用以下两个功能使其变得简单。

当您准备告诉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,
        },
    };
}

答案 2 :(得分:0)

如果您使用的是 Lex v2,则预期的响应格式与 v1 不同。

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