傳送及接收活動,讓對話內容更多元

在 Business Messages 對話中,事件會透過與非訊息內容通訊,提供使用者和多元對話的對話體驗。針對使用者,事件會在對話中顯示為通知,並根據使用者採取的各種動作觸發。代理程式會在其 Webhook 中接收事件,並透過 API 呼叫來傳送事件。

代理程式應瞭解使用者啟動的事件,並因此能夠做出回應。舉例來說,如果使用者要求線上代理程式,但代理程式無法對要求做出正面或負面回應,這會導致使用者體驗不佳。

事件類型

每個事件都屬於特定類型:

  • 「已要求的代理程式要求」事件表示使用者想直接與線上服務專員對話。

    如果代理程式可以將對話轉換為真人代表,請傳送「代營」事件,然後傳送來自人類代表的後續訊息。

    如果代理程式無法將對話轉換為真人代表,則請傳送訊息給使用者,通知他們即時服務專員將有空。

  • 「代表性的加入/左側」事件會通知即時服務專員加入或離開對話的時間。這些事件會在對話中顯示通知,並讓使用者對回應的反應和所能詢問的問題類型有所期待。

    代表已加入/向左代表

  • 輸入事件表示使用者或代理程式正在輸入。

    對於使用者而言,isTyping 布林值表示他們的輸入狀態。每次狀態變更都會觸發新事件。

    代理程式可傳送 TYPING_STARTEDTYPING_STOPPED 事件,以顯示對話中的輸入指標。輸入代理程式的事件會告知使用者代表,他們代表正在撰寫回應,或者後端自動化正在處理他們的問題或要求。

    文字輸入指標

傳送活動

如要傳送事件,請執行下列指令。替換下列項目:

  • CONVERSATION_ID 包含您要傳送問卷調查的對話 ID
  • EVENT_ID,包含事件專屬 ID
  • PATH_TO_SERVICE_ACCOUNT_KEY 替換為您機器上的服務帳戶金鑰路徑
  • EVENT_TYPEEventType 的值
  • REPRESENTATIVE_NAME 以及建立事件的即時代理程式或自動化名稱
  • REPRESENTATIVE_TYPERepresentativeType 的值
curl -X POST "https://businessmessages.googleapis.com/v1/conversations/CONVERSATION_ID/events?eventId=EVENT_ID" \
-H "Content-Type: application/json" \
-H "User-Agent: curl/business-messages" \
-H "`oauth2l header --json PATH_TO_SERVICE_ACCOUNT_KEY businessmessages`" \
-d "{
  'eventType': 'EVENT_TYPE',
  'representative': {
    'avatarImage': 'REPRESENTATIVE_AVATAR_URL',
    'displayName': 'REPRESENTATIVE_NAME',
    'representativeType': 'REPRESENTATIVE_TYPE',
  },
}"

如需格式設定和值選項,請參閱 conversations.events

範例:傳送具代表性的活動

# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#     https://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This code sends a REPRESENTATIVE_JOINED event to the user.
# Read more: https://developers.google.com/business-communications/business-messages/guides/how-to/message/events#send

# Replace the __CONVERSATION_ID__ with a conversation id that you can send messages to
# Make sure a service account key file exists at ./service_account_key.json

curl -X POST "https://businessmessages.googleapis.com/v1/conversations/__CONVERSATION_ID__/events?eventId=6a0af2c6-787d-4097-870d-93fe20351747" \
-H "Content-Type: application/json" \
-H "User-Agent: curl/business-messages" \
-H "$(oauth2l header --json ./service_account_key.json businessmessages)" \
-d "{
  'eventType': 'REPRESENTATIVE_JOINED',
  'representative': {
    'avatarImage': 'https://developers.google.com/identity/images/g-logo.png',
    'displayName': 'Chatbot',
    'representativeType': 'HUMAN'
  }
}"

接收事件

使用者在裝置上觸發事件時,代理程式會在其 Webhook 中接收事件。接收及處理事件的方式與接收訊息相同。

使用者啟動的事件格式如下。

{
  "agent": "brands/BRAND_ID/agents/AGENT_ID",
  "requestId": "REQUEST_ID",
  "conversationId": "CONVERSATION_ID",
  "customAgentId": "CUSTOM_AGENT_ID",
  "sendTime": "SEND_TIME",
  "userStatus": {
    "isTyping": "BOOLEAN",
    "requestedLiveAgent": "BOOLEAN",
    "createTime": "CREATION_TIME",
  }
}

如需格式設定和值選項,請參閱 UserMessage