準備回應格式 - 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您提供的是PlainTextCustomPayload、或SSML,請在content欄位中撰寫您要傳回給客戶的訊息。如果contentType您提供的是ImageResponseCard,請在該imageResponseCard字段中提供卡的詳細信息。如果您不提供訊息,Amazon Lex V2 會使用建立機器人時定義的適當訊息。

如果是ElicitIntent或,則此messages欄位dialogAction.type為必要欄位ConfirmIntent

清單中的每個項目都是下列格式的結構,其中包含要傳回給使用者之訊息的相關資訊。請見此處範例:

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

下面提供了每個字段的描述:

  • 內容類型 — 要使用的訊息類型。

    CustomPayload— 您可以自訂的回應字串,以包含應用程式的資料或中繼資料。

    ImageResponseCard— 帶有客戶可以選擇按鈕的圖像。ImageResponseCard如需詳細資訊,請參閱。

    PlainText— 純文字字串。

    SSML— 包含語音合成標記語言的字串,可自訂音訊回應。

  • 內容 — 要傳送給使用者的訊息。如果訊息類型為PlainText、或CustomPayload,請使用此欄位SSML

  • imageResponseCard— 包含要顯示給使用者的回應卡定義。如果訊息類型為,請使用此欄位ImageResponseCard。對映至包含下列欄位的結構:

    • 標題」— 響應卡的標題。

    • 副標題 — 提示使用者選擇按鈕。

    • imageUrl — 卡片影像的連結。

    • button — 包含按鈕相關資訊的結構清單。每個結構都包含一個text欄位,其中包含要顯示的文字,以及一個value欄位,其中包含要傳送至 Amazon Lex V2 的值 (如果客戶選取該按鈕)。您最多可以包含三個按鈕。

一種結構,其中包含針對客戶的響應的請求特定屬性。如需更多資訊,請參閱設定請求屬性。此欄位為選用欄位。

響應中的必填字段

至少,Lambda 回應必須包含一個sessionState物件。在其中,提供一個dialogAction對象並指定type字段。視您提供typedialogAction的欄位而定,Lambda 回應可能還有其他必要欄位。這些要求描述如下,以及最少的工作示例:

委派可讓 Amazon Lex V2 決定下一個步驟。不需要其他欄位。

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

ElicitIntent提示客戶表達意圖。您必須在messages欄位中包含至少一則訊息,才能提示意圖引出。

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

ElicitSlot提示客戶提供插槽值。您必須在物件的slotToElicit欄位中包含插槽的名dialogAction稱。您還必須在sessionState物件intent中包含name的。

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

ConfirmIntent確認客戶的插槽值,以及意圖是否已準備好可以實現。您必須在sessionState物件intent中包含name的,以及slots要確認的。您還必須在messages字段中包含至少一條消息,以要求用戶確認插槽值。您的訊息應提示「是」或「否」回應。如果使用者回應「是」,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 會結束意圖的履行程序,並指出使用者不需要進一步的回應。您必須在namestatesessionState物件intent中包括和。相容的意圖狀態為FailedFulfilled、和InProgress

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