在 Dialogflow 中探索
点击继续,将我们的对话退出示例导入 Dialogflow 中。然后,按照 部署和测试示例的步骤如下:
- 输入代理名称并为示例创建新的 Dialogflow 代理。
- 代理导入完成后,点击转到代理 (Go to agent)。
- 在主导航菜单中,前往 Fulfillment。
- 启用内嵌编辑器,然后点击部署。编辑器包含示例 代码。
- 在主导航菜单中,前往 Integrations(集成),然后点击 Google Google 助理。
- 在显示的模态窗口中,启用 Auto-preview changes(自动预览更改),然后点击 Test(测试) 打开 Actions 模拟器。
- 在模拟器中,输入
Talk to my test app
以测试该示例!
每当用户想在对话过程中退出时,都可以这样说:
- "exit"
- “取消”
- “停止”
- “不用了”
- 再见
默认情况下,Google 助理会退出你的对话并播放耳标。
要改变这种行为,您可以选择接收
可用于清理执行方式逻辑并最后一次响应用户。
您的最终响应必须是一条简单响应
其 textToSpeech
和 displayText
值的长度上限为 60 个字符。
对话退出请求允许的最长执行时间为 2 秒;如果否 收到回应时,系统会以默认退出耳标结束对话。 如果您的自定义取消响应失败(由于超出字符数限制, 响应类型不正确等),对话也会以默认退出耳标结束。
Dialogflow
若要接收对话退出事件,请执行以下操作:
- 在左侧导航栏中,点击意图菜单项旁边的 + 图标。
- 为 intent 命名(如
Exit Conversation
),然后点击 Events 部分。 - 在添加事件字段中输入
actions_intent_CANCEL
。 - 您可以在响应 (Response) 下指定响应,也可以自行设置针对 将此 intent 添加到网络钩子代码中。
- 启用将此 intent 设为对话结束。
- 点击保存。
- 在左侧导航栏中,点击集成。
- 选择 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
如需接收对话退出 intent,请执行以下操作:
-
在操作包内的
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" } ] } } }
-
收到
actions.intent.CANCEL
的请求后 intent,清理您想要的所有 fulfillment 逻辑,并返回一个 提供适当的退出短语例如,以下是一些执行方式代码 使用客户端库处理取消 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!" } } ] } } }