1. 事前準備
在本程式碼研究室中,您將瞭解如何使用 Dialogflow CX (對話式 AI 平台,CAIP) 建構零售聊天機器人,打造對話式 UI。Dialogflow CX 可實作虛擬服務專員,例如:聊天機器人、語音機器人、電話閘道,並支援超過 50 種語言的多個管道。
本程式碼研究室將引導您建構零售業的網站聊天機器人。我們要為名為「G-Records」的虛構商家建構聊天機器人。G-Records 是位於加州的搖滾唱片公司,這間唱片公司簽下了 4 個搖滾樂團:Alice Googler、G's N' Roses、The Goo Fighters 和 The Google Dolls。G-Records 向所有搖滾樂迷銷售樂團商品。
完成本程式碼研究室後,您可以使用聊天機器人訂購襯衫或音樂,也可以查詢訂單。

課程內容
您將透過實作,瞭解 Dialogflow CX 相較於 Dialogflow ES 的優點。包括下列概念:
- 如何在 Google Cloud 中建立 Dialogflow CX 虛擬服務專員
- 瞭解如何建立流程
- 瞭解如何建立實體
- 瞭解如何建立意圖
- 瞭解如何使用狀態處理常式建立頁面和轉場頁面
- 瞭解如何使用意圖路徑轉換網頁
- 瞭解如何使用參數和條件式路徑轉換網頁
- 瞭解如何使用系統函式傳回條件式回應
- 瞭解如何建立備用訊息
- 瞭解如何使用模擬器
- 瞭解如何建立測試案例和測試涵蓋範圍
最終的 Dialogflow CX 虛擬服務專員設計如下所示:

軟硬體需求
- 您必須擁有 Google 身分 / Gmail 地址,才能建立 Dialogflow CX 虛擬服務專員。
- 存取 Google Cloud。
2. 設定環境
建立 Google Cloud 專案
由於 Dialogflow CX 在 Google Cloud 中執行,因此您必須建立 Google Cloud 專案。專案會統整所有 Google Cloud 資源。專案是由一組協作者、已啟用的 API (和其他資源)、監控工具、帳單資訊,以及驗證和存取權控管組成。
建立新專案時,請輸入專案名稱。您必須將其連結至現有的帳單帳戶和機構。
帳單帳戶可用來定義特定資源組合費用的支付者,其可連結至一或多個專案。專案的使用費會從相連結的帳單帳戶中扣除。在大多數情況下,您會在建立專案時設定帳單。詳情請參閱帳單說明文件。確認 Cloud 專案已啟用計費功能。

啟用 Dialogflow API
如要使用 Dialogflow,您必須為專案啟用 Dialogflow API。
- 選取要啟用 API 的專案,然後按一下「繼續」。
- 收合「API 和服務」選單,然後按一下「建立憑證」。
- 按一下「應用程式資料」
- 由於您目前未使用 Kubernetes Engine、App Engine 或 Cloud Functions,請選取「否,我未使用這些服務」。
- 然後按一下 [完成]。

建立新的 Dialogflow CX 虛擬服務專員
如要建立新的 Dialogflow CX 虛擬服務專員,請先開啟 Dialogflow CX 控制台:
- 選擇先前建立的 Google Cloud 專案。
- 按一下「建立代理程式」。
填寫基本代理程式設定表單:
- 顯示名稱可自由選擇。
- 選擇位置:us-central1
- 選取偏好的時區。
- 選取「en - English」做為預設語言
點選「建立」。

好了,一切準備就緒。我們終於可以開始模擬虛擬代理人了。
3. 流程
複雜的對話通常會涉及多個對話主題。以我們為 G-Records 建立的聊天機器人為例,如果目的是銷售樂團商品,對話內容就會包含產品目錄、付款、訂單狀態和客戶服務問題。我們可以將這些對話主題分成流程。

流程可讓團隊處理個別對話路徑。建議簡化流程,讓流程更容易顯示在螢幕上,且更具模組化。
流程是 Dialogflow CX 的新概念。Dialogflow Essentials 具有 Mega Agents 的概念,與流程有些類似。不過,您會更常使用 Flows。
在本實驗室的後續部分,我們會使用可結束流程的狀態處理常式 (因此會跳回下一個或上一個流程),您也可以結束完整的代理程式工作階段。
現在就來建立一些流程。
建立流程
- 在 Dialogflow CX 中,依序點選 + 圖示 >「建立流程」。
- 指定名稱:
Catalog,然後按下 Enter 鍵。

第一個流程「目錄」已建立完成。現在建立其他流程:
Order ProcessMy OrderCustomer Care

稍後我們會在本實驗室中設定頁面狀態處理常式,確保最終的視覺化效果如下所示:

手機模擬器
在 Dialogflow CX 主控台右側,您可以使用內建的模擬工具測試虛擬服務專員。您可以從對話開頭或特定流程開始測試對話。
- 按一下畫面右上方的「測試代理程式」按鈕。
- 在「與服務專員對話」欄位中輸入:
Hello虛擬服務專員會以預設歡迎訊息回應:您好!您需要哪方面的協助?

我們來修改這個預設歡迎訊息。
預設啟動流程
首先,請建立意圖路徑,在您向虛擬代理程式問候時觸發。
- 在左側的「Build」>「Flows」側欄中,按一下「Default Start Flow」,然後選取「Start」樹狀結構節點。
「開始」頁面隨即開啟。系統已在「Build」>「Pages」側欄部分中,自動選取「Start」頁面。
- 在「Start」>「Routes」中,按一下「Default Welcome Intent」。
意圖會將使用者在一個對話回合中的意圖歸類。在 Dialogflow CX 中,意圖可以做為狀態處理常式的一部分,用於將下一個有效頁面或執行要求路徑
- 移除所有「服務專員說」項目,並新增以下文字:
Welcome, I am the virtual agent of G-Records, a fictional rock label. You can order artists merchandise, ask questions about your order or shipping, and I can tell you more which artists are currently signed with us. How can I help?
為簡化對話,我們也需要一些快速回覆按鈕 / 建議晶片。
- 按一下「新增對話選項」>「自訂酬載」,然後使用下列程式碼片段。
- 將下列程式碼片段做為自訂酬載,然後點選「儲存」。
如要進一步瞭解自訂酬載,請參閱說明文件。
{
"richContent": [
[
{
"type": "chips",
"options": [
{
"text": "Which artists?"
},
{
"text": "Which products?"
},
{
"text": "About my order..."
}
]
}
]
]
}

- 請在模擬工具中測試歡迎意圖。
您可能想知道為何看不到任何豐富內容。這是因為建議晶片等豐富內容需要整合才能使用。下列步驟需要帳單帳戶,但如果您沒有帳單帳戶,可以略過這些步驟。
- 在左側邊欄中,依序點選「管理」>「整合」。
- 選擇「Dialogflow Messenger」,然後按一下「連線」。
- 在彈出式視窗中,按一下「啟用」。

畫面上會顯示另一個彈出式視窗,這次是整合 JavaScript 程式碼,您可以將這段程式碼貼到網站中,在網站上整合 Dialogflow Messenger 元件。由於我們還沒有網站,因此會直接在工具中測試虛擬代理人。

- 按一下「立即試用」連結。
- 按一下右下方的聊天機器人圖示,開啟即時通訊視窗。輸入
Hello開始對話。

目前,虛擬服務專員無法理解你點選建議晶片的意思。這是因為虛擬服務專員尚未在狀態之間切換。我們可以在 Dialogflow CX 中使用「頁面」執行這項操作。繼續進行實驗室,首先建立一些實體和意圖。
4. 實體類型
實體類型可用來控管如何從使用者輸入內容中擷取資料。Dialogflow CX 實體類型與 Dialogflow ES 實體類型非常相似。Dialogflow 提供預先定義的系統實體,可以比對許多常見的資料類型。舉例來說,您可以使用系統實體來比對日期、時間、顏色、電子郵件地址等。您也可以自行建立自訂實體來比對自訂資料。
首先,請準備所有自訂實體,然後再設計流程中的網頁。我們會建立下列實體:

建立實體
讓我們建立 Artist 實體。
- 依序按一下「管理」>「實體類型」
- 點選「+ 建立」
- 顯示名稱:
Artist - 實體:
The Google Dolls(同義詞:Google Dolls)The Goo Fighters(同義詞:Goo Fighters)G's N' Roses(同義詞:Gs and Roses)Alice Googler- 按一下「進階選項」,然後勾選「模糊比對」。(即使樂團名稱拼錯,系統可能還是會比對到正確的實體)。
- 在「進階選項」中,也請勾選「在記錄中遮蓋」。(如果拼錯樂團名稱,系統會修正記錄中的名稱)。
- 點選「儲存」。
我們也需要 Merch 項目實體:
- 依序按一下「管理」>「實體類型」
- 點選「+ 建立」
- 顯示名稱:
Merch - 實體:
T-shirtLongsleeve(同義詞:Longsleeve shirt)Tour MovieDigital Album(同義詞:MP3 Album、MP3)CD(同義詞為Disc、Physical CD)
- 點選「儲存」。
我們也需要 Album 的實體:
- 依序按一下「管理」>「實體類型」
- 點選「+ 建立」
- 顯示名稱:
Album - 實體:
LiveGreatest Hits(同義詞:Hits)
- 點選「儲存」。
我們也需要服飾尺寸的實體:
- 依序按一下「管理」>「實體類型」
- 點選「+ 建立」
- 顯示名稱:
ShirtSize - 實體:
XS(同義詞:Extra Small)S(同義詞:Small)M(同義詞:Medium)L(同義詞:Large)XL(同義詞:Extra Large)2XL(同義詞:Extra Extra Large)3XL
- 點選「儲存」。
以及訂單編號的實體,通常由 4 個英數字元和 3 個數字組成。(例如 ABCD123)
- 依序按一下「管理」>「實體類型」
- 點選「+ 建立」
- 顯示名稱:
OrderNumber - 規則運算式實體
- 實體:[A-Z]{4}[0-9]{3}
- 點選「儲存」。
實體設定應類似下列所示:
@藝人:
@Merch:
@Album:
@ShirtSize:
@OrderNumber: 
自訂實體準備就緒後,我們就可以準備意圖。讓我們繼續完成實驗室。
5. 意圖
意圖會將使用者在一個對話回合中的意圖歸類。Dialogflow CX 大幅簡化了背景資訊,不再是控制對話的建構區塊。Dialogflow CX 只會使用意圖來比對使用者所說的內容。在 Dialogflow ES 中,您必須將所有項目 (參數、事件、完成等) 繫結至意圖。Dialogflow CX 中的意圖只包含訓練詞組,因此可重複使用。不再控管對話。因此建立意圖的程序相當簡單:
意圖中的訓練詞組可使用「實體」擷取「變數」輸入內容,因此建議您預先建立實體類型,這也是我們在實驗室步驟前一頁中執行的操作。
建立意圖
首先,請準備好所有意圖,再設計流程中的網頁。
- 依序點選「管理」>「意圖」。
- 點按「+ 建立」
請使用下列詳細資料:
- 顯示名稱
redirect.artists.overview - 說明
Artists overview: The bands supported by the label

向下捲動並建立下列訓練詞組:
Which bands are signed?Which bandsWhich artistsWhich artists are part of the record labelWho is part of the labelFrom which bands can I buy merchandiseBand merchandiseWhich music do you have?I would like to know who are signed to the labelWho are supported by the labelFrom who can I buy shirtsWhat music can I orderCan I get an overview of all the artists

- 按一下 [儲存]。
- 現在繼續建立所有其他意圖。發揮想像力,想出更多訓練詞組。最佳做法是為每個意圖提供至少 10 個訓練詞組,涵蓋使用者觸發意圖的各種方式。本實驗室的目標是瞭解如何使用這些工具,因此即使沒有這麼多資料也沒關係。
請注意以下幾點:
- 請注意,輸入訓練詞組時,Dialogflow CX 會自動為實體加註。如果沒有,您可能需要更新實體 (新增同義字),或自行手動為訓練詞組加註。
- 較短的訓練詞組:Dialogflow 的 NLU 系統也能處理較短的訓練詞組,我們在此提供幾個範例。
- 過度訓練:如果意圖的訓練詞組過多,可能會導致過度訓練,結果不盡理想。最佳做法是使用反覆遞增測試,並在沒有相符意圖的情況下新增訓練詞組。
顯示名稱 | 訓練詞組 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
現在我們已準備好可重複使用的元素 (流程、實體和意圖),接下來可以建立頁面和狀態處理常式,將這些元素整合在一起。
6. 頁面和狀態處理常式
Dialogflow CX 對話 (工作階段) 可以描述為有限狀態機,並以視覺化方式呈現。以自動販賣機為例,可以將其模擬為有限狀態機。這個狀態機有下列狀態:等待硬幣、選取糖果、給予糖果,並根據一組輸入內容在這些狀態之間移動。舉例來說,投入硬幣後,販賣機會從「等待投入硬幣」狀態轉為「選取糖果」。頁面是我們為 Dialogflow CX 虛擬服務專員模擬這些狀態的方式。
當使用者在對話中與 Dialogflow CX 互動時,對話會從一個頁面移至另一個頁面,因此在任何時間點,只有一個頁面是目前頁面,目前頁面會視為有效,與該頁面相關聯的流程也會視為有效。
您可為每個「流程」定義許多頁面,好讓這些頁面能夠合併起來,處理流程設計主題的完整對話。每個流程都有專屬的開始頁面。流程剛啟用時,開始頁面會成為目前頁面。在每次對話中,目前的頁面會維持不變或轉換至其他頁面。這個概念可讓您建立包含多個頁面和多輪對話的大型代理程式。
頁面包含完成動作 (靜態輸入對話和/或 Webhook)、參數和狀態處理常式。對話控制是透過狀態處理常式進行,可讓您建立各種轉移路徑,轉移至其他 Dialogflow CX 頁面,包括設定條件 (用於對話分支)。
對話的狀態是由處理頁面間的轉場效果所控制,共有三種不同類型的路徑:
- 意圖路徑:應比對意圖的時間 (例如根據使用者說的內容變更網頁)。(視覺化圖表中的藍線)。
- 條件路徑:應檢查條件的時間 (例如根據工作階段中儲存的特定參數變更網頁) (視覺化圖表中的橘色線條)。
- 事件處理常式:應處理特定備援事件時 (例如處理沒有輸入內容、沒有相符項目,以便向使用者明確指出意圖或條件路徑) (視覺化圖表中的綠線)。
對話語音內容 (即傳回給使用者的內容或回應) 由執行要求定義,可以是靜態或動態:
- 靜態完成:提供靜態完成回應時
- 動態執行要求:在呼叫執行要求 Webhook 以取得動態回應時
針對零售機器人,我們會建立一些意圖路徑,並提供一些靜態進入完成回應,網頁一經啟用就會向使用者顯示。稍後,我們會使用條件路徑建立參數,收集下訂單所需的資訊。
頁面意圖路徑
在預設開始流程中建立頁面
以下是預設啟動流程的流程圖:

讓我們一起點選:
- 依序點選「Build」>「Default Start Flow」
- 按一下「開始頁面」
- 按一下「路徑」旁的「+」圖示
- 新增 redirect.artists.overview
- 向下捲動至「轉換」,然後轉換至「目錄」流程。
- 按一下「儲存」
- 針對
redirect.product.overview和這個表格中的其他 11 個資料列重複上述步驟:
頁面 (流程中) | 路徑 > 意圖 | 路徑 > 轉換至 |
開始 |
| - |
開始 |
| 流程:目錄 |
開始 |
| 流程:目錄 |
開始 |
| 流程:目錄 |
開始 |
| 流程:目錄 |
開始 |
| 流程:目錄 |
開始 |
| 流程:目錄 |
開始 |
| 流程:客戶服務 |
開始 |
| 流程:客戶服務 |
開始 |
| 流程:客戶服務 |
開始 |
| 流程:我的訂單 |
開始 |
| 流程:我的訂單 |
開始 |
| 流程:我的訂單 |
開始 |
| 頁面:結束工作階段 |

預設開始流程的運作方式,與撥打電話給客服中心時的選項選單類似。不過,這個虛擬服務專員是透過自然語言訓練,意圖中含有訓練詞組。因此,互動是由對話驅動,而非 DTMF 選項,更自然且更像人類。

在目錄流程中建立頁面
以下是目錄流程的對話記錄:
> "Hi"
"Welcome, I am the virtual agent of G-Records, a fictional rock label.
You can order artists merchandise, ask questions about your order or shipping,
and I can tell you more which artists are currently signed with us. How can I help?"
> "Which bands are signed with this record label?"
"The following bands are signed with G-Records:
Alice Googler, G's N' Roses, The Goo Fighters and The Google Dolls.
From which of these artists would you like to order merchandise?"
> "Alice Googler"
"You want to rock with Alice Googler merchandise. Awesome!
We sell shirts, music or the tour movie.
Which merchandise item do you want?"
"(Suggestion chips: [Shirts] [Music] [Tour Movie])"
> "I would like to buy a Shirt"
"Do you want a longsleeve or a t-shirt?"
"(Suggestion chips: [T-shirt, Longsleeve, Price?])"
> "What's the price difference?"
"A t-shirt costs $25 and a longsleeve costs $30.
Do you want a longsleeve or a t-shirt?"
> "A t-shirt",
"What shirt size do you want?"
"(Suggestion chips: [XS, S, M, L, XL, 2XL, 3XL])"
> "M"
"A T-shirt of Alice Googler size: M costs $25. Shall I continue to order?"
選擇「音樂」或「巡迴演唱會電影」時,對話方塊會有所不同。選擇「音樂」時,對話方塊會如下所示:
> "Music"
"We have a Greatest Hits Album or the Live Album. Which one do you want?"
"(Suggestion chips: [Greatest Hits, Live, Price?])"
> "The Live Album"
"Do you want this album on CD or MP3?"
"(Suggestion chips: [CD, MP3])"
> "What's the price difference?"
"A CD costs $15. The digital album on MP3 costs $10.
Do you want this album on CD or MP3?"
> "Digital Album",
"The Digital Album: Alice Googler - Live costs $10. Shall I continue to order?"
如果是 Tour Movie,對話方塊會如下所示:
> "Tour Movie"
"The Tour Movie of G's N' Roses costs $25. Shall I continue to order?
以下是目錄流程中所有頁面的流程圖:

請注意這個流程的複雜程度:
- 我可以略過「你喜歡哪些藝人?」這個問題,直接詢問「有哪些周邊商品?」。
- 在預設啟動流程中,我可以詢問「我想購買 The Google Dolls T 恤」或「我想購買商品」。也就是說,虛擬服務專員會詢問後續問題,填入這些必要參數的空位。直接跳轉至產品頁面。
- 「價格」對話方塊來自將重複使用的「價格」頁面。
- 雖然「Tour Movie」的對話方塊看起來最簡單,但我們實際上會對其進行特殊處理。我們會重複使用對話的這部分,因此如果使用者一次輸入所有資訊,也可以直接輸入其他產品的資訊:
> "I want The Goo Fighters longsleeve size S."
"The longsleeve of The Goo Fighters size S costs $30. Shall I continue to order?"
首先,請先連結網頁。
- 依序點按「建構」>「目錄」
- 按一下「開始頁面」
- 按一下「路徑」旁的「+」圖示
- 新增 redirect.artists.overview
- 向下捲動至「轉場效果」,選取「頁面」,然後選擇「+ 新增頁面」。
- 使用頁面名稱:
Artist Overview,然後按一下「儲存」
現在,讓我們完成其餘流程:
- 您可以對下列網頁、意圖和完成事項重複上述步驟。接管這張桌子。「頁面」是您在流程中選取的頁面,「路徑」>「轉換至」是您要建立並轉換至的新流程或頁面。
頁面 (流程中) | 路徑 > 意圖 | 路徑 > 轉換至 |
目錄開始 |
| 藝人總覽 |
目錄開始 |
| 產品 |
目錄開始 |
| 產品總覽 |
目錄開始 |
| 產品總覽 |
目錄開始 |
| 襯衫 |
目錄開始 |
| 音樂 |
目錄開始 |
| 結束工作階段 |
目錄開始 |
| 結束流程 |
藝人總覽 |
| 產品總覽 |
現在繼續新增更多靜態履行項目。
- 在目錄流程中,按一下「藝人總覽」頁面。
- 在「Entry fulfillment」(項目執行要求) 區段中,按一下「Edit fulfillment」(編輯執行要求)。
- 請使用下列靜態完成動作 (Agent says):
The following bands are signed with G-Records: Alice Googler, G's N' Roses, The Goo Fighters and The Google Dolls.
- 點選「儲存」。
- 在目錄流程中,按一下「產品總覽」頁面。
- 在「Entry fulfillment」(項目執行要求) 區段中,按一下「Edit fulfillment」(編輯執行要求)。
- 使用下列靜態出貨 (Agent says):
We sell shirts, music or the tour movie.
- 按一下 [儲存]。
網頁參數
參數可用於擷取及參照使用者在工作階段中提供的值。每個參數都有名稱和實體類型。@Artist 和 @Merch 是我們收集商品訂單的必要參數。如果是 T 恤或長袖上衣,你還需要收集 @ShirtSize,如果想訂購音樂,則需要 @Carrier 和 @Album 名稱。
這些參數必須標示為「required」(必要)。一旦需要提供自訂提示,請務必提供,讓系統記住使用者,並提供正確答案,以便收集這些參數。Dialogflow CX 提供幾種機制,可協助您達成這個目標。
舉例來說,您可以在「參數」部分提供自訂靜態完成訊息。如果參數為必要參數,系統就會顯示這些參數的填入狀態。這些回覆訊息會新增至回覆佇列。在代理程式的回合中,可以呼叫多個完成動作,每個動作都可能會產生回應訊息 (有時也建議這麼做)。Dialogflow 會將這些回應保留在回應佇列中。如要進一步瞭解網頁生命週期,以及這些完成項目加入回應佇列的順序,請參閱 Dialogflow CX 網頁文件。
在「藝人總覽」頁面建立參數
讓我們定義一些網頁參數:
- 在「目錄」流程中,按一下「藝人總覽」頁面。
- 按一下「Parameters」區塊中的「+」。新增 artist 參數:
- 顯示名稱:
artist - 實體類型:
@Artist - 必要:勾選
- 在記錄中遮蓋:勾選
- 現在,我們要新增一些自訂參數填入訊息。如果虛擬代理程式尚未收集藝人參數,使用者會在回覆佇列中看到這則代理程式回覆:
From which of these artists would you like to order merchandise?
- 新增第二個對話選項,提供豐富的建議方塊。按一下「新增對話選項」,然後使用以下 JSON 程式碼:
{
"richContent": [
[
{
"options": [
{
"text": "The Google Dolls"
},
{
"text": "The Goo Fighters"
},
{
"text": "Alice Googler"
},
{
"text": "G's N' Roses"
}
],
"type": "chips"
}
]
]
}
您可以根據使用者嘗試回答問題的次數,處理不同的備用履行提示。您可以使用參數事件處理常式執行這項操作。您可以選擇各種內建事件處理常式,例如「無效參數」、「語音太長」、「沒有輸入」、「沒有輸入 (第一次嘗試)」、「第二次嘗試」或「沒有相符項目」。「沒有輸入內容」和「沒有相符項目」的差異在於,前者是指使用者從未提供答案,後者則是指使用者提供了答案,但 Dialogflow CX 無法將答案與頁面意圖相符。
- 向下捲動至「Reprompt event handlers」(重新提示事件處理常式) 部分。
- 按一下「新增事件處理常式」,然後選取事件:
No-match default - 請使用下列活動靜態文字填寫:
I missed that. Please, specify the artist. You can choose between: Alice Googler, G's N' Roses, The Google Dolls or The Goo Fighters. Which artist do you want to buy merchandise from?
- 點選「儲存」。
- 按一下「新增事件處理常式」,然後選取事件:
No-input default - 請使用下列活動靜態文字填寫:
I am sorry, I could understand the artist's name. You can choose between Alice Googler, G's N' Roses, The Google Dolls or The Goo Fighters. Which artist do you want to buy merchandise from?
- 點選「儲存」。
頁面條件路徑
參數搭配網頁條件式路徑使用時,功能非常強大。如果條件評估結果為 true,系統就會呼叫相關聯的網頁路徑。條件可以是「參數等於特定值」、「參數不得遺漏」或「已填寫表單」等等。如要進一步瞭解「參數」和「條件」,請參閱 Dialogflow CX 說明文件。
對於零售虛擬代理程式,我們需要收集一系列參數,因此需要建立條件,檢查「表單」是否已填寫完畢。表單是應向網頁使用者收集的參數清單。虛擬服務專員會與使用者進行多輪對話,直到收集到所有必要表單參數 (也稱為網頁參數) 為止。
Dialogflow CX 會自動設定使用者在填寫表單時提供的參數值。如要檢查目前網頁的完整表單是否已填寫,請使用下列條件:$page.params.status = "FINAL"
在藝人總覽頁面建立條件式路徑
讓我們建立條件式路徑,在得知藝人後轉換至下一頁:
- 在「藝人總覽」頁面中,按一下「路線」部分中的「+」圖示。
- 向下捲動至「條件」部分。
- 選取「至少一個」 (或)
- 接著,我們會編寫運算式,
- 參數:
$page.params.status - 運算子:
= - 值:
"FINAL"
- 現在,我們將在路徑上建立特定的靜態完成訊息,確認使用者的選擇。向下捲動至「Fulfillment」(執行要求) 區塊,然後撰寫下列執行要求訊息:
$session.params.artist, great choice! Rock on!You want to rock with $session.params.artist merchandise. Awesome!
- 條件為 true 時,應轉換至「產品總覽」頁面。向下捲動至「轉換」部分,然後使用下列頁面:
Product Overview - 按一下 [儲存]。

在「產品總覽」頁面建立路徑
現在我們已瞭解如何建立參數和條件式路徑,接著為下列網頁建立更多參數:
產品總覽
- 在「產品總覽」頁面中建立
artist參數:
- 顯示名稱:
artist - 實體類型:
@Artist - 必要:勾選
- 在記錄中遮蓋:勾選
- 初始提示詞完成:
From which of these artists would you like to order merchandise?
{
"richContent": [
[
{
"options": [
{
"text": "The Google Dolls"
},
{
"text": "The Goo Fighters"
},
{
"text": "Alice Googler"
},
{
"text": "G's N' Roses"
}
],
"type": "chips"
}
]
]
}
- 事件處理常式 >
No-match default:To buy merchandise you can choose between the following artists: Alice Googler, G's N' Roses, The Google Dolls or The Goo Fighters. Which artist do you want to buy merchandise from? - 自訂酬載:
{
"richContent": [
[
{
"options": [
{
"text": "The Google Dolls"
},
{
"text": "The Goo Fighters"
},
{
"text": "Alice Googler"
},
{
"text": "G's N' Roses"
}
],
"type": "chips"
}
]
]
}
- 事件處理常式 >
No-input default:To buy merchandise you can choose between the following artists: Alice Googler, G's N' Roses, The Google Dolls or The Goo Fighters. Which artist were you trying to mention? - 自訂酬載:
{
"richContent": [
[
{
"options": [
{
"text": "The Google Dolls"
},
{
"text": "The Goo Fighters"
},
{
"text": "Alice Googler"
},
{
"text": "G's N' Roses"
}
],
"type": "chips"
}
]
]
}
- 建立
merch參數:
- 顯示名稱:
merch - 實體類型:
@Merch - 必要:勾選
- 在記錄中遮蓋:勾選
- 完成:
Which merchandise item do you want? - 按一下「新增對話方塊選項」>「自訂酬載」:
{
"richContent": [
[
{
"type": "chips",
"options": [
{
"text": "Shirts"
},
{
"text": "Music"
},
{
"text": "Tour movie"
}
]
}
]
]
}
- 事件處理常式 >
No-match default - 事件處理常式完成:
We sell Shirts, Music or the Tour movie. Which of these items do you want? - 自訂酬載:
{
"richContent": [
[
{
"type": "chips",
"options": [
{
"text": "Shirts"
},
{
"text": "Music"
},
{
"text": "Tour movie"
}
]
}
]
]
}
- 事件處理常式 >
No-input default - 事件處理常式完成:
I couldn't understand which merchandise item you wanted to buy. You can choose between: Shirts, Music or the Tour movie. Which item do you want? - 自訂酬載:
{
"richContent": [
[
{
"type": "chips",
"options": [
{
"text": "Shirts"
},
{
"text": "Music"
},
{
"text": "Tour movie"
}
]
}
]
]
}
- 建立路由,在提供
artist和merch項目時,轉換至「產品」頁面。
- 條件:
- 符合所有規則 (AND)
- 運算式:
$session.params.artist != null - 運算式:
$session.params.merch != null - 完成:
Alright! $session.params.merch of $session.params.artist, let's go! - 轉場效果:頁面:
Product
- 建立使用者說出「襯衫」時的路線
- 意圖:redirect.shirts
- 轉場效果:頁面:
Shirts
- 為使用者說出「音樂」時建立路徑
- 意圖:redirect.music
- 轉場效果:頁面:
Music
- 建立使用者詢問價格資訊時的路線
- 意圖:redirect.price
- 轉場效果:建立新頁面:
Price
設定上述設定後,您會看到類似下圖的視覺化內容。請注意,意圖路徑在圖表中會以藍色顯示,條件路徑則會以橘色顯示。雖然未顯示,但事件處理常式為綠色,且當多個路徑類型轉換至某個頁面時,線條會變成灰色。

到目前為止,您已學會如何使用狀態處理常式 (例如:意圖路徑和條件路徑 (根據參數)) 建立流程、實體、意圖和頁面。在本實驗室稍後的部分,我們將在完成動作中使用條件分支,根據輸入內容提供不同的對話。
您可以使用下列設定,完成虛擬代理程式。
襯衫頁面:
- 在「Shirts」頁面中建立下列設定:
- 填寫項目:
Do you want a longsleeve or a t-shirt? - 項目執行要求自訂酬載:
{
"richContent": [
[
{
"type": "chips",
"options": [
{
"text": "T-shirt"
},
{
"text": "Longsleeve"
},
{
"text": "Price?"
}
]
}
]
]
}
- 建立意圖路徑:
redirect.price轉換至Price頁面 - 建立下列參數:
- 參數:
merch- 實體類型:@Merch、Required和Redact in log - 參數 > 事件處理常式 >
No-match default - 參數 > 事件處理常式完成:
You can choose between a t-shirt or a longsleeve. Which of these do you want? - 參數 > 事件處理常式完成自訂酬載:
{
"richContent": [
[
{
"type": "chips",
"options": [
{
"text": "T-shirt"
},
{
"text": "Longsleeve"
}
]
}
]
]
}
- 參數 > 事件處理常式 >
No-input default - 參數 > 事件處理常式完成:
I couldn't understand if you want the t-shirt or the longsleeve. Which of these do you want? - 參數 > 事件處理常式完成自訂酬載:
{
"richContent": [
[
{
"type": "chips",
"options": [
{
"text": "T-shirt"
},
{
"text": "Longsleeve"
}
]
}
]
]
}
- 按一下項目填寫內容,然後向下捲動至「參數預設值」。每當「襯衫」頁面啟用時,類別參數就會設為「襯衫」:
參數 | 值 |
|
|
- 新增條件式路徑:
- 符合至少一項規則 (OR)
- 運算式:
$session.params.merch = "T-shirt" - 運算式:
$session.params.merch = "Longsleeve" - 轉移至新頁面:
Shirt Size
價格頁面:
由於價格訊息會因所選商品項目或類別 (音樂或襯衫) 而異,我們會在實驗室稍後修正這部分。目前只要輸入預留位置即可。
- 在「價格」頁面中建立下列設定:
- 填寫項目:
PRICE TODO
由於你可以在對話中的各種位置要求價格,因此系統一律會提供答案,並將你轉回對話的先前部分,以便繼續訂購。對話樹中有 5 個位置可分支,以取得價格資訊。(襯衫、襯衫尺寸、音樂、貨運公司,以及透過 Intent Route 直接傳送),因此我們需要一些條件式路徑來返回:
- 新增條件式路徑:
- 符合所有規則 (AND)
- 運算式:
$session.params.category = "shirts" - 運算式:
$session.params.merch = "null" - 轉移至新頁面:
Shirts
- 新增條件式路徑:
- 符合所有規則 (AND)
- 運算式:
$session.params.category = "shirts" - 運算式:
$session.params.size = "null" - 轉移至新頁面:
Shirt Size
- 新增條件式路徑:
- 符合所有規則 (AND)
- 運算式:
$session.params.category = "music" - 運算式:
$session.params.album = "null" - 轉移至新頁面:
Music
- 新增條件式路徑:
- 符合所有規則 (AND)
- 運算式:
$session.params.category = "music" - 運算式:
$session.params.merch = "null" - 轉移至新頁面:
Carrier
- 新增條件式路徑:
- 符合所有規則 (AND)
- 運算式:
$session.params.category = "null" - 轉移至新頁面:
Product Overview
襯衫尺寸頁面:
- 在「Shirt Size」頁面中建立下列設定:
- 填寫項目:
What shirt size do you want? - 項目執行要求自訂酬載:
{
"richContent": [
[
{
"type": "chips",
"options": [
{
"text": "XS"
},
{
"text": "S"
},
{
"text": "M"
},
{
"text": "L"
},
{
"text": "XL"
},
{
"text": "2XL"
},
{
"text": "3XL"
}
]
}
]
]
}
- 建立意圖路徑:
redirect.price轉換至Price頁面。 - 建立下列參數:
- 參數:
shirtsize- 實體類型:@ShirtSize-Required、Redact In Log - 參數 > 事件處理常式 >
No-match default - 參數 > 事件處理常式完成:
Please tell me the shirt size, such as XL. - 參數 > 事件處理常式完成自訂酬載:
{
"richContent": [
[
{
"type": "chips",
"options": [
{
"text": "XS"
},
{
"text": "S"
},
{
"text": "M"
},
{
"text": "L"
},
{
"text": "XL"
},
{
"text": "2XL"
},
{
"text": "3XL"
}
]
}
]
]
}
- 參數 > 事件處理常式 >
No-input default - 參數 > 事件處理常式完成:
I couldn't understand the shirt size. What size do you want? - 參數 > 事件處理常式完成自訂酬載:
{
"richContent": [
[
{
"type": "chips",
"options": [
{
"text": "XS"
},
{
"text": "S"
},
{
"text": "M"
},
{
"text": "L"
},
{
"text": "XL"
},
{
"text": "2XL"
},
{
"text": "3XL"
}
]
}
]
]
}
- 新增條件式路徑:
- 符合所有規則 (AND)
- 運算式:
$page.params.shirtsize != "null" - 轉場至頁面:
Product
音樂頁面:
- 在「Music」頁面中建立下列設定:
- 填寫項目:
We have a Greatest Hits Album or the Live Album. Which one do you want? - 項目執行要求自訂酬載:
{
"richContent": [
[
{
"type": "chips",
"options": [
{
"text": "Greatest Hits"
},
{
"text": "Live"
},
{
"text": "Price?"
}
]
}
]
]
}
- 建立意圖路徑:
redirect.price轉換至「頁面」:Price。 - 建立下列參數:
- 參數:
album- 實體類型:@Album-Required、Redact In Log - 參數 > 事件處理常式 >
No-match default - 參數 > 事件處理常式完成:
You can choose between Greatest Hits and Live Album. Which of these do you want? - 參數 > 事件處理常式完成自訂酬載:
{
"richContent": [
[
{
"type": "chips",
"options": [
{
"text": "Greatest Hits"
},
{
"text": "Live"
}
]
}
]
]
}
- 參數 > 事件處理常式 >
No-input default - 參數 > 事件處理常式完成:
I couldn't understand if you want the album: Greatest Hit or Live. Which of these do you want? - 參數 > 事件處理常式完成自訂酬載:
{
"richContent": [
[
{
"type": "chips",
"options": [
{
"text": "Greatest Hits"
},
{
"text": "Live"
}
]
}
]
]
}
- 按一下項目填寫內容,然後向下捲動至「參數預設集」。每次音樂頁面處於啟用狀態時,系統都會將類別參數設為「music」:
參數 | 值 |
|
|
- 新增條件式路徑:
- 符合所有規則 (AND)
- 運算式:
$page.params.album != "null" - 轉場至頁面:
Carrier
貨運公司頁面:
- 在「Carrier」(貨運公司) 頁面中建立下列設定:
- 填寫項目:
Do you want this album on CD or MP3? - 項目執行要求自訂酬載:
{
"richContent": [
[
{
"type": "chips",
"options": [
{
"text": "CD"
},
{
"text": "MP3"
},
{
"text": "Price?"
}
]
}
]
]
}
- 建立意圖路徑:
redirect.price轉換至Price頁面。 - 建立下列參數:
- 參數:
merch- 實體類型:@Merch-Required、Redact In Log - 參數 > 事件處理常式 >
No-match default - 參數 > 事件處理常式完成:
Do you want a physical CD or the digital album? - 參數 > 事件處理常式完成:自訂酬載:
{
"richContent": [
[
{
"type": "chips",
"options": [
{
"text": "CD"
},
{
"text": "Digital Album"
}
]
}
]
]
}
- 參數 > 事件處理常式 >
No-input default - 參數 > 事件處理常式完成:
I couldn't understand if you mean CD or MP3. Which one do you want? - 參數 > 事件處理常式完成:自訂酬載:
{
"richContent": [
[
{
"type": "chips",
"options": [
{
"text": "CD"
},
{
"text": "MP3"
}
]
}
]
]
}
- 新增條件式路徑:
- 符合所有規則 (AND)
- 運算式:
$page.params.merch != "null" - 轉場至頁面:
Product
產品頁面:
- 建立下列參數:
參數顯示名稱 | 參數實體類型 | 檢查 |
|
| 必要,在記錄中遮蓋 |
|
| 必要,在記錄中遮蓋 |
- artist 參數需要下列初始提示填寫內容,當系統無法辨識藝人時,就會顯示這些內容。
You didn't mention which artist you are interested in. You can ask me to buy the $session.params.merch of the artist you like or ask which artists we signed. How can I help?
{
"richContent": [
[
{
"options": [
{
"text": "Which artists?"
}
],
"type": "chips"
}
]
]
}
- 此外,請新增執行要求的
No-input default事件處理常式:I couldn't understand what you just said. Ask me which artists are signed. - 以及含有執行的
No-match default事件處理常式:I missed that. Please ask me which artists are signed.
- merch 參數也需要重新提示事件處理常式。
- 新增具有出貨地點的
No-input default事件處理常式:I couldn't understand what you just said. Which merchandise item do you want? - 以及含有執行的
No-match default事件處理常式:I missed that. Which merchandise item do you want?
如果系統知道藝人是誰,且使用者選擇「巡迴演唱會電影」,下一個路徑會轉移至確認頁面。
- 新增條件式路徑:
- 符合所有規則 (AND)
- 運算式:
$session.params.artist != null - 運算式:
$session.params.merch = "Tour Movie" - 參數預設集「新增參數」>
price = 25 - 轉移至新頁面:
Confirmation
如果系統知道藝人是誰,且使用者選擇「T 恤」並選取尺寸,下一個路徑會轉移至確認頁面。
- 新增條件式路徑:
- 自訂運算式:
$session.params.artist != null AND $session.params.merch = "T-shirt" AND $session.params.shirtsize != null - 參數預設集「新增參數」>
price = 25 - 轉場至頁面:
Confirmation
如果系統知道藝人是誰,且使用者選擇「長袖」並選取襯衫尺寸,下一個路徑會轉移至確認頁面。
- 新增條件式路徑:
- 自訂運算式:
$session.params.artist != null AND $session.params.merch = "Longsleeve" AND $session.params.shirtsize != null - 參數預設集「新增參數」>
price = 30 - 轉場至頁面:
Confirmation
如果系統知道藝人是誰,且使用者選擇「CD」並選取專輯名稱,下一個路徑就會轉移至確認頁面。
- 新增條件式路徑:
- 自訂運算式:
$session.params.artist != null AND $session.params.merch = "CD" AND $session.params.album != null - 參數預設集「新增參數」>
price = 15 - 轉場至頁面:
Confirmation
如果系統知道藝人身分,且使用者選擇「數位專輯」並選取專輯名稱,下一個路徑會轉移至確認頁面。
- 新增條件式路徑:
- 自訂運算式:
$session.params.artist != null AND $session.params.merch = "Digital Album" AND $session.params.album != null - 參數預設集「新增參數」>
price = 10 - 轉場至頁面:
Confirmation
接下來,我們將使用提示建立一些進階條件,偵測缺少的資訊。如果系統知道藝人是誰,且使用者選擇「CD」或「數位專輯」,但未選擇專輯名稱,下一個路徑會轉回音樂頁面。
- 新增條件式路徑:
- 自訂運算式:
$session.params.artist != null AND ($session.params.merch = "CD" OR $session.params.merch = "Digital Album") AND $session.params.album = null - 完成:
I would also need to know which album you would like to buy! - 轉場至頁面:
Music
如果系統知道藝人是誰,且使用者選擇「T 恤」或「長袖上衣」,但未選擇 T 恤尺寸,最後一個路徑會轉換為確認頁面。
- 新增條件式路徑:
- 自訂運算式:
$session.params.artist != null AND ($session.params.merch = "T-shirt" OR $session.params.merch = "Longsleeve") AND $session.params.shirtsize = null - 完成:
I would also need to know which shirt size you need! - 轉場至頁面:
Shirt Size
在實驗室的下一部分,我們會使用條件式完成,根據輸入內容提供不同的完成訊息。
7. 條件式回應
系統會根據輸入內容傳回不同的對話,對話會分支,我們稱之為「條件式回覆」。如果您未使用 Webhook 執行要求,後端會判斷條件式回應,這時就會變得很有趣。範例如下:
if [condition]
[response]
elif [condition]
[response]
elif [condition]
[response]
else
[response]
endif
- [條件] 的範例可以是:
$session.params.user-age >= 21。格式與路徑中的條件類似。 - [response] 會採用靜態文字回應
- 條件式回應一律以
if開頭 elif和else區塊為選用項目
Dialogflow CX 也可以使用內建的系統函式。例如設定日期或時間格式,或是顯示目前時間 ($sys.func.NOW())
我們來修正「確認」和「價格」頁面,完成「目錄」流程。
確認頁面:
現在我們要建立確認頁面。必須符合下列條件:
- 如果商品是 CD 或數位專輯。確認畫面會顯示下列欄位:artist、merch、album 和 price。
- 如果商品是 T 恤或長袖上衣,確認畫面會顯示以下欄位:artist、merch、size 和 price。
- 否則 (因此如果 merch 是 Tour Movie)。確認畫面會顯示下列欄位:藝人、商品和價格。
- 按一下「確認」頁面。
- 依序點選「編輯完成」>「專員回覆」>「新增對話」選項 >「條件式回覆」:
if ($session.params.merch = "CD" OR $session.params.merch = "Digital Album")
The $session.params.merch: $session.params.artist - $session.params.album costs $$session.params.price. Shall I continue to order?
elif ($session.params.merch = "T-shirt" OR $session.params.merch = "Longsleeve")
A $session.params.merch of $session.params.artist size: $session.params.shirtsize costs $$session.params.price. Shall I continue to order?
elif $session.params.merch = "Tour Movie"
The $session.params.merch of $session.params.artist costs $$session.params.price. Shall I continue to order?
else
It looks like something went wrong with your order. You can say "Reset", to restart the order process.
endif
- Create the following Custom payload:
- Custom payload:
{
"richContent": [
[
{
"options": [
{
"text": "Yes, confirm"
}
],
"type": "chips"
}
]
]
}
Next, create two intent routes:
confirm.proceed.ordertransitions to:Order ProcessFlow.decline.proceed.ordertransitions toEnd Flow
When the user declines the order, and does not want to proceed the order process, we will have to transition back to the welcome page, but all the parameters have to be cleared. We can do this by specifically setting null to all the possible parameters. You can do this with Parameter presets.
- In the decline.proceed.order intent route, scroll down to Parameter presets and add the following parameters:
Parameter | Value |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Notice that we have created an additional parameter called restart. If this parameter is present, the Default Start Flow, should know to continue the conversation by showing a customized message.
- Click on the Default Start Flow, Start Page, and create another Conditional Route:
$session.params.restart = "true"- Fulfillment:
"Welcome back, as the virtual agent of G-Records, I can help you order artists merchandise, you can ask questions about your order or shipping, and I can tell you more which artists are currently signed with us. How can I help?" - Custom payload:
{
"richContent": [
[
{
"type": "chips",
"options": [
{
"text": "Which artists?"
},
{
"text": "Which products?"
},
{
"text": "About my order..."
}
]
}
]
]
}
- Select the Start Page and click on the
redirect.endintent. Create the following fulfillment:Thank you for contacting G-Records! Have a nice day!
Price Page:
Let's also fix the Price TODOs. The price information will be static for now. Click on the Price Page in the Catalog Flow, and use the following entry fulfillment:
- Delete the Agent Says entry fulfillment.
- Create a new Conditional Response:
if $session.params.category = "shirts"
A t-shirt costs $25 and a longsleeve costs $30.
elif $session.params.category = "music"
A CD costs $15. The digital album on MP3 costs $10.
else
A t-shirt costs $25 and a longsleeve costs $30. A CD costs $15 and a digital album on MP3 $10. In case you are interested in the Tour Movie, that one is $25.
endif

Well done, by now you completed the Catalog flow. Your flow should look similar to this diagram:
8. Wrapping up the agent
We are almost at the end of this lab. Let's configure the last flows together, and take in practice all the new things that we have learned.
Creating the My Order Flow
- Go to the My Order Flow, and create the following intent transitions:
Page (In Flow) | Routes > Intent | Routes > Transition To |
My Order Start |
| My Order |
My Order Start |
| My Order Status |
My Order Start |
| My Order Cancellation |
My Order Start |
| End Session |
My Order Start |
| End Flow |
My Order |
| My Order Status |
My Order |
| My Order Cancellation |
Default Start Flow |
| Flow: My Order |
Default Start Flow |
| Flow: My Order |
- Let's create the following entry fulfillment for the My Order Page:
- Entry fulfillment:
I can look up the status of your order, or I can cancel an order.
- In the My Order Page create the following parameter:
- Displayname:
ordernumber - Entity Type:
@OrderNumber - Required: checked
- Initial prompt fulfillment:
What's the order number? For example ABCD123. - Event Handler:
No-match default:To proceed with your order I will need an order number. Order numbers start with 4 characters and end with 3 numbers, such as ABCD123. Which order number may I use? - Event Handler:
No-input default:I missed that. To proceed with your order I will need an order number. Order numbers start with 4 characters and end with 3 numbers, such as ABCD123. Which order number may I use?
- Create the following conditional route:
- Customize Expression:
$page.params.status = "FINAL" - Fulfillment:
And do you want to Cancel your order, or should I look up the status?
- Click on Add state handler > Event Handlers and create the Event Handler:
No-input default
- Fulfillment:
I'm sorry, what was that? Would you like me to cancel an order or look up the status? - Custom payload:
{
"richContent": [
[
{
"options": [
{
"text": "Status"
},
{
"text": "Cancel"
}
],
"type": "chips"
}
]
]
}
- Create the Event Handler:
No-match default
- Fulfillment:
Would you like me to cancel an order or lookup the status? - Custom payload:
{
"richContent": [
[
{
"options": [
{
"text": "Status"
},
{
"text": "Cancel"
}
],
"type": "chips"
}
]
]
}
- In the My Order Status Page create the following parameter:
- Displayname:
ordernumber - Entity Type:
@OrderNumber - Required checked
- Initial prompt fulfillment:
What's the order number? For example ABCD123. - Event Handler:
No-match default:To proceed with your order I will need an order number. Order numbers start with 4 characters and end with 3 numbers, such as ABCD123. Which order number may I use? - Event Handler:
No-input default:I missed that. To proceed with your order I will need an order number. Order numbers start with 4 characters and end with 3 numbers, such as ABCD123. Which order number may I use?
- In the My Order Status Page create the following conditional route:
- Customize Expression:
$session.params.ordernumber != null - Fulfillment:
Your order $session.params.ordernumber has been shipped, it can take up to approx 2 weeks before you will receive your items. - Add dialogue option > Text:
Is there anything else I can help you with?
- In the My Order Cancelation Page create the following parameter:
- Displayname:
ordernumber - Entity Type:
@OrderNumber - Required checked
- Initial prompt fulfillment:
What's the order number? For example ABCD123. - Event Handler:
No-match default:To proceed with your order I will need an order number. Order numbers start with 4 characters and end with 3 numbers, such as ABCD123. Which order number may I use? - Event Handler:
No-input default:I missed that. To proceed with your order I will need an order number. Order numbers start with 4 characters and end with 3 numbers, such as ABCD123. Which order number may I use?
- In the My Order Cancelation Page create the following conditional route:
- Customize Expression:
$session.params.ordernumber != null - Fulfillment:
Your order $session.params.ordernumber has been canceled. - Add dialogue option > Text:
Is there anything else I can help you with?
- Test the flow and create the following two test scenarios:
>"About my order"
>"ABCD123"
>"Status"
And:
>"What's the status of order DEFG222"
- Select the Start Page and click on the
redirect.endintent. Create the following fulfillment:Thank you for contacting G-Records! Have a nice day! - Select the Start Page and click on the
redirect.homeintent. Create the following parameter preset:restart = true
Default Negative intents (Fallback)
When you create a virtual agent, a default negative intent is created for you. You can add training phrases to this intent that act as negative examples that will trigger a No-match event. There may be cases where end-user input has a slight resemblance to training phrases in normal intents, but you do not want these inputs to match any normal intents.
- Try in the simulator:
I don't like Alice Googler.
You will see that the virtual agent answers with the Product Overview Page, to continue ordering Alice Googler merchandise. However, your end user does not like that artist. Let's use the Default Negative Intent for this.
- Go to Manage > Intents and select the Default Negative Intent.
- Add the following training phrases that will trigger the No-match event.
I don't like Alice GooglerI am not a fan of G's N' RosesI can't stand the music of the Google Dolls
- Hit Save and test the following sentence in the simulator:
I am really not a fan of the Goo Fighters
This time the No-match event was triggered, you stayed on the Start Page.
Default Fallback Messages
- Click the Default Start Flow, select the
sys.no-input-defaultevent handler.
The No-input fallback basically means: No text or speech answers were detected. Likely no answers were given, or the system couldn't hear it. Therefore, let's make the fallback messages more specific. Use the tab key, to create alternative dialogues:
- Remove all answers, and add these text dialogues:
I'm sorry, I didn't receive an answer. Can you say it again?I missed your answer, can you say it again?Sorry, I didn't hear anything. Can you say it again?I couldn't hear what you were saying, what was that?I'm sorry, I missed your answer. What were you trying to say?
Don't forget to click Save.
- Click the Default Start Flow, select the
sys.no-match-defaultevent handler.
The No Match fallback basically means: Text or speech answers were detected but nothing in Dialogflow CX got matched.
- Remove all answers, and add these text dialogues:
Sorry, I didn't get that. Can you please rephrase?I'm sorry, I don't understand. Can you please rephrase?I don't understand, please rephrase.Sorry, I didn't get that. What was that?I didn't get that, can you please rephrase?
Don't forget to click Save.
- It's advised to repeat these steps for the Catalog, My Order, Order Process and Customer Care flows.
Here's a tip: when creating fallback messages, make them more explicit, by rephrasing the previous question or by mentioning an example. You could create these type of No-match and No-input events on Page level when creating parameters. In our labs, we have already done this.
Creating the Order Process Flow
- Go to the Order Process Flow, and create the following intent transitions:
Page (In Flow) | Routes > Intent | Routes > Transition To |
Order Process Start |
| End Session |
Order Process Start |
| End Flow |
Order Process Start |
| New Page: Shipping Details |
- Let's create the following entry fulfillment for the Shipping Details Page:
- Entry fulfillment:
To complete your order I will first need to collect your shipping details.
- Create the following parameters:
These parameters will make use of built-in system entities. System entity support differs for each language. See the docs for more information.
Parameter Display name | Entity | Required? | Initial prompt fulfillment | No-match default | No-input default |
| @sys.person | Required |
|
|
|
| @sys.person | Required |
|
|
|
| @sys.address | Required |
|
|
|
| @sys.any | Required |
|
|
|
| @sys.geo-city | Required |
|
|
|
| @sys.geo-country | Required |
|
|
|
| @sys.email | Required |
|
|
|
- Create the following conditional route:
- Customize Expression:
$page.params.status = "FINAL" - Transition to new Page:
Payment Details
- Create the following entry fulfillment.
Let's fake it that this virtual agent makes use of Google Pay. Don't worry this tutorial won't make real transactions. Create the following entry dialogues:
- Agent Says:
Alright $session.params.firstname! We will make use of Google Pay, that's connected to your email account: $session.params.email.
- Conditional Response
if $session.params.merch != "Digital Album"
Shipping costs an additional 5 dollars. This will make the total price $$sys.func.TO_TEXT($sys.func.ADD($session.params.price, 5)).
Your merchandise will be shipped to:
$session.params.firstname $session.params.lastname
$session.params.address
$session.params.zipcode $session.params.city
$session.params.country
To continue the order process please explicitly say "I confirm". Do you want to confirm your $session.params.artist $session.params.merch order?
else
The total costs will be: $$session.params.price.
After purchasing the digital album, you will receive an email with the download link.
To continue the order process please explicitly say "I confirm".
Do you want to confirm your $session.params.artist $session.params.merch order?
endif
- 建立下列意圖路徑
- 意圖:
confirm.proceed.order - 虛擬服務專員說:
Thank you for your order! Your merchandise will be shipped today! - 新增對話選項 > 文字:
Here's the order number: ABCD123。 - 新增對話選項 > 文字:
Have a good day! - 轉場效果:
End Session
- 選取「開始」頁面,然後按一下
redirect.end意圖。建立下列完成動作:Thank you for contacting G-Records! Have a nice day! - 選取「開始」頁面,然後按一下
redirect.home意圖。建立下列參數預設設定:restart = true
太棒了!到目前為止,我們已經建立一個可正常運作的零售商聊天機器人!在下一個實驗室中,我們將測試虛擬代理人的表現!
9. 測試虛擬服務專員
您可以使用內建模擬工具測試虛擬代理程式的對話。在模擬器中測試流程的優點是,您可以在逐步完成流程時,清楚瞭解模擬器收集的流程、頁面、參數和 (DTMF) 事件。與直接在整合中測試相比,這樣做會比較容易,因為這類資訊會對使用者隱藏。您甚至可以建立、儲存及重複使用測試案例。如果您會長期維護或編輯流程,並想確保變更不會破壞先前的工作,這項功能就非常實用。
您也可以將先前建立的測試案例儲存在 Google Cloud Storage 或本機,然後匯出及匯入這些案例。匯出測試時,系統會下載 blob 檔案。如要進一步瞭解模擬器和測試案例,請參閱模擬器 / 測試案例文件。
建立測試案例前,請先完成虛擬代理程式的其餘部分:
建立客戶服務流程
- 前往「Customer Care」流程,然後建立下列意圖轉換:
頁面 (流程中) | 路徑 > 意圖 | 路徑 > 轉換至 |
Customer Care Start |
| 運費 |
Customer Care Start |
| 退款 |
Customer Care Start |
| 更換 |
Customer Care Start |
| 結束流程 |
Customer Care Start |
| 結束工作階段 |

- 為「運送」頁面建立下列項目履行作業:
Shipping physical merchandise items can take up to 2 weeks.Is there anything else I can help you with?
- 為「退款」頁面建立下列項目完成情況:
We offer free returns and refunds. We provide one free return label for each order. You can use it within 30 days from receiving your order. If your refund is accepted, we will refund the price you paid for your item back to your original payment method.Is there anything else I can help you with?
- 為「Swapping」(交換) 頁面建立下列項目完成動作:
If you would like to change your item for a different one, please return your unwanted item and place a new order. If your refund is accepted, we will refund the price you paid for your item back to your original payment method.Is there anything else I can help you with?
- 選取「開始」頁面,然後按一下
redirect.end意圖。建立下列完成動作:Thank you for contacting G-Records! Have a nice day! - 選取「開始」頁面,然後按一下
redirect.home意圖。建立下列參數預設設定:restart = true
建立測試案例
- 按一下畫面右側的「測試代理程式」按鈕。
首次開啟模擬器時,您需要選取代理程式環境和有效流程。在大多數情況下,您應該使用草稿環境和預設啟動流程。
- 類型:
Hi

- 提問:
Which artists are signed with your label? - 說出:
The Google Dolls - 說出:
I am interested in buying a shirt - 說出:
A t-shirt - 說出:
Medium - 現在請按一下「儲存測試案例」按鈕。您可以在模擬器頂端找到這個圖示 (位於「重做」箭頭和「重設垃圾桶」圖示旁)。

- 提供下列詳細資料:
- 測試案例名稱:
Buy Google Dolls t-shirt size M - 標記:#catalog、#shirts、#t-shirt、#TheGoogleDolls
- 點選「儲存」。
我們來建立更多測試案例。
- 請先點按「重設」(垃圾桶) 圖示,清除目前的對話。
- 建立下列測試案例:
購買 Alice Googler T 恤:
>"Buy the Alice Googler t-shirt."
>"XL"
- 測試案例名稱:
Buy the Alice Googler t-shirt - 標記:
#catalog, #shirts, #t-shirt, #AliceGoogler
購買 M 號 T 恤 (請注意,系統並未提及藝人名稱,但您想略過樂團簡介、產品簡介、襯衫和襯衫尺寸頁面)
>"Buy a t-shirt size M"
>"The Google Fighters"
- 測試案例名稱:
Buy a t-shirt size M - 標記:
#catalog, #shirts, #t-shirt, #TheGoogleFighters - 說明:(請注意,這裡沒有提及藝人名稱,但您想略過樂團總覽、產品總覽、襯衫和襯衫尺寸頁面)
購買 Guns N' Roses 的音樂 (注意:系統會略過樂團總覽和產品總覽頁面)
>"Purchase music of G's N' Roses"
>"Live"
>"CD"
- 測試案例名稱:
Purchase music of G's N' Roses - 標記:
#catalog, #music, #CD, #GsNRoses, #live - 說明:(請注意,系統會略過樂團總覽和產品總覽頁面)
查看價格資訊:
>"Which products"
>"Shirts"
>"What's the price difference?"
>"Longsleeve"
>"What does it cost?"
>"M"
>"The Google Dolls"
>"No"
>"Which bands"
>"The Gooo Fighters"
>"Music"
>"How much does it cost?"
>"Greatest Hits"
>"What's the price difference?"
>"Mp3"
>"No"
>"I want to buy the tour movie"
>"Alice Googler"
>"Yes"
- 測試案例名稱:
Price info - 標記:
#catalog, #music, #tourmovie, #shirts - 說明:在對話中的各個點測試價格資訊
測試預先錄製的測試案例
- 在左側的 Dialogflow 主選單中,依序選取「管理」 >「測試案例」。
- 選取所有測試案例,然後按一下表格上方的「執行」按鈕。
Dialogflow CX 會針對儲存為「黃金測試案例」的錄音,執行所有選取的測試案例。如果結果與您儲存的結果相同,則測試通過。- 如果流程有任何變更,例如網頁設定不正確,或意圖將您導向錯誤的網頁,測試就會失敗。

- 在模擬工具中提出下列問題:
How long will shipping take? - 請記下結果,並將測試案例儲存為
Shipping,標記為#shipping。 - 前往「管理」>「測試案例」面板,然後按下格線右上方的「執行」按鈕,即可只執行
Shipping測試案例。
這項測試應該會通過。
- 返回「Customer Care Flow」,選取「Start」頁面,然後點按「Routes」標題。
畫面上會顯示格線,列出所有路線。
- 移除
redirect.shipping.info route - 前往「管理」>「測試案例」面板,然後按下格線右上方的「執行」按鈕,即可只執行
Shipping測試案例。
這項測試應該會失敗。
- 您可以點選失敗的測試,查看失敗的詳細資料。
在本例中,測試失敗並顯示下列錯誤訊息:
Page: Page mismatch:
Expected: Shipping
Actual: Start Page
這是因為該頁面已不在流程中。我們預期會看到「Shipping」頁面,但實際上我們從未離開「Start」頁面。(或您的終端使用者會收到備用訊息)。
換句話說,這是錯失的要求,也就是偽陰性測試結果。測試失敗。我們預期會看到「運送」頁面,但沒有任何反應,或是顯示備用訊息。
- 返回「Customer Care Flow」,然後將
redirect.shipping.info新增為意圖路徑,加到「Start」頁面。別忘了切換至「運送」頁面,然後按一下「儲存」。 - 在模擬器中記錄下列測試案例:
I want to swap my item,並將這個測試案例儲存為Swapping#swapping。 - 開啟「管理」>「意圖」>「redirect.refund.info」,然後新增以下訓練詞組:
I want to swap this item for a refund
如果沒有這個訓練詞組,當使用者要求更換商品以申請退款時,系統會觸發 redirect.swapping.info 意圖,但我們不想提供更換商品的資訊,而是想提供退款資訊。
- 在模擬器中建立下列黃金測試案例:
I want to swap this item for a refund,並將此測試案例儲存為Swap for Refund#refund - 返回「管理」>「意圖」>「redirect.refund.info」意圖,然後移除
I want to swap this item for a refund行。 - 返回「管理」>「測試案例」,選取「換貨退款」測試案例,然後「執行」。
最近一次測試失敗,錯誤訊息如下:
If you would like to change your item for a different one, please return your unwanted item and place a new order. If your refund is accepted, we will refund the price you paid for your item back to your original payment method.`
Is there anything else I can help you with?
Page: Page mismatch:
Expected: Refund
Actual: Swapping
換句話說,這是誤解要求,屬於誤判測試結果。測試失敗。我們預期會看到「退款」頁面,但系統卻啟用了「換貨」頁面。
涵蓋範圍
在 Dialogflow CX 中,測試涵蓋範圍是一種指標,用於說明特定測試套件執行時,虛擬服務專員對話 (頁面和意圖) 的執行程度。如果虛擬代理程式的測試涵蓋範圍百分比較高,表示在測試期間執行的對話較多,因此與測試涵蓋範圍較低的虛擬代理程式相比,含有未偵測到錯誤 (例如未正確解讀要求) 的機率較低。
- 如要查看所有測試案例的測試涵蓋範圍報告,請按一下「涵蓋範圍」。
- 按一下「轉場效果」分頁標籤。
這會顯示所有網頁轉換的測試涵蓋範圍。

- 按一下「意圖」分頁標籤。
這會顯示所有意圖的測試涵蓋率。

恭喜!您已建構並測試零售商機器人的完整實際範例。請前往下一頁的實驗室,閱讀結論並查看實用參考資料!
10. 結語
Dialogflow CX 是一種對話式 AI 平台 (CAIP),可用於建立聊天或語音機器人等虛擬服務專員。Dialogflow CX 提供視覺化機器人建構工具、可重複使用的意圖,以及處理多輪對話的功能,可協助團隊加速建立企業級對話體驗。
在本程式碼研究室中,您已瞭解如何建構實際的零售虛擬服務專員。我們探討了下列概念:
- 流量數
- 參數、自訂和系統實體
- 頁面
- 意圖路徑和條件路徑等狀態處理常式
- 靜態執行要求訊息和條件式回應
- 備用意圖
- 模擬器、測試案例和涵蓋範圍

參考資料
如要進一步瞭解 Dialogflow CX,請參閱下列網誌和說明文件!