使用 API 操作将 AWS Lambda 函数关联到机器人
必须先将 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 }, ... } }
-
botAliasLocaleSettings字段映射到一个对象,该对象的键是要在其中关联 Lambda 函数的区域设置。有关支持的区域设置和有效密钥代码的列表,请参阅 支持的语言和区域设置。 -
要查找 Lambda 函数的
lambdaARN,请访问 https://console.aws.amazon.com/lambda/home,打开 AWS Lambda 控制台,在左侧栏中选择函数,然后选择要与机器人别名关联的函数。在函数概述的右侧,找到函数 ARN 下的 lambdaARN。其中应包含区域、账户 ID 和函数名称。 -
要允许 Amazon Lex V2 为别名调用 Lambda 函数,请将
enabled字段设置为true。
使用 API 操作设置意图以调用 Lambda 函数
要在意图期间设置 Lambda 函数调用,请在创建新意图时执行 CreateIntent 操作,或者在现有意图中调用该函数时执行 UpdateIntent 操作。在意图操作中控制 Lambda 函数调用的字段是 dialogCodeHook、initialResponseSetting、intentConfirmationSetting 和 fulfillmentCodeHook。
如果您在引发槽位期间调用该函数,则在创建新槽位时执行 CreateSlot 操作,或者执行 UpdateSlot 操作在现有槽位中调用该函数。在槽位操作中控制 Lambda 函数调用的字段是 valueElicitationSetting 对象的 slotCaptureSetting 字段。
-
要将 Lambda 对话框代码挂钩设置为在对话的每回合之后运行,请将
dialogCodeHook字段中 DialogCodeHookSettings 对象的enabled字段设置为true。"dialogCodeHook": { "enabled":boolean} -
另外,您可以通过修改与您要调用函数的对话阶段对应的结构中的
codeHook和/或elicitationCodeHook字段,将 Lambda 对话框代码挂钩设置为仅在对话的特定点运行。要使用 Lambda 对话框代码挂钩履行意图,请使用 CreateIntent 或 UpdateIntent 操作中的fulfillmentCodeHook字段。这三种类型的代码挂钩的结构和用途如下:
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字段指定代码挂钩成功、失败或超时后的消息和操作。
您可以通过将 active 和 enableCodeHookInvocation/enabled 字段设置为 true,在对话的以下时间点调用 Lambda 代码挂钩:
要在识别意图后在初始响应中调用 Lambda 函数,请使用 CreateIntent 或 UpdateIntent 操作的 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 函数,请使用 CreateSlot 或 UpdateSlot 操作的 valueElicitation 字段中的 slotCaptureSetting。slotCaptureSetting 字段映射到以下 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 函数,请使用 CreateIntent 或 UpdateIntent 操作的 intentConfirmationSetting 字段。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 函数来履行意图,请使用 CreateIntent 或 UpdateIntent 操作的 fulfillmentCodeHook 字段。fulfillmentCodeHook 字段映射到以下 FulfillmentCodeHookSettings 对象:
{ "active":boolean, "enabled":boolean, "fulfillmentUpdatesSpecification": FulfillmentUpdatesSpecification object, "postFulfillmentStatusSpecification": PostFulfillmentStatusSpecification object }
3. 设置调用 Lambda 函数的对话阶段后,请执行 BuildBotLocale 操作重建机器人以测试该函数。