對話結束 (Dialogflow)

在 Dialogflow 中探索

點選「繼續」,在 Dialogflow 中匯入 Conversation 離開範例。接著,請按照 部署及測試範例的步驟:

  1. 輸入虛擬服務專員名稱,並為範例建立新的 Dialogflow 代理程式。
  2. 代理程式匯入完畢後,按一下「Go to agent」
  3. 在主要導覽選單中,前往「Fulfillment」(執行要求)
  4. 啟用「Inline Editor」(內嵌編輯器),然後按一下 [Deploy] (部署)。編輯器包含範例 再也不是件繁重乏味的工作
  5. 在主要導覽選單中,前往「Integrations」(整合),然後按一下「Google」 Google 助理
  6. 在出現的互動視窗中,啟用「自動預覽變更」,然後按一下「測試」 以開啟動作模擬工具
  7. 在模擬工具中輸入 Talk to my test app 即可測試範例!
繼續

每當使用者想要離開對話中,可以說:

  • "exit"
  • 「取消」
  • "停止"
  • 「不用了」
  • "再見"

根據預設,Google 助理會結束對話並播放音效。 如要覆寫此行為,您可以選擇接收最後一項要求 即可清除執行要求邏輯,以及最後一次回應使用者。 最終回應必須是單一簡單回應 textToSpeechdisplayText 值最多只能有 60 個半形字元。

對話退出要求的執行時間上限為 2 秒。如果答案為「否」 對話結束後,對話就會以預設的退出耳機結束。 如果您的自訂取消回應失敗 (因為超過字元限制, 不當回應類型等) 對話也會以預設的退出音效結束對話。

Dialogflow

如何接收對話離開事件:

  1. 在左側導覽列中,按一下「Intents」(意圖) 選單項目旁的 + 圖示。
  2. 為意圖命名 (例如 Exit Conversation),然後按一下「Events」(事件) 部分。
  3. 在「新增事件」欄位中輸入 actions_intent_CANCEL
  4. 在「Response」下方指定回應,或設定您自己的回應: 在 Webhook 程式碼中執行這項意圖
  5. 啟用「將此意圖設為對話結尾」
  6. 按一下 [儲存]
  7. 點選左側導覽面板中的「Integrations」(整合)
  8. 選擇「Google 助理」,然後依序點選「更新草稿」和「測試」,即可建立 並確保專案已反映相關變更

當使用者要求對話結束時,系統會觸發您建立的意圖, 則會將回應傳回給使用者適用對象 例如,以下的執行要求程式碼使用用戶端程式庫處理 並傳回回應

Node.js

const {dialogflow} = require('actions-on-google');
const functions = require('firebase-functions');

const app = dialogflow({debug: true});

app.intent('Exit Conversation', (conv) => {
  conv.close(`Okay, talk to you next time!`);
});

exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);

Java

package com.example;

import com.google.actions.api.ActionRequest;
import com.google.actions.api.ActionResponse;
import com.google.actions.api.DialogflowApp;
import com.google.actions.api.ForIntent;
import com.google.actions.api.response.ResponseBuilder;

public class MyActionsApp extends DialogflowApp {

  @ForIntent("Exit Conversation")
  public ActionResponse exit(ActionRequest request) {
    ResponseBuilder responseBuilder = getResponseBuilder(request);
    responseBuilder.add("Okay, talk to you next time!");
    responseBuilder.endConversation();
    return responseBuilder.build();
  }
}

要求 JSON

請注意,以下 JSON 描述的是 Webhook 要求。

{
  "responseId": "e0c9d47f-aa99-4d03-bd48-a64c79e9c2a2-712767ed",
  "queryResult": {
    "queryText": "actions_intent_CANCEL",
    "parameters": {},
    "allRequiredParamsPresent": true,
    "fulfillmentText": "Webhook failed for intent: Exit Conversation",
    "fulfillmentMessages": [
      {
        "text": {
          "text": [
            "Webhook failed for intent: Exit Conversation"
          ]
        }
      }
    ],
    "outputContexts": [
      {
        "name": "projects/df-exits-kohler/agent/sessions/ABwppHGVnlRtMz1RhGGmDOSlCFAbc2QaM12xYtw0raWvv-R5GYM6tkqiVOpIOexm_LTBCvmARwpbZ_onRuX0/contexts/actions_capability_audio_output"
      },
      {
        "name": "projects/df-exits-kohler/agent/sessions/ABwppHGVnlRtMz1RhGGmDOSlCFAbc2QaM12xYtw0raWvv-R5GYM6tkqiVOpIOexm_LTBCvmARwpbZ_onRuX0/contexts/actions_capability_media_response_audio"
      },
      {
        "name": "projects/df-exits-kohler/agent/sessions/ABwppHGVnlRtMz1RhGGmDOSlCFAbc2QaM12xYtw0raWvv-R5GYM6tkqiVOpIOexm_LTBCvmARwpbZ_onRuX0/contexts/actions_capability_account_linking"
      },
      {
        "name": "projects/df-exits-kohler/agent/sessions/ABwppHGVnlRtMz1RhGGmDOSlCFAbc2QaM12xYtw0raWvv-R5GYM6tkqiVOpIOexm_LTBCvmARwpbZ_onRuX0/contexts/actions_capability_screen_output"
      },
      {
        "name": "projects/df-exits-kohler/agent/sessions/ABwppHGVnlRtMz1RhGGmDOSlCFAbc2QaM12xYtw0raWvv-R5GYM6tkqiVOpIOexm_LTBCvmARwpbZ_onRuX0/contexts/actions_capability_web_browser"
      },
      {
        "name": "projects/df-exits-kohler/agent/sessions/ABwppHGVnlRtMz1RhGGmDOSlCFAbc2QaM12xYtw0raWvv-R5GYM6tkqiVOpIOexm_LTBCvmARwpbZ_onRuX0/contexts/google_assistant_input_type_keyboard"
      },
      {
        "name": "projects/df-exits-kohler/agent/sessions/ABwppHGVnlRtMz1RhGGmDOSlCFAbc2QaM12xYtw0raWvv-R5GYM6tkqiVOpIOexm_LTBCvmARwpbZ_onRuX0/contexts/actions_intent_cancel"
      }
    ],
    "intent": {
      "name": "projects/df-exits-kohler/agent/intents/b506991f-ad7a-4c4b-8adc-5e6c62727381",
      "displayName": "Exit Conversation"
    },
    "intentDetectionConfidence": 1,
    "languageCode": "en"
  },
  "originalDetectIntentRequest": {
    "source": "google",
    "version": "2",
    "payload": {
      "user": {
        "locale": "en-US",
        "userVerificationStatus": "VERIFIED"
      },
      "conversation": {
        "conversationId": "ABwppHGVnlRtMz1RhGGmDOSlCFAbc2QaM12xYtw0raWvv-R5GYM6tkqiVOpIOexm_LTBCvmARwpbZ_onRuX0",
        "type": "ACTIVE",
        "conversationToken": "[]"
      },
      "inputs": [
        {
          "intent": "actions.intent.CANCEL",
          "rawInputs": [
            {
              "inputType": "KEYBOARD",
              "query": "cancel"
            }
          ]
        }
      ],
      "surface": {
        "capabilities": [
          {
            "name": "actions.capability.AUDIO_OUTPUT"
          },
          {
            "name": "actions.capability.MEDIA_RESPONSE_AUDIO"
          },
          {
            "name": "actions.capability.ACCOUNT_LINKING"
          },
          {
            "name": "actions.capability.SCREEN_OUTPUT"
          },
          {
            "name": "actions.capability.WEB_BROWSER"
          }
        ]
      },
      "availableSurfaces": [
        {
          "capabilities": [
            {
              "name": "actions.capability.SCREEN_OUTPUT"
            },
            {
              "name": "actions.capability.WEB_BROWSER"
            },
            {
              "name": "actions.capability.AUDIO_OUTPUT"
            }
          ]
        }
      ]
    }
  },
  "session": "projects/df-exits-kohler/agent/sessions/ABwppHGVnlRtMz1RhGGmDOSlCFAbc2QaM12xYtw0raWvv-R5GYM6tkqiVOpIOexm_LTBCvmARwpbZ_onRuX0"
}

回應 JSON

請注意,下列 JSON 會說明 Webhook 回應。

{
  "payload": {
    "google": {
      "expectUserResponse": false,
      "richResponse": {
        "items": [
          {
            "simpleResponse": {
              "textToSpeech": "Okay, talk to you next time!"
            }
          }
        ]
      }
    }
  }
}

Actions SDK

如要接收對話結束意圖,請按照下列步驟操作:

  1. 在動作套件中的 conversations 物件中宣告 且您要接收 actions.intent.CANCEL 意圖的 每當使用者想要在對話中結束時就結束轉換。

    
    {
      "actions": [
        {
          "description": "Default Welcome Intent",
          "name": "MAIN",
          "fulfillment": {
            "conversationName": "conversation_1"
          },
          "intent": {
            "name": "actions.intent.MAIN"
          }
        }
      ],
      "conversations": {
        "conversation_1": {
          "name": "conversation_1",
          "url": "YOUR_ENDPOINT_URL",
          "inDialogIntents": [
            {
              "name": "actions.intent.CANCEL"
            }
          ]
        }
      }
    }
    
  2. 收到含有 actions.intent.CANCEL 的要求時 清理任何想使用的執行要求邏輯,然後傳回 提供適當的退出詞組例如以下的執行要求代碼 ,使用用戶端程式庫處理取消意圖。

    Node.js

    const {actionssdk} = require('actions-on-google');
    const functions = require('firebase-functions');
    
    const app = actionssdk({debug: true});
    
    app.intent('actions.intent.MAIN', (conv) => {
      conv.ask(`Hi! Try saying 'exit' or 'cancel'`);
    });
    
    app.intent('actions.intent.TEXT', (conv, input) => {
      conv.ask(`You said ${input}`);
      conv.ask(`Try saying 'exit' or 'cancel'`);
    });
    
    app.intent('actions.intent.CANCEL', (conv) => {
      conv.close(`Okay, talk to you next time!`);
    });
    
    exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);

    Java

    package com.example;
    
    import com.google.actions.api.ActionRequest;
    import com.google.actions.api.ActionResponse;
    import com.google.actions.api.ActionsSdkApp;
    import com.google.actions.api.ConstantsKt;
    import com.google.actions.api.ForIntent;
    import com.google.actions.api.response.ResponseBuilder;
    import com.google.actions.api.response.helperintent.Confirmation;
    import com.google.actions.api.response.helperintent.DateTimePrompt;
    import com.google.actions.api.response.helperintent.Permission;
    import com.google.actions.api.response.helperintent.Place;
    import com.google.api.services.actions_fulfillment.v2.model.DateTime;
    import com.google.api.services.actions_fulfillment.v2.model.Location;
    
    public class MyActionsApp extends ActionsSdkApp {
    
      @ForIntent("actions.intent.MAIN")
      public ActionResponse welcome(ActionRequest request) {
        ResponseBuilder responseBuilder = getResponseBuilder(request);
        responseBuilder.add("Hi! Try saying 'exit' or 'cancel'");
        return responseBuilder.build();
      }
    
      @ForIntent("actions.intent.TEXT")
      public ActionResponse fallback(ActionRequest request) {
        ResponseBuilder responseBuilder = getResponseBuilder(request);
        responseBuilder.add("You said " + request.getRawInput().getQuery());
        responseBuilder.add("Try saying 'exit' or 'cancel'");
        return responseBuilder.build();
      }
    
      @ForIntent("actions.intent.CANCEL")
      public ActionResponse exit(ActionRequest request) {
        ResponseBuilder responseBuilder = getResponseBuilder(request);
        responseBuilder.add("Okay, talk to you next time!");
        responseBuilder.endConversation();
        return responseBuilder.build();
      }
    
    }

    要求 JSON

    請注意,以下 JSON 描述的是 Webhook 要求。

    {
      "user": {
        "locale": "en-US",
        "userVerificationStatus": "VERIFIED"
      },
      "conversation": {
        "conversationId": "ABwppHEtH6XptryyLGWDBOVnLgy8EkOclTPETd4lcLh_f6ghQAzzg0seUuq3YocyFT8B17N6AGD6YPPjQw5CXBM",
        "type": "NEW"
      },
      "inputs": [
        {
          "intent": "actions.intent.CANCEL",
          "rawInputs": [
            {
              "inputType": "KEYBOARD",
              "query": "exit"
            }
          ]
        }
      ],
      "surface": {
        "capabilities": [
          {
            "name": "actions.capability.SCREEN_OUTPUT"
          },
          {
            "name": "actions.capability.ACCOUNT_LINKING"
          },
          {
            "name": "actions.capability.MEDIA_RESPONSE_AUDIO"
          },
          {
            "name": "actions.capability.AUDIO_OUTPUT"
          },
          {
            "name": "actions.capability.WEB_BROWSER"
          }
        ]
      },
      "availableSurfaces": [
        {
          "capabilities": [
            {
              "name": "actions.capability.AUDIO_OUTPUT"
            },
            {
              "name": "actions.capability.SCREEN_OUTPUT"
            },
            {
              "name": "actions.capability.WEB_BROWSER"
            }
          ]
        }
      ]
    }

    回應 JSON

    請注意,下列 JSON 會說明 Webhook 回應。

    {
      "expectUserResponse": false,
      "finalResponse": {
        "richResponse": {
          "items": [
            {
              "simpleResponse": {
                "textToSpeech": "Okay, talk to you next time!"
              }
            }
          ]
        }
      }
    }