練習 3:新增 Lambda 函數 (AWS CLI) - Amazon Lex V1

如果您使用的是 Amazon Lex V2,請參閱 Amazon Lex V2 指南

 

如果您使用的是 Amazon Lex V1,我們建議您將機器人升級到 Amazon Lex V2。我們不再向 V1 添加新功能,強烈建議所有新機器人使用 V2。

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

練習 3:新增 Lambda 函數 (AWS CLI)

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

新增 Lambda 表達式包含五個步驟。

  1. 使用 LambdaAddPermission函數來啟用OrderFlowers意圖調用 Lambda呼叫operation.

  2. 使用GetIntent操作從 Amazon Lex 取得意圖。

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

  4. 使用PutIntent操作將更新的意圖傳回 Amazon Lex。

  5. 使用 GetBotPutBot 操作重建使用該意圖的任何機器人。

若要執行本練習中的命令,您必須知道要執行命令的區域。如需區域的列表,請參閱 模型建置配額

如 Lambda 您在新增InvokeFunction權限,您會得到以下錯誤訊息:

            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 操作更新意圖時,您必須提供檢查總和值。若不提供,會得到以下錯誤訊息:

            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 機器人。如需有關建立 Lambda 函數的指示,請參步驟 3:建立 Lambda 函數

注意

以下 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:*\"}}}"
    }
  2. 從 Amazon Lex 取得意圖。Amazon Lex 會將輸出傳送到一個稱為OrderFlowers-V3.json

    aws lex-models get-intent \ --region region \ --name OrderFlowers \ --intent-version "\$LATEST" > OrderFlowers-V3.json
  3. 在文字編輯器中,開啟 OrderFlowers-V3.json

    1. 尋找並刪除 createdDatelastUpdatedDateversion 欄位。

    2. 更新 fulfillmentActivity 欄位:

      "fulfillmentActivity": { "type": "CodeHook", "codeHook": { "uri": "arn:aws:lambda:region:account ID:function:OrderFlowersCodeHook", "messageVersion": "1.0" } }
    3. 儲存檔案。

  4. 在 中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
  2. 在文字編輯器中開啟 OrderFlowersBot-V3.json。移除 createdDatelastUpdatedDatestatusversion 欄位。

  3. 在文字編輯器中,將下列行新增至機器人的定義:

    "processBehavior": "BUILD",
  4. 在 AWS CLI 中,建立機器人的新修訂版本:

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

    伺服器的回應為:

    {
        "status": "READY", 
        "intents": [
            {
                "intentVersion": "$LATEST", 
                "intentName": "OrderFlowers"
            }
        ], 
        "name": "OrderFlowersBot", 
        "locale": "en-US", 
        "checksum": "checksum", 
        "abortStatement": {
            "messages": [
                {
                    "content": "Sorry, I'm not able to assist at this time", 
                    "contentType": "PlainText"
                }
            ]
        }, 
        "version": "$LATEST", 
        "lastUpdatedDate": timestamp, 
        "createdDate": timestamp, 
        "clarificationPrompt": {
            "maxAttempts": 2, 
            "messages": [
                {
                    "content": "I didn't understand you, what would you like to do?", 
                    "contentType": "PlainText"
                }
            ]
        }, 
        "voiceId": "Salli", 
        "childDirected": false, 
        "idleSessionTTLInSeconds": 600, 
        "description": "Bot to order flowers on the behalf of a user"
    }
    

後續步驟

練習 4:發佈版本 (AWS CLI)