

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 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 函數`lambdaARN`的 ，請在 https：//[https://console.aws.amazon.com/lambda/home](https://console.aws.amazon.com/lambda/home) 開啟AWS Lambda主控台，選取**左側邊欄中的函數**，然後選取要與機器人別名建立關聯的函數。在**函數概觀**的右側，找到**函數 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`、`intentConfirmationSetting`、 `initialResponseSetting`和 `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`和/或 `elicitationCodeHook` 欄位，對應至您要叫用函數的對話階段。若要使用 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) 操作中的 `fulfillmentCodeHook` 欄位。這三種類型的程式碼掛鉤的結構和用途如下所示：

## 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),
}
```
+ 將 `active` Amazon Lex V2 `true` 的欄位變更為 ，以在對話中的該時間點呼叫程式碼掛勾。
+ 將 `enableCodeHookInvocation` Amazon Lex V2 `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
}
```
+ 將 `enableCodeHookInvocation` Amazon Lex V2 `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)
}
```
+ 將 `active` Amazon Lex V2 `true` 的欄位變更為 ，以在對話中的該時間點呼叫程式碼掛勾。
+ 將 `enabled` Amazon Lex V2 `true` 的欄位變更為 ，以允許程式碼掛鉤正常執行。如果您標記 `false`，Amazon Lex V2 就像程式碼掛鉤成功傳回一樣。
+ 使用 `fulfillmentUpdatesSpecification` 欄位來指定在滿足意圖期間顯示來更新使用者的訊息，以及與其相關聯的時間。
+ 使用 `postFulfillmentStatusSpecification` 欄位指定程式碼掛鉤成功、失敗或逾時後發生的訊息和動作。

您可以將 `active`和`enableCodeHookInvocation`/`enabled` 欄位設定為 ，在對話中的下列時間點叫用 Lambda 程式碼掛勾`true`：

## 在初始回應期間
<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 函數，請使用 [CreateSlot](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_CreateSlot.html) 或 [UpdateSlot](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_UpdateSlot.html) 操作的 `valueElicitation` 欄位中的 `slotCaptureSetting` 欄位。`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) 或 [UpdateIntent](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_UpdateIntent.html) 操作`intentConfirmationSetting`的 欄位。`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) 或 [UpdateIntent](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_UpdateIntent.html) 操作中的 `fulfillmentCodeHook` 欄位。`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`操作來重建機器人，以測試函數。