对话退出 (Dialogflow)

在 Dialogflow 中探索

点击继续,在 Dialogflow 中导入我们的对话退出事件示例。然后,按照以下步骤部署和测试该示例:

  1. 输入代理名称并为示例创建新的 Dialogflow 代理。
  2. 代理导入完成后,点击转至代理 (Go to agent)。
  3. 在主导航菜单中,前往 Fulfillment
  4. 启用内嵌编辑器,然后点击部署。编辑器包含示例代码。
  5. 在主导航菜单中,点击 Integrations(集成),然后点击 Google Assistant
  6. 在显示的模态窗口中,启用 Auto-preview changes,然后点击 Test 打开 Actions 模拟器。
  7. 在模拟器中,输入 Talk to my test app 以测试示例!
继续

每当用户想要在对话中退出时,可以说出如下提示:

  • "exit"
  • “取消”
  • "stop"
  • “无”
  • “再见”

默认情况下,Google 助理会退出对话并播放耳标。 如需替换此行为,您可以选择接收最后一个请求,以便用于清理执行方式逻辑并最后一次响应用户。您的最终响应必须是单个简单响应,其 textToSpeechdisplayText 值不得超过 60 个字符。

对话退出请求允许的最长执行时间为 2 秒;如果未接收到响应,对话将以默认退出耳标结束。 如果您的自定义取消响应失败(由于超出字符数限制、响应类型不当等),对话也会以默认的退出耳标结束。

Dialogflow

如需接收对话退出事件,请执行以下操作:

  1. 在左侧导航栏中,点击意图菜单项旁边的 + 图标。
  2. 为 intent 命名(例如 Exit Conversation),然后点击 Events 部分。
  3. 添加事件字段中输入 actions_intent_CANCEL
  4. Response 下指定响应,或者在您的 webhook 代码中为此意图设置自己的响应。
  5. 启用将此 intent 设置为对话结束
  6. 点击保存
  7. 在左侧导航栏中,点击集成
  8. 选择 Google 助理,然后依次点击更新草稿测试,确保更改已反映在您的项目中。

当用户请求退出对话时,系统会触发您创建的 intent,并将您的响应返回给用户。例如,以下是使用客户端库处理取消 intent 并返回响应的一些执行方式代码。

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

如需接收对话退出 intent,请执行以下操作:

  1. 在 Action 包内的 conversations 对象中,声明每当用户想要在对话中退出时接收 actions.intent.CANCEL intent。

    
    {
      "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 intent 的请求时,请清理所需的所有执行方式逻辑,并将相应的退出短语返回给用户。例如,下面是一些使用客户端库处理取消 intent 的执行方式代码。

    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!"
              }
            }
          ]
        }
      }
    }