การออกจากการสนทนา (Dialogflow)

สำรวจใน Dialogflow

คลิกต่อไปเพื่อนำเข้าตัวอย่างการออกจากการสนทนาใน Dialogflow จากนั้นทำตาม ด้านล่างเพื่อปรับใช้และทดสอบตัวอย่าง

  1. ป้อนชื่อ Agent และสร้าง Agent ใหม่ของ Dialogflow สำหรับตัวอย่าง
  2. หลังจากนำเข้าตัวแทนเสร็จแล้ว ให้คลิกไปที่ตัวแทน
  3. จากเมนูการนำทางหลัก ให้ไปที่การดำเนินการตามคำสั่งซื้อ
  4. เปิดใช้เครื่องมือแก้ไขในบรรทัด แล้วคลิกทำให้ใช้งานได้ เครื่องมือแก้ไขมีตัวอย่าง โค้ด
  5. จากเมนูการนำทางหลัก ให้ไปที่ Integrations แล้วคลิก Google Assistant
  6. ในหน้าต่างโมดัลที่ปรากฏขึ้น ให้เปิดใช้แสดงตัวอย่างการเปลี่ยนแปลงอัตโนมัติ แล้วคลิกทดสอบ เพื่อเปิดเครื่องมือจำลอง Actions
  7. ในเครื่องมือจำลอง ให้ป้อน Talk to my test app เพื่อทดสอบตัวอย่าง

เมื่อต้องออกจากการสนทนาระหว่างกลาง ผู้ใช้ก็สามารถพูดประโยคต่อไปนี้

  • "ออก"
  • "ยกเลิก"
  • "หยุด"
  • "ไม่เป็นไร"
  • "ลาก่อน"

โดยค่าเริ่มต้น Assistant จะออกจากการสนทนาของคุณและเปิด Earcon ในการลบล้างลักษณะการทำงานนี้ คุณสามารถเลือกรับคำขอสุดท้ายที่คุณ สามารถใช้เพื่อล้างตรรกะการดำเนินการตามคำสั่งซื้อและตอบกลับผู้ใช้เป็นครั้งสุดท้ายได้ คำตอบสุดท้ายต้องเป็นคำตอบง่ายๆ ครั้งเดียว ซึ่งมีจำนวนอักขระสูงสุด 60 ตัวสำหรับค่า textToSpeech และ displayText

เวลาดำเนินการสูงสุดที่อนุญาตสำหรับคำขอออกจากการสนทนาคือ 2 วินาที หากไม่ใช่ ได้รับคำตอบ การสนทนาจะจบลงด้วย earcon ที่เป็นค่าเริ่มต้น หากการตอบกลับการยกเลิกที่กำหนดเองไม่สำเร็จ (เนื่องจากเกินจำนวนอักขระสูงสุด ประเภทคำตอบที่ไม่เหมาะสม ฯลฯ) การสนทนาก็จะจบลงด้วย Exit Earcon ที่เป็นค่าเริ่มต้นเช่นกัน

Dialogflow

หากต้องการรับเหตุการณ์ออกจากการสนทนา ให้ทำดังนี้

  1. ในการนำทางด้านซ้าย ให้คลิกไอคอน + ข้างรายการในเมนู Intent
  2. ตั้งชื่อ Intent (เช่น Exit Conversation) แล้วคลิกส่วนเหตุการณ์
  3. ป้อน actions_intent_CANCEL ในช่องเพิ่มเหตุการณ์
  4. ระบุการตอบกลับในส่วนการตอบกลับ หรือตั้งค่าการตอบกลับของคุณเองสำหรับ ในโค้ดเว็บฮุคของคุณ
  5. เปิดใช้ตั้งค่า Intent นี้เป็นสิ้นสุดการสนทนา
  6. คลิกบันทึก
  7. คลิกการผสานที่การนำทางด้านซ้าย
  8. เลือก Google Assistant แล้วคลิกอัปเดตฉบับร่าง จากนั้นคลิกทดสอบเพื่อดำเนินการ ให้ดูว่าการเปลี่ยนแปลงแสดงในโปรเจ็กต์แล้ว

เมื่อผู้ใช้ขอให้ออกจากการสนทนา ระบบจะเรียกให้แสดงความตั้งใจที่คุณสร้างขึ้นและ ส่งการตอบสนองให้กับผู้ใช้ สำหรับ นี่คือตัวอย่างโค้ดการดำเนินการตามคำสั่งซื้อที่ใช้ไลบรารีของไคลเอ็นต์เพื่อจัดการ ยกเลิก 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 ด้านล่างอธิบายถึงคำขอเว็บฮุค

{
  "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 ด้านล่างจะอธิบายการตอบสนองของเว็บฮุค

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

Actions SDK

หากต้องการรับ Intent ของการออกจากการสนทนา ให้ทำดังนี้

  1. ในออบเจ็กต์ conversations ภายในแพ็กเกจการดำเนินการ ให้ประกาศ ที่คุณต้องการได้รับ Intent actions.intent.CANCEL เมื่อใดก็ตามที่ผู้ใช้ต้องการออกจากการสนทนา

    {
      a"ctions:" [
        {
          d"escription:" D"efault Welcome Intent,"
          n"ame:" M"AIN,"
          f"ulfillment:" {
            c"onversationName:" c"onversation_1
    "      },
          i"ntent:" {
            n"ame:" a"ctions.intent.MAIN
    "      }
        }
      ],
      c"onversations:" {
        c"onversation_1:" {
          n"ame:" c"onversation_1,"
          u"rl:" Y"OUR_ENDPOINT_URL,"
          i"nDialogIntents:" [
            {
              n"ame:" a"ctions.intent.CANCEL
    "        }
          ]
        }
      }
    }a
  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 ด้านล่างอธิบายถึงคำขอเว็บฮุค

    {
      "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 ด้านล่างจะอธิบายการตอบสนองของเว็บฮุค

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