

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

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

# 資訊流程的詳細資訊
<a name="ex1-sch-appt-info-flow-details"></a>

`ScheduleAppointment` 機器人藍圖主要是展示如何運用動態產生的回應卡。本練習中的 Lambda 函數在其對 Amazon Lex 的回應中包含回應卡。Amazon Lex 會在回覆用戶端時包含回應卡。本部分說明下列兩項：
+ 用戶端與 Amazon Lex 之間的資料流程。

   

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

   
+ Amazon Lex 和 Lambda 函數之間的資料流程。如需詳細資訊，請參閱[Lambda 函數輸入事件和回應格式](lambda-input-response-format.md)。

**注意**  
此範例假設您使用 Facebook Messenger 用戶端，該用戶端不會在請求中將工作階段屬性傳遞給 Amazon Lex。因此，本節所示的範例顯示空的 `sessionAttributes`。如果您使用 Amazon Lex 主控台中提供的用戶端來測試機器人，用戶端會包含工作階段屬性。

本節說明在使用者每次輸入之後，會發生什麼情況。

1. 使用者：類型**Book an appointment**。

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

      ```
      POST /bot/ScheduleAppointment/alias/$LATEST/user/bijt6rovckwecnzesbthrr1d7lv3ja3n/text
      "Content-Type":"application/json"
      "Content-Encoding":"amz-1.0"
      
      {
         "inputText":"book appointment",
         "sessionAttributes":{}
      }
      ```

      請求 URI 和內文都會提供資訊給 Amazon Lex：
      + 請求 URI – 提供機器人名稱 (ScheduleAppointment)、機器人別名 (\$1LATEST) 和使用者名稱 ID。結尾 `text` 表示它是一種 `PostText` (而非 `PostContent`) API 請求。
      + 請求本文 – 包含使用者輸入 (`inputText`) 和空的 `sessionAttributes`。

   1. 從 `inputText`，Amazon Lex 會偵測意圖 (`MakeAppointment`)。服務會叫用設定為程式碼掛勾的 Lambda 函數，透過傳遞下列事件來執行初始化和驗證。如需詳細資訊，請參閱[輸入事件格式](lambda-input-response-format.md#using-lambda-input-event-format)。

      ```
      {
          "currentIntent": {
              "slots": {
                  "AppointmentType": null,
                  "Date": null,
                  "Time": null
              },
              "name": "MakeAppointment",
              "confirmationStatus": "None"
          },
          "bot": {
              "alias": null,
              "version": "$LATEST",
              "name": "ScheduleAppointment"
          },
          "userId": "bijt6rovckwecnzesbthrr1d7lv3ja3n",
          "invocationSource": "DialogCodeHook",
          "outputDialogMode": "Text",
          "messageVersion": "1.0",
          "sessionAttributes": {}
      }
      ```

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

   1. 目前，所有槽值都是 null (沒有可驗證的內容)。Lambda 函數會傳回下列回應給 Amazon Lex，引導服務引出`AppointmentType`插槽的資訊。如需回應格式的相關資訊，請參閱[回應格式](lambda-input-response-format.md#using-lambda-response-format)。

      ```
      {
          "dialogAction": {
              "slotToElicit": "AppointmentType",
              "intentName": "MakeAppointment",
              "responseCard": {
                  "genericAttachments": [
                      {
                          "buttons": [
                              {
                                  "text": "cleaning (30 min)",
                                  "value": "cleaning"
                              },
                              {
                                  "text": "root canal (60 min)",
                                  "value": "root canal"
                              },
                              {
                                  "text": "whitening (30 min)",
                                  "value": "whitening"
                              }
                          ],
                          "subTitle": "What type of appointment would you like to schedule?",
                          "title": "Specify Appointment Type"
                      }
                  ],
                  "version": 1,
                  "contentType": "application/vnd.amazonaws.card.generic"
              },
              "slots": {
                  "AppointmentType": null,
                  "Date": null,
                  "Time": null
              },
              "type": "ElicitSlot",
              "message": {
                  "content": "What type of appointment would you like to schedule?",
                  "contentType": "PlainText"
              }
          },
          "sessionAttributes": {}
      }
      ```

      回應包含 `dialogAction` 和 `sessionAttributes` 欄位。此外，`dialogAction` 欄位會傳回以下欄位：
      + `type` – 透過將此欄位設定為 `ElicitSlot`，Lambda 函數會指示 Amazon Lex 引出`slotToElicit`欄位中指定插槽的值。Lambda 函數也提供 `message`以傳達給使用者。
      + `responseCard` – 識別`AppointmentType`槽的可能值清單。支援回應卡的用戶端 （例如 Facebook Messenger) 會顯示回應卡，以允許使用者選擇預約類型，如下圖所示：  
![\[詢問要安排的預約類型和三個選項的回應卡：清理 (30 分鐘）、根管 (60 分鐘） 和白化 (30 分鐘）。\]](http://docs.aws.amazon.com/zh_tw/lex/latest/dg/images/respcard-10.png)

   1. 如 Lambda 函數回應`dialogAction.type`中的 所示，Amazon Lex 會將下列回應傳回給用戶端：  
![\[JSON 回應包含預約意圖和要引出的預約類型槽的相關資訊。\]](http://docs.aws.amazon.com/zh_tw/lex/latest/dg/images/appt-10.png)

      用戶端會讀取回應，然後顯示訊息：「您想要安排哪種類型的預約？」 以及回應卡 (如果用戶端支援回應卡的話)。

1. 使用者：根據用戶端，使用者有兩個選項：
   + 如果顯示回應卡，請選擇 **root canal (60 min)** 或輸入 **root canal**。
   + 如果用戶端不支援回應卡，輸入 **root canal**。

   1. 用戶端會將下列`PostText`請求傳送至 Amazon Lex （已新增換行以方便閱讀）：

      ```
      POST /bot/BookTrip/alias/$LATEST/user/bijt6rovckwecnzesbthrr1d7lv3ja3n/text
      "Content-Type":"application/json"
      "Content-Encoding":"amz-1.0"
      
      {
         "inputText": "root canal",
          "sessionAttributes": {}
      }
      ```

   1. Amazon Lex 透過傳送下列事件做為參數，叫用 Lambda 函數進行使用者資料驗證：

      ```
      {
          "currentIntent": {
              "slots": {
                  "AppointmentType": "root canal",
                  "Date": null,
                  "Time": null
              },
              "name": "MakeAppointment",
              "confirmationStatus": "None"
          },
          "bot": {
              "alias": null,
              "version": "$LATEST",
              "name": "ScheduleAppointment"
          },
          "userId": "bijt6rovckwecnzesbthrr1d7lv3ja3n",
          "invocationSource": "DialogCodeHook",
          "outputDialogMode": "Text",
          "messageVersion": "1.0",
          "sessionAttributes": {}
      }
      ```

      在事件資料中，注意下列事項：
      + `invocationSource` 繼續做為 `DialogCodeHook`。在此步驟中，我們只需驗證使用者資料。
      + Amazon Lex 會將`currentIntent.slots`插槽中的 `AppointmentType` 欄位設定為 `root canal`。
      + Amazon Lex 只會在用戶端和 Lambda 函數之間傳遞 `sessionAttributes` 欄位。

   1. Lambda 函數會驗證使用者輸入，並將下列回應傳回 Amazon Lex，指示服務引出預約日期的值。

      ```
      {
          "dialogAction": {
              "slotToElicit": "Date",
              "intentName": "MakeAppointment",
              "responseCard": {
                  "genericAttachments": [
                      {
                          "buttons": [
                              {
                                  "text": "2-15 (Wed)",
                                  "value": "Wednesday, February 15, 2017"
                              },
                              {
                                  "text": "2-16 (Thu)",
                                  "value": "Thursday, February 16, 2017"
                              },
                              {
                                  "text": "2-17 (Fri)",
                                  "value": "Friday, February 17, 2017"
                              },
                              {
                                  "text": "2-20 (Mon)",
                                  "value": "Monday, February 20, 2017"
                              },
                              {
                                  "text": "2-21 (Tue)",
                                  "value": "Tuesday, February 21, 2017"
                              }
                          ],
                          "subTitle": "When would you like to schedule your root canal?",
                          "title": "Specify Date"
                      }
                  ],
                  "version": 1,
                  "contentType": "application/vnd.amazonaws.card.generic"
              },
              "slots": {
                  "AppointmentType": "root canal",
                  "Date": null,
                  "Time": null
              },
              "type": "ElicitSlot",
              "message": {
                  "content": "When would you like to schedule your root canal?",
                  "contentType": "PlainText"
              }
          },
          "sessionAttributes": {}
      }
      ```

      同樣地，回應也包含 `dialogAction` 和 `sessionAttributes` 欄位。此外，`dialogAction` 欄位會傳回以下欄位：
      + `type` – 透過將此欄位設定為 `ElicitSlot`，Lambda 函數會指示 Amazon Lex 引出`slotToElicit`欄位中指定插槽的值。Lambda 函數也提供 `message`以傳達給使用者。
      + `responseCard` – 識別`Date`槽的可能值清單。支援回應卡 （例如 Facebook Messenger) 的用戶端會顯示回應卡，允許使用者選擇預約日期，如下圖所示：  
![\[回應卡會引出日期來排程根管治療和三個選項：2-15、2-16 和 2-17。\]](http://docs.aws.amazon.com/zh_tw/lex/latest/dg/images/respcard-20.png)

        雖然 Lambda 函數傳回五個日期，但用戶端 (Facebook Messenger) 對於回應卡有三個按鈕的限制。因此，您在螢幕擷取畫面中只會看到前三個值。

        這些日期會在 Lambda 函數中硬式編碼。在生產應用程式中，您可以使用行事曆以即時取得可用的日期。由於日期是動態的，您必須在 Lambda 函數中動態產生回應卡。

   1. Amazon Lex 注意到 `dialogAction.type`，並將下列回應傳回給用戶端，其中包含 Lambda 函數回應中的資訊。  
![\[JSON 回應包含預約的意圖、填入預約類型的 ，以及引出預約日期的訊息。\]](http://docs.aws.amazon.com/zh_tw/lex/latest/dg/images/appt-20.png)

      用戶端會顯示訊息：**When would you like to schedule your root canal?** 和回應卡 (如果用戶端有支援回應卡)。

1. 使用者：類型**Thursday**。

   1. 用戶端會將下列`PostText`請求傳送至 Amazon Lex （已新增換行以方便閱讀）：

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

   1. Amazon Lex 會在下列事件中以參數傳送 ，以叫用 Lambda 函數進行使用者資料驗證：

      ```
      {
          "currentIntent": {
              "slots": {
                  "AppointmentType": "root canal",
                  "Date": "2017-02-16",
                  "Time": null
              },
              "name": "MakeAppointment",
              "confirmationStatus": "None"
          },
          "bot": {
              "alias": null,
              "version": "$LATEST",
              "name": "ScheduleAppointment"
          },
          "userId": "u3fpr9gghj02zts7y5tpq5mm4din2xqy",
          "invocationSource": "DialogCodeHook",
          "outputDialogMode": "Text",
          "messageVersion": "1.0",
          "sessionAttributes": {}
      }
      ```

      在事件資料中，注意下列事項：
      + `invocationSource` 繼續做為 `DialogCodeHook`。在此步驟中，我們只需驗證使用者資料。
      + Amazon Lex 會將`currentIntent.slots`插槽中的 `Date` 欄位設定為 `2017-02-16`。
      + Amazon Lex 只會在用戶端和 Lambda 函數`sessionAttributes`之間傳遞 。

   1. Lambda 函數會驗證使用者輸入。這次 Lambda 函數會判斷指定日期沒有可用的預約。它會傳回下列回應給 Amazon Lex，指示服務再次引出預約日期的值。

      ```
      {
          "dialogAction": {
              "slotToElicit": "Date",
              "intentName": "MakeAppointment",
              "responseCard": {
                  "genericAttachments": [
                      {
                          "buttons": [
                              {
                                  "text": "2-15 (Wed)",
                                  "value": "Wednesday, February 15, 2017"
                              },
                              {
                                  "text": "2-17 (Fri)",
                                  "value": "Friday, February 17, 2017"
                              },
                              {
                                  "text": "2-20 (Mon)",
                                  "value": "Monday, February 20, 2017"
                              },
                              {
                                  "text": "2-21 (Tue)",
                                  "value": "Tuesday, February 21, 2017"
                              }
                          ],
                          "subTitle": "When would you like to schedule your root canal?",
                          "title": "Specify Date"
                      }
                  ],
                  "version": 1,
                  "contentType": "application/vnd.amazonaws.card.generic"
              },
              "slots": {
                  "AppointmentType": "root canal",
                  "Date": null,
                  "Time": null
              },
              "type": "ElicitSlot",
              "message": {
                  "content": "We do not have any availability on that date, is there another day which works for you?",
                  "contentType": "PlainText"
              }
          },
          "sessionAttributes": {
           "bookingMap": "{\"2017-02-16\": []}"
         }
      }
      ```

      同樣地，回應也包含 `dialogAction` 和 `sessionAttributes` 欄位。此外，`dialogAction` 會傳回以下欄位：
      + `dialogAction` 欄位：
        + `type` – Lambda 函數將此值設定為 ，`ElicitSlot`並將 `slotToElicit` 欄位重設為 `Date`。Lambda 函數也提供適當的 `message` 以傳達給使用者。
        + `responseCard` – 傳回 `Date` 槽的值清單。
      + `sessionAttributes` - 這次 Lambda 函數包含`bookingMap`工作階段屬性。其值是要求的預約日期及可預約的時間 (空白物件表示沒有可預約的時間)。

   1. Amazon Lex 注意到 `dialogAction.type`，並將下列回應傳回給用戶端，其中包含 Lambda 函數回應中的資訊。  
![\[JSON 回應顯示打算進行預約，並說明客戶請求的日期沒有空的訊息。\]](http://docs.aws.amazon.com/zh_tw/lex/latest/dg/images/appt-30.png)

      用戶端會顯示訊息：**We do not have any availability on that date, is there another day which works for you?** 和回應卡 (如果用戶端有支援回應卡)。

1. 使用者：根據用戶端，使用者有兩個選項：
   + 如果有顯示回應卡，請選擇 **2-15 (Wed)**，或輸入 **Wednesday**。
   + 如果用戶端不支援回應卡，輸入 **Wednesday**。

   1. 用戶端會將下列`PostText`請求傳送至 Amazon Lex：

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

      
**注意**  
Facebook Messenger 用戶端不會設定任何工作階段屬性。如果您想要在請求之間維持工作階段狀態，您必須在 Lambda 函數中執行此操作。在真實的應用程式中，您可能需要在後端資料庫中維護這些工作階段屬性。

   1. Amazon Lex 透過傳送下列事件做為參數，叫用 Lambda 函數進行使用者資料驗證：

      ```
      {
          "currentIntent": {
              "slots": {
                  "AppointmentType": "root canal",
                  "Date": "2017-02-15",
                  "Time": null
              },
              "name": "MakeAppointment",
              "confirmationStatus": "None"
          },
          "bot": {
              "alias": null,
              "version": "$LATEST",
              "name": "ScheduleAppointment"
          },
          "userId": "u3fpr9gghj02zts7y5tpq5mm4din2xqy",
          "invocationSource": "DialogCodeHook",
          "outputDialogMode": "Text",
          "messageVersion": "1.0",
          "sessionAttributes": {
          }
      }
      ```

      Amazon Lex 已更新`currentIntent.slots`，方法是將`Date`插槽設定為 `2017-02-15`。

   1. Lambda 函數會驗證使用者輸入，並將下列回應傳回 Amazon Lex，並指示它引出預約時間的值。

      ```
      {
          "dialogAction": {
              "slots": {
                  "AppointmentType": "root canal",
                  "Date": "2017-02-15",
                  "Time": "16:00"
              },
              "message": {
                  "content": "What time on 2017-02-15 works for you? 4:00 p.m. is our only availability, does that work for you?",
                  "contentType": "PlainText"
              },
              "type": "ConfirmIntent",
              "intentName": "MakeAppointment",
              "responseCard": {
                  "genericAttachments": [
                      {
                          "buttons": [
                              {
                                  "text": "yes",
                                  "value": "yes"
                              },
                              {
                                  "text": "no",
                                  "value": "no"
                              }
                          ],
                          "subTitle": "Is 4:00 p.m. on 2017-02-15 okay?",
                          "title": "Confirm Appointment"
                      }
                  ],
                  "version": 1,
                  "contentType": "application/vnd.amazonaws.card.generic"
              }
          },
          "sessionAttributes": {
              "bookingMap": "{\"2017-02-15\": [\"10:00\", \"16:00\", \"16:30\"]}"
          }
      }
      ```

      同樣地，回應也包含 `dialogAction` 和 `sessionAttributes` 欄位。此外，`dialogAction` 會傳回以下欄位：
      + `dialogAction` 欄位：
        + `type` – `Lambda`函數會將此值設定為 `ConfirmIntent`，指示 Amazon Lex 取得使用者在 中建議的預約時間確認`message`。
        + `responseCard` – 傳回使用者可選擇的是/否值清單。如果用戶端支援回應卡，它會顯示回應卡，如下例所示：  
![\[顯示預約確認和兩個選項的回應卡：是和否。\]](http://docs.aws.amazon.com/zh_tw/lex/latest/dg/images/respcard-30.png)
      + `sessionAttributes` - Lambda 函數會將`bookingMap`工作階段屬性的值設定為該日期的預約日期和可用的預約。在這個範例中，這些是 30 分鐘的預約。對於需要一個小時的根管治療，只能預訂下午 4 點。

        

   1. 如 Lambda 函數回應`dialogAction.type`中的 所示，Amazon Lex 會將下列回應傳回給用戶端：  
![\[JSON 回應顯示預約的意圖和填入的所有槽。\]](http://docs.aws.amazon.com/zh_tw/lex/latest/dg/images/appt-40.png)

      用戶端會顯示訊息：**2017-02-15 的什麼時間適用於您？下午 4：00 是我們唯一的可用性，這是否適用於您？**

   

1. 使用者：選擇 **yes**。

   Amazon Lex 會使用下列事件資料叫用 Lambda 函數。由於使用者已回應 **yes**，Amazon Lex `confirmationStatus`會將 設定為 `Confirmed`，並將 中的 `Time` 欄位`currentIntent.slots `設定為 `4 p.m`。

   ```
   {
       "currentIntent": {
           "slots": {
               "AppointmentType": "root canal",
               "Date": "2017-02-15",
               "Time": "16:00"
           },
           "name": "MakeAppointment",
           "confirmationStatus": "Confirmed"
       },
       "bot": {
           "alias": null,
           "version": "$LATEST",
           "name": "ScheduleAppointment"
       },
       "userId": "u3fpr9gghj02zts7y5tpq5mm4din2xqy",
       "invocationSource": "FulfillmentCodeHook",
       "outputDialogMode": "Text",
       "messageVersion": "1.0",
       "sessionAttributes": {
      }
   }
   ```

   由於`confirmationStatus`已確認 ，Lambda 函數會處理意圖 （預約牙科預約），並將下列回應傳回 Amazon Lex：

   ```
   {
       "dialogAction": {
           "message": {
               "content": "Okay, I have booked your appointment.  We will see you at 4:00 p.m. on 2017-02-15",
               "contentType": "PlainText"
           },
           "type": "Close",
           "fulfillmentState": "Fulfilled"
       },
       "sessionAttributes": {
           "formattedTime": "4:00 p.m.",
           "bookingMap": "{\"2017-02-15\": [\"10:00\"]}"
       }
   }
   ```

   注意下列事項：
   + Lambda 函數已更新 `sessionAttributes`。
   + `dialogAction.type` 設定為 `Close`，這會指示 Amazon Lex 不預期使用者回應。
   + `dialogAction.fulfillmentState` 設為 `Fulfilled`，指出意圖已順利達到。

   用戶端會顯示訊息：**好的，我已預約您的預約。我們將在 2017-02-15 下午 4：00 與您相見。**





