

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

# 使用 API 操作将AWS Lambda函数附加到 Amazon Lex V2 机器人
<a name="lambda-attach-api"></a>

您必须先将 Lambda 函数附加到您的 Amazon Lex V2 机器人别名中，然后才能调用该函数。每个机器人别名只能关联一个 Lambda 函数。使用 API 操作执行以下步骤以关联 Lambda 函数。

如果您要创建新的机器人别名，请使用[CreateBotAlias](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_CreateBotAlias.html)操作来附加 Lambda 函数。要将 Lambda 函数附加到现有机器人别名，请使用操作。[UpdateBotAlias](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_UpdateBotAlias.html)修改 `botAliasLocaleSettings` 字段以包含正确的设置：

```
{
    "botAliasLocaleSettings" : {
        locale: {
            "codeHookSpecification": {
                "lambdaCodeHook": {
                    "codeHookInterfaceVersion": "1.0",
                    "lambdaARN": "arn:aws:lambda:region:account-id:function:function-name"
                }
            },
            "enabled": true
        },
        ...
    }
}
```

1. `botAliasLocaleSettings` 字段映射到一个对象，该对象的键是要在其中关联 Lambda 函数的区域设置。有关支持的区域设置和有效密钥代码的列表，请参阅 [支持的语言和区域设置](how-languages.md#supported-languages)。

1. 要查找 Lambda 函数，请在[https://console.aws.amazon.com/lambda/家](https://console.aws.amazon.com/lambda/home)中打开AWS Lambda控制台，在左侧边栏中选择**函**数，然后选择要与机器人别名关联的函数。`lambdaARN`在**函数概述**的右侧，找到**函数 ARN** 下的 `lambdaARN`。其中应包含区域、账户 ID 和函数名称。

1. 要允许 Amazon Lex V2 为别名调用 Lambda 函数，请将 `enabled` 字段设置为 `true`。

**将 Amazon Lex V2 意图设置为使用 API 操作调用 Lambda 函数**

要在意图期间设置 Lambda 函数调用，请在创建新意图时使用该[CreateIntent](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_CreateIntent.html)操作；如果您在现有意图中调用该函数，则使用该[UpdateIntent](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_UpdateIntent.html)操作。在意图操作中控制 Lambda 函数调用的字段是 `dialogCodeHook`、`initialResponseSetting`、`intentConfirmationSetting` 和 `fulfillmentCodeHook`。

如果在获取插槽期间调用该函数，则在创建新插槽时使用该[CreateSlot](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_CreateSlot.html)操作，或者使用该[UpdateSlot](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_UpdateSlot.html)操作在现有插槽中调用该函数。在槽位操作中控制 Lambda 函数调用的字段是 `valueElicitationSetting` 对象的 `slotCaptureSetting` 字段。

1. 要将 Lambda 对话框代码挂钩设置为在对话的每回合之后运行，请将该`enabled`字段中以下[DialogCodeHookSettings](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_DialogCodeHookSettings.html)对象的`dialogCodeHook`字段设置为：`true`

   ```
   "dialogCodeHook": {
       "enabled": boolean
   }
   ```

1. 或者，您可以将 Lambda 对话框代码挂钩设置为仅在对话中的特定时刻运行，方法是修改结构中与您要调用该函数的对话阶段相对应的`codeHook` and/or `elicitationCodeHook`字段。要使用 Lambda 对话框代码挂钩实现意图，请使用[CreateIntent](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_CreateIntent.html)或`fulfillmentCodeHook`[UpdateIntent](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_UpdateIntent.html)操作中的字段。这三种类型的代码挂钩的结构和用途如下：

## codeHook
<a name="lambda-code-hook"></a>

`codeHook` 字段定义了代码挂钩在对话的给定阶段运行的设置。它是一个具有以下结构的[DialogCodeHookInvocationSetting](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_DialogCodeHookInvocationSetting.html)对象：

```
"codeHook": {
    "active": boolean,
    "enableCodeHookInvocation": boolean,
    "invocationLabel": string,
    "postCodeHookSpecification": [PostDialogCodeHookInvocationSpecification object](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_PostDialogCodeHookInvocationSpecification.html),
}
```
+ 将 Amazon Lex V2 的 `active` 字段更改为 `true`，从而在对话中该取值对应的时刻调用代码挂钩。
+ 将 Amazon Lex V2 的 `enableCodeHookInvocation` 字段更改为 `true`，从而允许代码挂钩正常运行。如果将其标记为 `false`，Amazon Lex V2 则表现为代码挂钩已成功返回。
+ `invocationLabel` 表示调用代码挂钩的对话步骤。
+ 通过 `postCodeHookSpecification` 字段指定代码挂钩成功、失败或超时后的操作和消息。

## elicitationCodeHook
<a name="lambda-elicitation-code-hook"></a>

`elicitationCodeHook` 字段定义了在需要重新引发一个或多个槽位时运行的代码挂钩的设置。如果槽位引发失败或意图确认被拒绝，则可能会出现这种情况。该`elicitationCodeHook`字段是一个具有以下结构的[ElicitationCodeHookInvocationSetting](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_ElicitationCodeHookInvocationSetting.html)对象：

```
"elicitationCodeHook": {
    "enableCodeHookInvocation": boolean,
    "invocationLabel": string
}
```
+ 将 Amazon Lex V2 的 `enableCodeHookInvocation` 字段更改为 `true`，从而允许代码挂钩正常运行。如果将其标记为 `false`，Amazon Lex V2 则表现为代码挂钩已成功返回。
+ `invocationLabel` 表示调用代码挂钩的对话步骤。

## fulfillmentCodeHook
<a name="lambda-fulfillment-code-hook"></a>

`fulfillmentCodeHook` 字段定义了为履行意图而运行的代码挂钩的设置。它映射到以下[FulfillmentCodeHookSettings](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_FulfillmentCodeHookSettings.html)对象：

```
"fulfillmentCodeHook": {
    "active": boolean,
    "enabled": boolean,
    "fulfillmentUpdatesSpecification": [FulfillmentUpdatesSpecification object](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_FulfillmentUpdatesSpecification.html),
    "postFulfillmentStatusSpecification": [PostFulfillmentStatusSpecification object](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_PostFulfillmentStatusSpecification.html)
}
```
+ 将 Amazon Lex V2 的 `active` 字段更改为 `true`，从而在对话中该取值对应的时刻调用代码挂钩。
+ 将 Amazon Lex V2 的 `enabled` 字段更改为 `true`，从而允许代码挂钩正常运行。如果将其标记为 `false`，Amazon Lex V2 则表现为代码挂钩已成功返回。
+ 通过 `fulfillmentUpdatesSpecification` 字段指定在履行意图期间出现用户更新的消息以及与之相关的时间。
+ 通过 `postFulfillmentStatusSpecification` 字段指定代码挂钩成功、失败或超时后的消息和操作。

您可以通过将 `active` 和 `enableCodeHookInvocation`/`enabled` 字段设置为 `true`，在对话的以下时间点调用 Lambda 代码挂钩：

## 初始响应中
<a name="lambda-hook-initial-response"></a>

要在识别意图后在初始响应中调用 Lambda 函数，请使用[CreateIntent](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_CreateIntent.html)或[UpdateIntent](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_UpdateIntent.html)操作`initialResponse`字段中的`codeHook`结构。该`initialResponse`字段映射到以下[InitialResponseSetting](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_InitialResponseSetting.html)对象：

```
"initialResponse": {
    "codeHook": {
        "active": boolean,
        "enableCodeHookInvocation": boolean,
        "invocationLabel": string,
        "postCodeHookSpecification": [PostDialogCodeHookInvocationSpecification object](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_PostDialogCodeHookInvocationSpecification.html),
    },
    "initialResponse": [FulfillmentUpdatesSpecification object](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_FulfillmentUpdatesSpecification.html),
    "nextStep": [PostFulfillmentStatusSpecification object](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_PostFulfillmentStatusSpecification.html),
    "conditional": [ConditionalSpecification object](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_ConditionalSpecification.html)
}
```

## 引发槽位之后或重新引发槽位期间
<a name="lambda-hook-elicit-slot"></a>

要在获得槽值后调用 Lambda 函数，请使用或`slotCaptureSetting`操作`valueElicitation`字段中的字段。[CreateSlot[UpdateSlot](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_UpdateSlot.html)](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_CreateSlot.html)该`slotCaptureSetting`字段映射到以下[SlotCaptureSetting](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_SlotCaptureSetting.html)对象：

```
"slotCaptureSetting": {
    "captureConditional": [ConditionalSpecification object](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_ConditionalSpecification.html),
    "captureNextStep": [DialogState object](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_DialogState.html),
    "captureResponse": [ResponseSpecification object](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_ResponseSpecification.html),
    "codeHook": {
        "active": true,
        "enableCodeHookInvocation": true,
        "invocationLabel": string,
        "postCodeHookSpecification": [PostDialogCodeHookInvocationSpecification object](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_PostDialogCodeHookInvocationSpecification.html),
    },
    "elicitationCodeHook": {
        "enableCodeHookInvocation": boolean,
        "invocationLabel": string
    },
    "failureConditional": [ConditionalSpecification object](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_ConditionalSpecification.html),
    "failureNextStep": [DialogState object](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_DialogState.html),
    "failureResponse": [ResponseSpecification object](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_ResponseSpecification.html)
}
```
+ 要在成功引发槽位后调用 Lambda 函数，请使用 `codeHook` 字段。
+ 要在槽位引发失败且 Amazon Lex V2 尝试重试槽位引发后调用 Lambda 函数，请使用 `elicitationCodeHook` 字段。

## 确认或拒绝意图后
<a name="lambda-hook-confirm-intent"></a>

要在确认意图时调用 Lambda 函数，请使用[CreateIntent](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_CreateIntent.html)或`intentConfirmationSetting`[UpdateIntent](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_UpdateIntent.html)操作的字段。该`intentConfirmation`字段映射到以下[IntentConfirmationSetting](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_IntentConfirmationSetting.html)对象：

```
"intentConfirmationSetting": {
    "active": boolean,
    "codeHook": {
        "active": boolean,
        "enableCodeHookInvocation": boolean,
        "invocationLabel": string,
        "postCodeHookSpecification": [PostDialogCodeHookInvocationSpecification object](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_PostDialogCodeHookInvocationSpecification.html),
    },
    "confirmationConditional": [ConditionalSpecification object](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_ConditionalSpecification.html),
    "confirmationNextStep": [DialogState object](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_DialogState.html),
    "confirmationResponse": [ResponseSpecification object](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_DialResponseSpecificationogState.html),
    "declinationConditional": [ConditionalSpecification object](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_ConditionalSpecification.html),
    "declinationNextStep": [FulfillmentUpdatesSpecification object](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_FulfillmentUpdatesSpecification.html),
    "declinationResponse": [PostFulfillmentStatusSpecification object](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_PostFulfillmentStatusSpecification.html),
    "elicitationCodeHook": {
        "enableCodeHookInvocation": boolean,
        "invocationLabel": string,
    },
    "failureConditional": [ConditionalSpecification object](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_ConditionalSpecification.html),
    "failureNextStep": [DialogState object](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_DialogState.html),
    "failureResponse": [ResponseSpecification object](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_ResponseSpecification.html),
    "promptSpecification": [PromptSpecification object](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_PromptSpecification.html)
}
```
+ 要在用户确认意图及其槽位后调用 Lambda 函数，请使用 `codeHook` 字段。
+ 要在用户拒绝意图确认且 Amazon Lex V2 尝试重试槽位引发后调用 Lambda 函数，请使用 `elicitationCodeHook` 字段。

## 履行意图期间
<a name="lambda-hook-fulfill-intent"></a>

要调用 Lambda 函数来实现意图，请使用[CreateIntent](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_CreateIntent.html)或`fulfillmentCodeHook`[UpdateIntent](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_UpdateIntent.html)操作中的字段。该`fulfillmentCodeHook`字段映射到以下[FulfillmentCodeHookSettings](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_FulfillmentCodeHookSettings.html)对象：

```
{
    "active": boolean,
    "enabled": boolean,
    "fulfillmentUpdatesSpecification": [FulfillmentUpdatesSpecification object](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_FulfillmentUpdatesSpecification.html),
    "postFulfillmentStatusSpecification": [PostFulfillmentStatusSpecification object](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_PostFulfillmentStatusSpecification.html)
}
```

3. 设置调用 Lambda 函数的对话阶段后，请执行 `BuildBotLocale` 操作重建机器人以测试该函数。