步驟 1:建立 Lambda 函數 - Amazon Lex V1

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

 

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

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

步驟 1:建立 Lambda 函數

首先,創建一個滿足比薩餅訂單的 Lambda 函數。在下一節中,您在所建立的 Amazon Lex 機器人中指定此函數。

若要建立 Lambda 函數

  1. 請登入 AWS Management Console,並開啟位於 https://console.aws.amazon.com/lambda/ 的 AWS Lambda 主控台。

  2. 選擇 Create function (建立函數)。

  3. Create function (建立函數) 頁面上,選擇 Author from scratch (從頭開始撰寫)

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

    請執行下列動作:

    1. 輸入名稱 (PizzaOrderProcessor)。

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

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

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

    5. 選擇 Create function (建立函數)。

  4. 在函數頁面上,執行以下操作:

    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); } };
  5. 選擇 Save (儲存)

使用範例事件資料測試 Lambda 函數

在主控台中,使用範例事件資料手動叫用 Lambda 函數來測試它。

測試 Lambda 函數:
  1. 請登入 AWS Management Console,並開啟位於 https://console.aws.amazon.com/lambda/ 的 AWS Lambda 主控台。

  2. Lambda 函數頁面上,選擇 Lambda 函數 (PizzaOrderProcessor).

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

  4. Configure test event (設定測試事件) 頁面上,執行以下操作:

    1. 選擇 Create new test event (建立新測試事件)

    2. Event name (事件名稱) 欄位內,輸入事件的名稱 (PizzaOrderProcessorTest)。

    3. 將下列 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" } }
  5. 選擇 Create (建立)。

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." } }

後續步驟

步驟 2:建立機器人