准备响应格式 - Amazon Lex

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

准备响应格式

将 Lambda 函数集成到您的 Amazon Lex V2 机器人中的第二步是了解 Lambda 函数响应中的字段,并确定您要操作哪些参数。以下 JSON 对象显示了返回到 Amazon Lex V2 的 Lambda 响应的一般格式:

{ "sessionState": { // see 会话状态 for details about the structure }, "messages": [ { "contentType": "CustomPayload | ImageResponseCard | PlainText | SSML", "content": string, "imageResponseCard": { "title": string, "subtitle": string, "imageUrl": string, "buttons": [ { "text": string, "value": string }, ... ] } }, ... ], "requestAttributes": { string: string, ... } }

响应中的每个字段如下所述:

您想要返回的用户与您的 Amazon Lex V2 机器人之间的对话状态。有关结构的详细信息,请参阅会话状态。此字段为必填。

Amazon Lex V2 在下一轮对话中返回给客户的消息的列表。如果您提供的 contentType 取值是 PlainTextCustomPayloadSSML,请在 content 字段中写下您要返回给客户的消息。如果您提供的 contentType 取值是 ImageResponseCard,请在 imageResponseCard 字段中提供卡的详细信息。如果您不提供消息,Amazon Lex V2 将使用创建机器人时定义的相应消息。

如果 dialogAction.type 的取值是 ElicitIntentConfirmIntent,则 messages 字段为必填字段。

列表中的每一项都是以下格式的结构,其中包含要返回给用户的消息的相关信息。示例如下:

{ "contentType": "CustomPayload | ImageResponseCard | PlainText | SSML", "content": string, "imageResponseCard": { "title": string, "subtitle": string, "imageUrl": string, "buttons": [ { "text": string, "value": string }, ... ] } }

每个字段的描述如下:

  • contentType:要使用的消息类型。

    CustomPayload:一个响应字符串,您可以自定义该字符串以包含应用程序的数据或元数据。

    ImageResponseCard:带有按钮的图像,客户可以选择。有关更多信息,请参阅 ImageResponseCard

    PlainText:纯文本字符串。

    SSML:包含语音合成标记语言的字符串,用于自定义音频响应。

  • content:要发送给用户的消息。如果消息类型为 PlainTextCustomPayloadSSML,则使用此字段。

  • imageResponseCard:包含要向用户显示的响应卡的定义。如果消息类型为 ImageResponseCard,则使用此字段。映射到一个包含以下字段的结构:

    • title:响应卡的标题。

    • subtitle:提示用户选择按钮。

    • imageUrl:指向卡的图像的链接。

    • buttons:包含按钮相关信息的结构的列表。如果客户选择该按钮,每个结构都包含一个包含要显示文本的 text 字段,以及一个包含要发送到 Amazon Lex V2 的值的 value 字段。最多可以包含三个按钮。

一种结构,其中包含针对客户的响应的请求特定的属性。请参阅设置请求属性了解更多信息。该字段是可选的。

响应中的必填字段

Lambda 响应必须包含至少一个 sessionState 对象。该响应提供一个 dialogAction 对象并指定 type 字段。根据您提供的 dialogActiontype,Lambda 响应可能还包含其他必填字段。这些要求如下所述,并附带最少的工作示例:

Delegate 让 Amazon Lex V2 决定下一步操作。没有其他必填字段。

{ "sessionState": { "dialogAction": { "type": "Delegate" } }

ElicitIntent 提示客户表达意图。messages 字段中必须包含至少一条消息才能提示引发意图。

{ "sessionState": { "dialogAction": { "type": "ElicitIntent" }, "messages": [ { "contentType": PlainText, "content": "How can I help you?" } ] }

ElicitSlot 提示客户提供槽位值。dialogAction 对象的 slotToElicit 字段中必须包含槽位的名称。还必须包含 sessionState 对象中 intentname

{` "sessionState": { "dialogAction": { "slotToElicit": "OriginCity", "type": "ElicitSlot" }, "intent": { "name": "BookFlight" } } }

ConfirmIntent 确认客户的槽位值以及意图是否准备履行。必须包含 sessionState 对象中 intentname,以及待确认的 slotsmessages 字段中还必须包含至少一条消息,要求用户确认槽位值。您的消息应提示响应“是”或“否”。如果用户响应“是”,则 Amazon Lex V2 会将意图的 confirmationState 设置为 Confirmed。如果用户响应“否”,则 Amazon Lex V2 会将意图的 confirmationState 设置为 Denied

{ "sessionState": { "dialogAction": { "type": "ConfirmIntent" }, "intent": { "name": "BookFlight", "slots": { "DepartureDate": { "value": { "originalValue": "tomorrow", "interpretedValue": "2023-05-09", "resolvedValues": [ "2023-05-09" ] } }, "DestinationCity": { "value": { "originalValue": "sf", "interpretedValue": "sf", "resolvedValues": [ "sf" ] } }, "OriginCity": { "value": { "originalValue": "nyc", "interpretedValue": "nyc", "resolvedValues": [ "nyc" ] } } } } }, "messages": [ { "contentType": PlainText, "content": "Okay, you want to fly from {OriginCity} to \ {DestinationCity} on {DepartureDate}. Is that correct?" } ] }

Close 结束意图的履行过程,并表示预计用户不会给出进一步的响应。您必须在 sessionState 对象中包含 intentnamestate。兼容的意图状态包括 FailedFulfilledInProgress

"sessionState": { "dialogAction": { "type": "Close" }, "intent": { "name": "BookFlight", "state": "Failed | Fulfilled | InProgress" } }