

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

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

# 步驟 1：建立 Lambda 函式
<a name="gs2-prepare"></a>

首先，建立滿足比薩訂單的 Lambda 函數。您可以在 Amazon Lex 機器人中指定此函數，並在下一節中建立。

**建立 Lambda 函數**



1. 登入 AWS 管理主控台 ，並在 https：//[https://console.aws.amazon.com/lambda/](https://console.aws.amazon.com/lambda/) 開啟 AWS Lambda 主控台。

1. 選擇**建立函數**。

1. 在 **Create function (建立函數)** 頁面上，選擇 **Author from scratch (從頭開始撰寫)**。

   由於您是使用本練習提供的自訂程式碼建立 Lambda 函數，因此您需要選擇從頭開始撰寫該函數。

   請執行下列操作：

   1. 輸入名稱 (`PizzaOrderProcessor`)。

   1. 對於 **Runtime (執行時間)**，選擇最新版本的 Node.js。

   1. **Role (角色)** 選擇 **Create new role from template(s) (從範本建立新角色)**。

   1. 輸入新的角色名稱 (`PizzaOrderProcessorRole`)。

   1. 選擇**建立函數**。

1. 在函數頁面上，執行以下操作：

   在 **Function code (函數程式碼)** 區段，選擇 **Edit code inline (編輯程式碼內嵌)**，然後複製以下 Node.js 函數程式碼並將其貼至視窗中。

   ```
   'use strict';
        
   // Close dialog with the customer, reporting fulfillmentState of Failed or Fulfilled ("Thanks, your pizza will arrive in 20 minutes")
   function close(sessionAttributes, fulfillmentState, message) {
       return {
           sessionAttributes,
           dialogAction: {
               type: 'Close',
               fulfillmentState,
               message,
           },
       };
   }
    
   // --------------- Events -----------------------
    
   function dispatch(intentRequest, callback) {
       console.log(`request received for userId=${intentRequest.userId}, intentName=${intentRequest.currentIntent.name}`);
       const sessionAttributes = intentRequest.sessionAttributes;
       const slots = intentRequest.currentIntent.slots;
       const crust = slots.crust;
       const size = slots.size;
       const pizzaKind = slots.pizzaKind;
       
       callback(close(sessionAttributes, 'Fulfilled',
       {'contentType': 'PlainText', 'content': `Okay, I have ordered your ${size} ${pizzaKind} pizza on ${crust} crust`}));
       
   }
    
   // --------------- Main handler -----------------------
    
   // Route the incoming request based on intent.
   // The JSON body of the request is provided in the event slot.
   export const handler = (event, context, callback) => {
       try {
           dispatch(event,
               (response) => {
                   callback(null, response);
               });
       } catch (err) {
           callback(err);
       }
   };
   ```

1. 選擇**儲存**。

## 使用範例事件資料測試 Lambda 函數
<a name="gs2-lambdafunction-test"></a>

在 主控台中，使用範例事件資料手動叫用 Lambda 函數。

**若要測試 Lambda 函數：**

1. 登入 AWS 管理主控台 ，並在 https：//[https://console.aws.amazon.com/lambda/](https://console.aws.amazon.com/lambda/) 開啟 AWS Lambda 主控台。

1. 在 **Lambda 函數**頁面上，選擇 Lambda 函數 (`PizzaOrderProcessor).`

1. 從函數頁面上的測試事件清單中，選擇 **Configure test events (設定測試事件)**。

1. 在 **Configure test event (設定測試事件)** 頁面上，執行以下操作：

   1. 選擇 **建立新測試事件**。

   1. 在 **Event name (事件名稱)** 欄位內，輸入事件的名稱 (`PizzaOrderProcessorTest`)。

   1. 將下列 Amazon Lex 事件複製到 視窗。

      ```
      {
        "messageVersion": "1.0",
        "invocationSource": "FulfillmentCodeHook",
        "userId": "user-1",
        "sessionAttributes": {},
        "bot": {
          "name": "PizzaOrderingApp",
          "alias": "$LATEST",
          "version": "$LATEST"
        },
        "outputDialogMode": "Text",
        "currentIntent": {
          "name": "OrderPizza",
          "slots": {
            "size": "large",
            "pizzaKind": "meat",
            "crust": "thin"
          },
          "confirmationStatus": "None"
        }
      }
      ```

1. 選擇**建立**。

AWS Lambda 會建立測試，然後您返回 函數頁面。選擇**測試**，Lambda 會執行您的 Lambda 函數。

在結果方塊中，選擇 **Details (詳細資訊)**。主控台將在 **Execution result (執行結果)** 窗格內顯示以下輸出。

```
{
  "sessionAttributes": {},
  "dialogAction": {
    "type": "Close",
    "fulfillmentState": "Fulfilled",
    "message": {
      "contentType": "PlainText",
      "content": "Okay, I have ordered your large meat pizza on thin crust."
    }
}
```

## 後續步驟
<a name="gs2-next-step-create-bot"></a>

[步驟 2：建立機器人](gs2-create-bot.md)