步骤 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,然后通过以下网址打开 AWS Lambda 控制台:https://console.aws.amazon.com/lambda/

  2. 选择 Create function(创建函数)。

  3. 创建函数页面上,选择从 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,然后通过以下网址打开 AWS Lambda 控制台:https://console.aws.amazon.com/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:创建自动程序