选择您的 Cookie 首选项

我们使用必要 Cookie 和类似工具提供我们的网站和服务。我们使用性能 Cookie 收集匿名统计数据,以便我们可以了解客户如何使用我们的网站并进行改进。必要 Cookie 无法停用,但您可以单击“自定义”或“拒绝”来拒绝性能 Cookie。

如果您同意,AWS 和经批准的第三方还将使用 Cookie 提供有用的网站功能、记住您的首选项并显示相关内容,包括相关广告。要接受或拒绝所有非必要 Cookie,请单击“接受”或“拒绝”。要做出更详细的选择,请单击“自定义”。

步骤 1:创建 Lambda 函数

聚焦模式
步骤 1:创建 Lambda 函数 - Amazon Lex V1

终止支持通知:2025年9月15日, AWS 我们将停止对Amazon Lex V1的支持。2025 年 9 月 15 日之后,您将无法再访问亚马逊 Lex V1 主机或 Amazon Lex V1 资源。如果您使用的是 Amazon Lex V2,请改为参阅 Amazon Lex V2 指南

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

终止支持通知:2025年9月15日, AWS 我们将停止对Amazon Lex V1的支持。2025 年 9 月 15 日之后,您将无法再访问亚马逊 Lex V1 主机或 Amazon Lex V1 资源。如果您使用的是 Amazon Lex V2,请改为参阅 Amazon Lex V2 指南

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

首先,创建一个用于履行披萨订单的 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. 选择保存

使用示例事件数据测试 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. 选择创建

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:创建自动程序

本页内容

隐私网站条款Cookie 首选项
© 2025, Amazon Web Services, Inc. 或其附属公司。保留所有权利。