

支援終止通知：2025 年 9 月 15 日， AWS 將停止對 Amazon Lex V1 的支援。2025 年 9 月 15 日之後，您將無法再存取 Amazon Lex V1 主控台或 Amazon Lex V1 資源。如果您使用的是 Amazon Lex V2，請改參閱 [Amazon Lex V2 指南](https://docs.aws.amazon.com/lexv2/latest/dg/what-is.html)。

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

# 練習 3：新增 Lambda 函數 (AWS CLI)
<a name="gs-cli-update-lambda"></a>

新增 Lambda 函數，以驗證使用者輸入並滿足使用者對機器人的意圖。

新增 Lambda 表達式是一個五步驟的程序。

1. 使用 Lambda [AddPermission](https://docs.aws.amazon.com/lambda/latest/dg/API_AddPermission.html) 函數來啟用呼叫 Lambda [調用](https://docs.aws.amazon.com/lambda/latest/dg/lambda-api-permissions-ref.html)操作的`OrderFlowers`意圖。

1. 使用 [GetIntent](API_GetIntent.md)操作從 Amazon Lex 取得意圖。

1. 更新意圖以新增 Lambda 函數。

1. 使用 [PutIntent](API_PutIntent.md)操作將更新的意圖傳回 Amazon Lex。

1. 使用 [GetBot](API_GetBot.md) 和 [PutBot](API_PutBot.md) 操作重建使用該意圖的任何機器人。

若要執行本練習中的命令，您必須知道要執行命令的區域。如需區域的列表，請參閱[模型建置配額](gl-limits.md#gl-limits-model-building)。

如果您在新增`InvokeFunction`許可之前將 Lambda 函數新增至意圖，您會收到下列錯誤訊息：

```
            An error occurred (BadRequestException) when calling the 
            PutIntent operation: Lex is unable to access the Lambda 
            function Lambda function ARN in the context of intent 
            intent ARN.  Please check the resource-based policy on 
            the function.
```

來自 `GetIntent` 操作的回應包含一個名為 `checksum` 的欄位，可識別意圖的特定修訂版本。使用 [PutIntent](API_PutIntent.md) 操作更新意圖時，您必須提供檢查總和值。若不提供，會得到以下錯誤訊息：

```
            An error occurred (PreconditionFailedException) when calling 
            the PutIntent operation: Intent intent name already exists. 
            If you are trying to update intent name you must specify the 
            checksum.
```

本練習使用來自 的 Lambda 函數[練習 1：使用藍圖建立 Amazon Lex 機器人 （主控台）](gs-bp.md)。如需建立 Lambda 函數的說明，請參閱 [步驟 3：建立 Lambda 函數 （主控台）](gs-bp-create-lambda-function.md)。

**注意**  
下列 AWS CLI 範例已針對 Unix、Linux 和 macOS 格式化。用於 Windows 時，請將 `"\$LATEST"` 變更為 `$LATEST`。

**將 Lambda 函數新增至意圖**

1. 在 中 AWS CLI，新增`OrderFlowers`意圖的`InvokeFunction`許可：

   ```
   aws lambda add-permission \
       --region region \
       --function-name OrderFlowersCodeHook \
       --statement-id LexGettingStarted-OrderFlowersBot \
       --action lambda:InvokeFunction \
       --principal lex.amazonaws.com \
       --source-arn "arn:aws:lex:region:account ID:intent:OrderFlowers:*"
       --source-account account ID
   ```

   Lambda 會傳送下列回應：

   ```
   {
       "Statement": "{\"Sid\":\"LexGettingStarted-OrderFlowersBot\",
         \"Resource\":\"arn:aws:lambda:region:account ID:function:OrderFlowersCodeHook\",
         \"Effect\":\"Allow\",
         \"Principal\":{\"Service\":\"lex.amazonaws.com\"},
         \"Action\":[\"lambda:InvokeFunction\"],
         \"Condition\":{\"StringEquals\":
           {\"AWS:SourceAccount\": \"account ID\"},
           {\"AWS:SourceArn\":
             \"arn:aws:lex:region:account ID:intent:OrderFlowers:*\"}}}"
   }
   ```

1. 從 Amazon Lex 取得意圖。Amazon Lex 會將輸出傳送至名為 的檔案**OrderFlowers-V3.json**。

   ```
   aws lex-models get-intent \
       --region region \
       --name OrderFlowers \
       --intent-version "\$LATEST" > OrderFlowers-V3.json
   ```

1. 在文字編輯器中，開啟 **OrderFlowers-V3.json**。

   1. 尋找並刪除 `createdDate`、`lastUpdatedDate` 和 `version` 欄位。

   1. 更新 `fulfillmentActivity` 欄位：

      ```
          "fulfillmentActivity": {
              "type": "CodeHook",
              "codeHook": {
                  "uri": "arn:aws:lambda:region:account ID:function:OrderFlowersCodeHook",
                  "messageVersion": "1.0"
              }
          }
      ```

   1. 儲存檔案。

1. 在 中 AWS CLI，將更新的意圖傳送至 Amazon Lex：

   ```
   aws lex-models put-intent \
       --region region \
       --name OrderFlowers \
       --cli-input-json file://OrderFlowers-V3.json
   ```

更新好意圖之後，重建機器人。

**重建 `OrderFlowersBot` 機器人**

1. 在 中 AWS CLI，取得`OrderFlowersBot`機器人的定義，並將其儲存至檔案：

   ```
   aws lex-models get-bot \
       --region region \
       --name OrderFlowersBot \
       --version-or-alias "\$LATEST" > OrderFlowersBot-V3.json
   ```

1. 在文字編輯器中開啟 **OrderFlowersBot-V3.json**。移除 `createdDate`、`lastUpdatedDate`、`status` 和 `version` 欄位。

1. 在文字編輯器中，將下列行新增至機器人的定義：

   ```
   "processBehavior": "BUILD",
   ```

1. 在 中 AWS CLI，建置機器人的新修訂版：

   ```
   aws lex-models put-bot \
       --region region \
       --name OrderFlowersBot \
       --cli-input-json file://OrderFlowersBot-V3.json
   ```

   伺服器的回應為：

## 後續步驟
<a name="gs-cli-next-exercise-4"></a>

[練習 4：發佈版本 (AWS CLI)](gs-cli-publish.md)