附上一个 AWS Lambda 使用API操作对机器人执行函数 - Amazon Lex

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

附上一个 AWS Lambda 使用API操作对机器人执行函数

您必须先将 Lambda 函数附加到您的机器人别名中,然后才能调用该函数。您只能将一个 Lambda 函数与每个机器人别名相关联。执行以下步骤以使用API操作附加 Lambda 函数。

如果您要创建新的机器人别名,请使用CreateBotAlias操作来附加 Lambda 函数。要将 Lambda 函数附加到现有机器人别名,请使用操作。UpdateBotAlias修改 botAliasLocaleSettings 字段以包含正确的设置:

{ "botAliasLocaleSettings" : { locale: { "codeHookSpecification": { "lambdaCodeHook": { "codeHookInterfaceVersion": "1.0", "lambdaARN": "arn:aws:lambda:region:account-id:function:function-name" } }, "enabled": true }, ... } }
  1. botAliasLocaleSettings 字段映射到一个对象,该对象的键是要在其中附加 Lambda 函数的区域设置。有关支持的区域设置和有效密钥代码的列表,请参阅 支持的语言和区域设置

  2. 要查找 Lambda 函数的,请lambdaARN打开 AWS Lambda https://console.aws.amazon.com/lambda/家控制台,在左侧边栏中选择 “功能”,然后选择要与机器人别名关联的功能。函数概述的右侧,找到函数lambdaARN下的ARN。其中应包含区域、账户 ID 和函数名称。

  3. 要允许 Amazon Lex V2 为别名调用 Lambda 函数,请将 enabled 字段设置为 true

设置使用操作调用 Lambda 函数的意图 API

要在意图期间设置 Lambda 函数调用,请在创建新意图时使用该CreateIntent操作;如果您在现有意图中调用该函数,则使用该UpdateIntent操作。在意图操作中控制 Lambda 函数调用的字段是 dialogCodeHookinitialResponseSettingintentConfirmationSettingfulfillmentCodeHook

如果在获取插槽期间调用该函数,则在创建新插槽时使用该CreateSlot操作,或者使用该UpdateSlot操作在现有插槽中调用该函数。在槽位操作中控制 Lambda 函数调用的字段是 valueElicitationSetting 对象的 slotCaptureSetting 字段。

  1. 要将 Lambda 对话框代码挂钩设置为在对话的每回合之后运行,请将该enabled字段中以下DialogCodeHookSettings对象的dialogCodeHook字段设置为:true

    "dialogCodeHook": { "enabled": boolean }
  2. 另外,您可以通过修改与您要调用函数的对话阶段对应的结构中的 codeHook 和/或 elicitationCodeHook 字段,将 Lambda 对话框代码挂钩设置为仅在对话的特定点运行。要使用 Lambda 对话框代码挂钩实现意图,请使用CreateIntentfulfillmentCodeHookUpdateIntent操作中的字段。这三种类型的代码挂钩的结构和用途如下:

codeHook 字段定义了代码挂钩在对话的给定阶段运行的设置。它是一个具有以下结构的DialogCodeHookInvocationSetting对象:

"codeHook": { "active": boolean, "enableCodeHookInvocation": boolean, "invocationLabel": string, "postCodeHookSpecification": PostDialogCodeHookInvocationSpecification object, }
  • 将 Amazon Lex V2 的 active 字段更改为 true,从而在对话中该取值对应的时刻调用代码挂钩。

  • 将 Amazon Lex V2 的 enableCodeHookInvocation 字段更改为 true,从而允许代码挂钩正常运行。如果将其标记为 false,Amazon Lex V2 则表现为代码挂钩已成功返回。

  • invocationLabel 表示调用代码挂钩的对话步骤。

  • 通过 postCodeHookSpecification 字段指定代码挂钩成功、失败或超时后的操作和消息。

elicitationCodeHook 字段定义了在需要重新引发一个或多个槽位时运行的代码挂钩的设置。如果槽位引发失败或意图确认被拒绝,则可能会出现这种情况。该elicitationCodeHook字段是一个具有以下结构的ElicitationCodeHookInvocationSetting对象:

"elicitationCodeHook": { "enableCodeHookInvocation": boolean, "invocationLabel": string }
  • 将 Amazon Lex V2 的 enableCodeHookInvocation 字段更改为 true,从而允许代码挂钩正常运行。如果将其标记为 false,Amazon Lex V2 则表现为代码挂钩已成功返回。

  • invocationLabel 表示调用代码挂钩的对话步骤。

fulfillmentCodeHook 字段定义了为履行意图而运行的代码挂钩的设置。它映射到以下FulfillmentCodeHookSettings对象:

"fulfillmentCodeHook": { "active": boolean, "enabled": boolean, "fulfillmentUpdatesSpecification": FulfillmentUpdatesSpecification object, "postFulfillmentStatusSpecification": PostFulfillmentStatusSpecification object }
  • 将 Amazon Lex V2 的 active 字段更改为 true,从而在对话中该取值对应的时刻调用代码挂钩。

  • 将 Amazon Lex V2 的 enabled 字段更改为 true,从而允许代码挂钩正常运行。如果将其标记为 false,Amazon Lex V2 则表现为代码挂钩已成功返回。

  • 通过 fulfillmentUpdatesSpecification 字段指定在履行意图期间出现用户更新的消息以及与之相关的时间。

  • 通过 postFulfillmentStatusSpecification 字段指定代码挂钩成功、失败或超时后的消息和操作。

您可以通过将 activeenableCodeHookInvocation/enabled 字段设置为 true,在对话的以下时间点调用 Lambda 代码挂钩:

要在识别意图后在初始响应中调用 Lambda 函数,请使用CreateIntentUpdateIntent操作initialResponse字段中的codeHook结构。该initialResponse字段映射到以下InitialResponseSetting对象:

"initialResponse": { "codeHook": { "active": boolean, "enableCodeHookInvocation": boolean, "invocationLabel": string, "postCodeHookSpecification": PostDialogCodeHookInvocationSpecification object, }, "initialResponse": FulfillmentUpdatesSpecification object, "nextStep": PostFulfillmentStatusSpecification object, "conditional": ConditionalSpecification object }

要在获得槽值后调用 Lambda 函数,请使用或slotCaptureSetting操作valueElicitation字段中的字段。CreateSlotUpdateSlotslotCaptureSetting字段映射到以下SlotCaptureSetting对象:

"slotCaptureSetting": { "captureConditional": ConditionalSpecification object, "captureNextStep": DialogState object, "captureResponse": ResponseSpecification object, "codeHook": { "active": true, "enableCodeHookInvocation": true, "invocationLabel": string, "postCodeHookSpecification": PostDialogCodeHookInvocationSpecification object, }, "elicitationCodeHook": { "enableCodeHookInvocation": boolean, "invocationLabel": string }, "failureConditional": ConditionalSpecification object, "failureNextStep": DialogState object, "failureResponse": ResponseSpecification object }
  • 要在成功引发槽位后调用 Lambda 函数,请使用 codeHook 字段。

  • 要在槽位引发失败且 Amazon Lex V2 尝试重试槽位引发后调用 Lambda 函数,请使用 elicitationCodeHook 字段。

要在确认意图时调用 Lambda 函数,请使用CreateIntentintentConfirmationSettingUpdateIntent操作的字段。该intentConfirmation字段映射到以下IntentConfirmationSetting对象:

"intentConfirmationSetting": { "active": boolean, "codeHook": { "active": boolean, "enableCodeHookInvocation": boolean, "invocationLabel": string, "postCodeHookSpecification": PostDialogCodeHookInvocationSpecification object, }, "confirmationConditional": ConditionalSpecification object, "confirmationNextStep": DialogState object, "confirmationResponse": ResponseSpecification object, "declinationConditional": ConditionalSpecification object, "declinationNextStep": FulfillmentUpdatesSpecification object, "declinationResponse": PostFulfillmentStatusSpecification object, "elicitationCodeHook": { "enableCodeHookInvocation": boolean, "invocationLabel": string, }, "failureConditional": ConditionalSpecification object, "failureNextStep": DialogState object, "failureResponse": ResponseSpecification object, "promptSpecification": PromptSpecification object }
  • 要在用户确认意图及其槽位后调用 Lambda 函数,请使用 codeHook 字段。

  • 要在用户拒绝意图确认且 Amazon Lex V2 尝试重试槽位引发后调用 Lambda 函数,请使用 elicitationCodeHook 字段。

要调用 Lambda 函数来实现意图,请使用CreateIntentfulfillmentCodeHookUpdateIntent操作中的字段。该fulfillmentCodeHook字段映射到以下FulfillmentCodeHookSettings对象:

{ "active": boolean, "enabled": boolean, "fulfillmentUpdatesSpecification": FulfillmentUpdatesSpecification object, "postFulfillmentStatusSpecification": PostFulfillmentStatusSpecification object }

3. 设置调用 Lambda 函数的对话阶段后,请执行 BuildBotLocale 操作重建机器人以测试该函数。