

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# AWS LambdaLex V2 的响应格式
<a name="lambda-response-format"></a>

将 Lambda 函数集成到您的 Amazon Lex V2 机器人中的第二步是了解 Lambda 函数响应中的字段，并确定您要操作哪些参数。以下 JSON 对象显示了返回到 Amazon Lex V2 的 Lambda 响应的一般格式：

```
{
    "sessionState": {
        // see 会话状态 for details about the structure
    },
    "messages": [
        {
            "contentType": "CustomPayload | ImageResponseCard | PlainText | SSML",
            "content": string,
            "imageResponseCard": {
                "title": string,
                "subtitle": string,
                "imageUrl": string,
                "buttons": [
                    {
                        "text": string,
                        "value": string
                    },
                    ...
                ]
            }
        },
        ...
    ],
    "requestAttributes": {
        string: string,
        ...
    }
}
```

响应中的每个字段如下所述：

## sessionState
<a name="lambda-response-sessionstate"></a>

您想要返回的用户与您的 Amazon Lex V2 机器人之间的对话状态。有关结构的详细信息，请参阅[会话状态](lambda-common-structures.md#lambda-session-state)。此字段为必填。

## messages
<a name="lambda-response-messages"></a>

Amazon Lex V2 在下一轮对话中返回给客户的消息的列表。如果您提供的 `contentType` 取值是 `PlainText`、`CustomPayload` 或 `SSML`，请在 `content` 字段中写下您要返回给客户的消息。如果您提供的 `contentType` 取值是 `ImageResponseCard`，请在 `imageResponseCard` 字段中提供卡的详细信息。如果您不提供消息，Amazon Lex V2 将使用创建机器人时定义的相应消息。

如果 `dialogAction.type` 的取值是 `ElicitIntent` 或 `ConfirmIntent`，则 `messages` 字段为必填字段。

列表中的每一项都是以下格式的结构，其中包含要返回给用户的消息的相关信息。示例如下：

```
{
    "contentType": "CustomPayload | ImageResponseCard | PlainText | SSML",
    "content": string,
    "imageResponseCard": {
        "title": string,
        "subtitle": string,
        "imageUrl": string,
        "buttons": [
            {
                "text": string,
                "value": string
            },
            ...
        ]
    }
}
```

每个字段的描述如下：
+ **contentType**：要使用的消息类型。

  `CustomPayload`：一个响应字符串，您可以自定义该字符串以包含应用程序的数据或元数据。

  `ImageResponseCard`：带有按钮的图像，客户可以选择。请参阅[ImageResponseCard](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_runtime_ImageResponseCard.html)了解更多信息。

  `PlainText`：纯文本字符串。

  `SSML`：包含语音合成标记语言的字符串，用于自定义音频响应。
+ **content**：要发送给用户的消息。如果消息类型为 `PlainText`、`CustomPayload` 或 `SSML`，则使用此字段。
+ **imageResponseCard**— 包含要向用户显示的响应卡的定义。如果消息类型为 `ImageResponseCard`，则使用此字段。映射到一个包含以下字段的结构：
  + **title**：响应卡的标题。
  + **subtitle**：提示用户选择按钮。
  + **imageUrl**：指向卡的图像的链接。
  + **buttons**：包含按钮相关信息的结构的列表。如果客户选择该按钮，每个结构都包含一个包含要显示文本的 `text` 字段，以及一个包含要发送到 Amazon Lex V2 的值的 `value` 字段。最多可以包含三个按钮。

## requestAttributes
<a name="lambda-response-requestattributes"></a>

一种结构，其中包含针对客户的响应的请求特定的属性。请参阅[设置 Lex V2 机器人的请求属性](context-mgmt-request-attribs.md)了解更多信息。该字段是可选的。

## 响应中的必填字段
<a name="lambda-response-required"></a>

Lambda 响应必须包含至少一个 `sessionState` 对象。该响应提供一个 `dialogAction` 对象并指定 `type` 字段。根据您提供的 `dialogAction` 的 `type`，Lambda 响应可能还包含其他必填字段。这些要求如下所述，并附带最少的工作示例：

### Delegate
<a name="lambda-response-delegate"></a>

**Delegate** 让 Amazon Lex V2 决定下一步操作。没有其他必填字段。

```
{
    "sessionState": {
        "dialogAction": {
            "type": "Delegate"
    }
}
```

### ElicitIntent
<a name="lambda-response-elicitintent"></a>

**ElicitIntent**提示客户表达意图。`messages` 字段中必须包含至少一条消息才能提示引发意图。

```
{
    "sessionState": {
        "dialogAction": {
            "type": "ElicitIntent"
    },
    "messages": [
        {
            "contentType": PlainText,
            "content": "How can I help you?"
        }
    ]
}
```

### ElicitSlot
<a name="lambda-response-elicitslot"></a>

**ElicitSlot**提示客户提供槽位值。`dialogAction` 对象的 `slotToElicit` 字段中必须包含槽位的名称。还必须包含 `sessionState` 对象中 `intent` 的 `name`。

```
{`
    "sessionState": {
        "dialogAction": {
            "slotToElicit": "OriginCity",
            "type": "ElicitSlot"
        },
        "intent": {
            "name": "BookFlight"
        }
    }
}
```

### ConfirmIntent
<a name="lambda-response-confirmintent"></a>

**ConfirmIntent**确认客户的槽位值以及意图是否已准备就绪。必须包含 `sessionState` 对象中 `intent` 的 `name`，以及待确认的 `slots`。`messages` 字段中还必须包含至少一条消息，要求用户确认槽位值。您的消息应提示响应“是”或“否”。如果用户响应“是”，则 Amazon Lex V2 会将意图的 `confirmationState` 设置为 `Confirmed`。如果用户响应“否”，则 Amazon Lex V2 会将意图的 `confirmationState` 设置为 `Denied`。

```
{
    "sessionState": {
        "dialogAction": {
            "type": "ConfirmIntent"
        },
        "intent": {
            "name": "BookFlight",
            "slots": {
                "DepartureDate": {
                    "value": {
                        "originalValue": "tomorrow",
                        "interpretedValue": "2023-05-09",
                        "resolvedValues": [
                            "2023-05-09"
                    ]
                 }
                },
                "DestinationCity": {
                    "value": {
                        "originalValue": "sf",
                        "interpretedValue": "sf",
                        "resolvedValues": [
                            "sf"
                        ]
                    }
                },
                "OriginCity": {
                    "value": {
                        "originalValue": "nyc",
                        "interpretedValue": "nyc",
                        "resolvedValues": [
                            "nyc"
                        ]
                    }
                }
            }
        }
    },
    "messages": [
        {
            "contentType": PlainText,
            "content": "Okay, you want to fly from {OriginCity} to \
            {DestinationCity} on {DepartureDate}. Is that correct?"
        }
    ]
}
```

### Close
<a name="lambda-response-close"></a>

**Close** 结束意图的履行过程，并表示预计用户不会给出进一步的响应。您必须在 `sessionState` 对象中包含 `intent` 的 `name` 和 `state`。兼容的意图状态包括 `Failed`、`Fulfilled` 和 `InProgress`。

```
"sessionState": {
    "dialogAction": {
        "type": "Close"
    },
    "intent": {
        "name": "BookFlight",
        "state": "Failed | Fulfilled | InProgress"
    }
}
```