

支援終止通知：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)。

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

# 步驟 5 (選用)：檢閱資訊流程的詳細資訊 (主控台)
<a name="gs-bp-details-after-lambda"></a>

本節說明用戶端與 Amazon Lex 之間每個使用者輸入的資訊流程，包括 Lambda 函數的整合。

**注意**  
本節假設用戶端使用`PostText`執行時間 API 將請求傳送至 Amazon Lex，並相應地顯示請求和回應詳細資訊。如需用戶端與用戶端使用 `PostContent` API 的 Amazon Lex 之間資訊流程的範例，請參閱 [步驟 2a (選用)：檢閱口語化資訊流程的詳細資訊 (主控台)](gs-bp-details-postcontent-flow.md)。

如需 `PostText` 執行時間 API 的詳細資訊及關於以下步驟所示的請求與回應的更多細節，請參閱 [PostText](API_runtime_PostText.md)。

1. 使用者：我想要訂花。

   1. 用戶端 (主控台) 傳送以下 [PostText](API_runtime_PostText.md) 請求給 Amazon Lex：

      ```
      POST /bot/OrderFlowers/alias/$LATEST/user/ignw84y6seypre4xly5rimopuri2xwnd/text
      "Content-Type":"application/json"
      "Content-Encoding":"amz-1.0"
      
      {
          "inputText": "I would like to order some flowers",
          "sessionAttributes": {}
      }
      ```

      請求 URI 和內文都會提供資訊給 Amazon Lex：
      + 請求 URI – 提供機器人名稱 (`OrderFlowers`)、機器人別名 (`$LATEST`) 和使用者名稱 （識別使用者的隨機字串）。末尾的 `text` 表示其為 `PostText` API 請求 (而非 `PostContent`)。
      + 請求本文 – 包含使用者輸入 (`inputText`) 和空的 `sessionAttributes`。用戶端發出第一次請求時，沒有工作階段屬性。稍後將由 Lambda 函數起始這些屬性。

   1. 從 `inputText`，Amazon Lex 會偵測意圖 (`OrderFlowers`)。此意圖是以 Lambda 函數設定為程式碼掛勾，用於使用者資料初始化和驗證。因此，Amazon Lex 透過將下列資訊做為事件資料傳遞，叫用該 Lambda 函數：

      ```
      {
          "messageVersion": "1.0",
          "invocationSource": "DialogCodeHook",
          "userId": "ignw84y6seypre4xly5rimopuri2xwnd",
          "sessionAttributes": {},
          "bot": {
              "name": "OrderFlowers",
              "alias": null,
              "version": "$LATEST"
          },
          "outputDialogMode": "Text",
          "currentIntent": {
              "name": "OrderFlowers",
              "slots": {
                  "PickupTime": null,
                  "FlowerType": null,
                  "PickupDate": null
              },
              "confirmationStatus": "None"
          }
      }
      ```

      如需詳細資訊，請參閱[輸入事件格式](lambda-input-response-format.md#using-lambda-input-event-format)。

      除了用戶端傳送的資訊之外，Amazon Lex 還包含下列其他資料：
      + `messageVersion` – 目前 Amazon Lex 僅支援 1.0 版本。
      + `invocationSource` – 指出 Lambda 函數調用的目的。在本例中，目的是執行使用者資料初始化和驗證。目前，Amazon Lex 知道使用者尚未提供所有槽資料以滿足意圖。
      + `currentIntent` 資訊 – 所有槽值均設定為 null。

   1. 此時，所有槽值都是 null。Lambda 函數不需要驗證。Lambda 函數會傳回下列回應給 Amazon Lex：

      ```
      {
          "sessionAttributes": {},
          "dialogAction": {
              "type": "Delegate",
              "slots": {
                  "PickupTime": null,
                  "FlowerType": null,
                  "PickupDate": null
              }
          }
      }
      ```

      如需回應格式的相關資訊，請參閱[回應格式](lambda-input-response-format.md#using-lambda-response-format)。

      注意下列事項：
      + `dialogAction.type` – 透過將此值設定為 `Delegate`，Lambda 函數會將決定下一個動作過程的責任委派給 Amazon Lex。
**注意**  
如果 Lambda 函數在使用者資料驗證中偵測到任何內容，它會指示 Amazon Lex 接下來要做什麼，如以下幾個步驟所示。

   1. 根據 `dialogAction.type`，Amazon Lex 會決定下一個動作。由於沒有任何槽獲得填充，其決定引出 `FlowerType` 槽的值。服務將選取該槽的其中一個值引出提示「您想要訂購哪一種花？」，然後傳回以下回應給用戶端：  
![\[\]](http://docs.aws.amazon.com/zh_tw/lex/latest/dg/images/gs-1-details-10.png)

      用戶端顯示回應中的訊息。

1. 使用者：玫瑰

   1. 用戶端會將下列[PostText](API_runtime_PostText.md)請求傳送至 Amazon Lex：

      ```
      POST /bot/OrderFlowers/alias/$LATEST/user/ignw84y6seypre4xly5rimopuri2xwnd/text
      "Content-Type":"application/json"
      "Content-Encoding":"amz-1.0"
      
      {
          "inputText": "roses",
          "sessionAttributes": {}
      }
      ```

      請求內文中的 `inputText` 會提供使用者輸入。`sessionAttributes` 仍為空白。

   1. Amazon Lex 會先在目前意圖的內容`inputText`中解譯 。服務會記住其已向具體使用者詢問過有關 `FlowerType` 槽的資訊。它會更新目前意圖中的槽值，並使用下列事件資料叫用 Lambda 函數：

      ```
      {
          "messageVersion": "1.0",
          "invocationSource": "DialogCodeHook",
          "userId": "ignw84y6seypre4xly5rimopuri2xwnd",
          "sessionAttributes": {},
          "bot": {
              "name": "OrderFlowers",
              "alias": null,
              "version": "$LATEST"
          },
          "outputDialogMode": "Text",
          "currentIntent": {
              "name": "OrderFlowers",
              "slots": {
                  "PickupTime": null,
                  "FlowerType": "roses",
                  "PickupDate": null
              },
              "confirmationStatus": "None"
          }
      }
      ```

      注意下列事項：
      + `invocationSource` – 仍為 `DialogCodeHook` (僅驗證使用者資料)。
      + `currentIntent.slots` – Amazon Lex 已將`FlowerType`槽更新為玫瑰。

   1. 根據 `invocationSource`的值`DialogCodeHook`，Lambda 函數會執行使用者資料驗證。它會將 `roses`辨識為有效的槽值 （並設定為工作階段屬性）`Price`，並將下列回應傳回給 Amazon Lex。

      ```
      {
          "sessionAttributes": {
              "Price": 25
          },
          "dialogAction": {
              "type": "Delegate",
              "slots": {
                  "PickupTime": null,
                  "FlowerType": "roses",
                  "PickupDate": null
              }
          }
      }
      ```

      注意下列事項：
      + `sessionAttributes` – Lambda 函數已新增 `Price`（玫瑰的） 做為工作階段屬性。
      + `dialogAction.type` – 設定為 `Delegate`。使用者資料有效，因此 Lambda 函數會指示 Amazon Lex 選擇下一個動作。

       

   1. 根據 `dialogAction.type`，Amazon Lex 選擇下一個動作。Amazon Lex 知道它需要更多槽資料，因此會根據意圖組態，挑選具有最高優先順序的下一個未填充槽 (`PickupDate`)。Amazon Lex 會根據意圖組態選取其中一個引出值提示訊息：「您希望在哪一天撿起玫瑰？」，針對此槽傳送下列回應回用戶端：  
![\[\]](http://docs.aws.amazon.com/zh_tw/lex/latest/dg/images/gs-1-details-20.png)

      用戶端將顯示回應中的訊息 –「您想要在哪一天拿取玫瑰？」

1. 使用者：明天

   1. 用戶端會將下列[PostText](API_runtime_PostText.md)請求傳送至 Amazon Lex：

      ```
      POST /bot/OrderFlowers/alias/$LATEST/user/ignw84y6seypre4xly5rimopuri2xwnd/text
      "Content-Type":"application/json"
      "Content-Encoding":"amz-1.0"
      
      {
          "inputText": "tomorrow",
          "sessionAttributes": {
              "Price": "25"
          }
      }
      ```

      請求內文中的 `inputText` 會提供使用者輸入，且用戶端會將工作階段屬性傳回給服務。

   1. Amazon Lex 會記住內容，也就是它正在為`PickupDate`插槽引出資料。在此情況下，服務知道 `inputText` 值是用於 `PickupDate` 槽。然後，Amazon Lex 會傳送下列事件來叫用 Lambda 函數：

      ```
      {
          "messageVersion": "1.0",
          "invocationSource": "DialogCodeHook",
          "userId": "ignw84y6seypre4xly5rimopuri2xwnd",
          "sessionAttributes": {
              "Price": "25"
          },
          "bot": {
              "name": "OrderFlowersCustomWithRespCard",
              "alias": null,
              "version": "$LATEST"
          },
          "outputDialogMode": "Text",
          "currentIntent": {
              "name": "OrderFlowers",
              "slots": {
                  "PickupTime": null,
                  "FlowerType": "roses",
                  "PickupDate": "2017-01-05"
              },
              "confirmationStatus": "None"
          }
      }
      ```

      Amazon Lex 已透過設定 `PickupDate`值`currentIntent.slots`來更新 。另請注意，服務會將 `sessionAttributes` 傳遞至 Lambda 函數。

   1. 根據 `invocationSource`的值`DialogCodeHook`，Lambda 函數會執行使用者資料驗證。它會辨識`PickupDate`槽值是否有效，並傳回下列回應給 Amazon Lex：

      ```
      {
          "sessionAttributes": {
              "Price": 25
          },
          "dialogAction": {
              "type": "Delegate",
              "slots": {
                  "PickupTime": null,
                  "FlowerType": "roses",
                  "PickupDate": "2017-01-05"
              }
          }
      }
      ```

      注意下列事項：
      + `sessionAttributes` – 未改變。
      + `dialogAction.type` – 設定為 `Delegate`。使用者資料有效，Lambda 函數會指示 Amazon Lex 選擇下一個動作。

   1. 根據 `dialogAction.type`，Amazon Lex 選擇下一個動作。Amazon Lex 知道它需要更多槽資料，因此會根據意圖組態，挑選具有最高優先順序的下一個未填充槽 (`PickupTime`)。Amazon Lex 會根據意圖組態為此槽選取其中一個提示訊息 (「在 2017-01-05 的什麼時間交付玫瑰？」)，並將下列回應傳回給用戶端：  
![\[\]](http://docs.aws.amazon.com/zh_tw/lex/latest/dg/images/gs-1-details-30.png)

      用戶端會在回應中顯示訊息 –「在 2017-01-05 的什麼時間交付玫瑰？」

1. 使用者：下午 4 點

   1. 用戶端會將下列[PostText](API_runtime_PostText.md)請求傳送至 Amazon Lex：

      ```
      POST /bot/OrderFlowers/alias/$LATEST/user/ignw84y6seypre4xly5rimopuri2xwnd/text
      "Content-Type":"application/json"
      "Content-Encoding":"amz-1.0"
      
      {
          "inputText": "4 pm",
          "sessionAttributes": {
              "Price": "25"
          }
      }
      ```

      請求內文中的 `inputText` 會提供使用者輸入。用戶端將在請求中傳遞 `sessionAttributes`。

   1. Amazon Lex 了解內容。其明白這是稍早引出的 `PickupTime` 槽的資料。在這種情況下，它會知道 `inputText`值適用於`PickupTime`插槽。然後，Amazon Lex 會傳送下列事件來叫用 Lambda 函數：

      ```
      {
          "messageVersion": "1.0",
          "invocationSource": "DialogCodeHook",
          "userId": "ignw84y6seypre4xly5rimopuri2xwnd",
          "sessionAttributes": {
              "Price": "25"
          },
          "bot": {
              "name": "OrderFlowersCustomWithRespCard",
              "alias": null,
              "version": "$LATEST"
          },
          "outputDialogMode": "Text",
          "currentIntent": {
              "name": "OrderFlowers",
              "slots": {
                  "PickupTime": "16:00",
                  "FlowerType": "roses",
                  "PickupDate": "2017-01-05"
              },
              "confirmationStatus": "None"
          }
      }
      ```

      Amazon Lex 已透過設定 `PickupTime`值`currentIntent.slots`來更新 。

   1. 根據 `invocationSource`的值`DialogCodeHook`，Lambda 函數會執行使用者資料驗證。它會辨識`PickupDate`槽值是否有效，並傳回下列回應給 Amazon Lex。

      ```
      {
          "sessionAttributes": {
              "Price": 25
          },
          "dialogAction": {
              "type": "Delegate",
              "slots": {
                  "PickupTime": "16:00",
                  "FlowerType": "roses",
                  "PickupDate": "2017-01-05"
              }
          }
      }
      ```

      注意下列事項：
      + `sessionAttributes` – 工作階段屬性未改變。
      + `dialogAction.type` – 設定為 `Delegate`。使用者資料有效，因此 Lambda 函數會指示 Amazon Lex 選擇下一個動作。

   1. 目前，Amazon Lex 知道它擁有所有槽資料。該意圖設定了一則確認提示。因此，Amazon Lex 會傳送下列回應給使用者，在滿足意圖之前要求確認：  
![\[\]](http://docs.aws.amazon.com/zh_tw/lex/latest/dg/images/gs-1-details-45.png)

      用戶端將顯示回應中的訊息並等待使用者回應。

1. 使用者：好

   1. 用戶端會將下列[PostText](API_runtime_PostText.md)請求傳送至 Amazon Lex：

      ```
      POST /bot/OrderFlowers/alias/$LATEST/user/ignw84y6seypre4xly5rimopuri2xwnd/text
      "Content-Type":"application/json"
      "Content-Encoding":"amz-1.0"
      
      {
          "inputText": "yes",
          "sessionAttributes": {
              "Price": "25"
          }
      }
      ```

   1. Amazon Lex 會在確認目前意圖`inputText`的情況下解譯 。Amazon Lex 了解使用者想要繼續訂單。這次 Amazon Lex 會傳送下列事件來叫用 Lambda 函數以滿足意圖，這會在傳送至 Lambda 函數`invocationSource``FulfillmentCodeHook`時將 設定為 。Amazon Lex 也會將 `confirmationStatus`設定為 `Confirmed`。

      ```
      {
          "messageVersion": "1.0",
          "invocationSource": "FulfillmentCodeHook",
          "userId": "ignw84y6seypre4xly5rimopuri2xwnd",
          "sessionAttributes": {
              "Price": "25"
          },
          "bot": {
              "name": "OrderFlowersCustomWithRespCard",
              "alias": null,
              "version": "$LATEST"
          },
          "outputDialogMode": "Text",
          "currentIntent": {
              "name": "OrderFlowers",
              "slots": {
                  "PickupTime": "16:00",
                  "FlowerType": "roses",
                  "PickupDate": "2017-01-05"
              },
              "confirmationStatus": "Confirmed"
          }
      }
      ```

      注意下列事項：
      + `invocationSource` – 這次 Amazon Lex 將此值設定為 `FulfillmentCodeHook`，指示 Lambda 函數實現意圖。
      + `confirmationStatus` – 設定為 `Confirmed`。

   1. 這次，Lambda 函數會滿足`OrderFlowers`意圖，並傳回下列回應：

      ```
      {
          "sessionAttributes": {
              "Price": "25"
          },
          "dialogAction": {
              "type": "Close",
              "fulfillmentState": "Fulfilled",
              "message": {
                  "contentType": "PlainText",
                  "content": "Thanks, your order for roses has been placed and will be ready for pickup by 16:00 on 2017-01-05"
              }
          }
      }
      ```

      注意下列事項：
      + 設定 `dialogAction.type`- Lambda 函數將此值設定為 `Close`，指示 Amazon Lex 不預期使用者回應。
      + `dialogAction.fulfillmentState` – 設定為 Fulfilled 且附上相應的 `message` 以傳達給使用者。

   1. Amazon Lex 會檢閱 `fulfillmentState`，並將下列回應傳回給用戶端。

      然後，Amazon Lex 會將下列項目傳回給用戶端：  
![\[\]](http://docs.aws.amazon.com/zh_tw/lex/latest/dg/images/gs-1-details-48.png)

      請注意：
      + `dialogState` – Amazon Lex 將此值設定為 `fulfilled`。
      + `message` – 是 Lambda 函數提供的相同訊息。

      用戶端將顯示該訊息。

1. 現在，再次測試機器人。要建立新的 (使用者) 內容，請由測試視窗選擇 **Clear (清除)** 連結。接著為 `OrderFlowers` 意圖提供無效的槽資料。這次 Lambda 函數會執行資料驗證、將無效的槽資料值重設為 null，並要求 Amazon Lex 提示使用者提供有效的資料。例如，嘗試以下操作：
   + 花種為「茉莉」(此花種不受支援)，
   + 想要取花的日期為「昨天」。
   + 下單後再輸入其他花種，而不要回覆「好」確認下單。為了回應，Lambda 函數會更新工作階段屬性`Price`中的 ，保持花訂單的執行總數。

   Lambda 函數也會執行履行活動。

**後續步驟**  
[步驟 6：更新意圖組態以加入表達用語 (主控台)](gs-bp-utterance.md)