無頭網頁 SDK 事件

Headless Web SDK 提供可監聽的事件,以便處理應用程式中的特定動作或更新。

const handleReady = () => {
  console.log("**** client is ready")
}

client.on("ready", handleReady)

// client.off("ready", handleReady)

一般事件

以下是 SDK 的一般事件

ready

用戶端準備好通訊時發出。

client.on("ready", () => {
})

authenticated

用戶端使用使用者權杖完成驗證時,系統會發出這個事件。

client.on("authenticated", () => {
})

所有可用事件

SDK 提供多種事件,您可以監聽這些事件,在應用程式中處理特定動作或更新。

您可以使用 .on 和 .off 方法,新增及移除事件監聽器。

const handleReady = () => {
  console.log(" client is ready")
}
client.on("ready", handleReady)
// client.off("ready", handleReady)

以下是所有可用事件的完整清單:

全球活動

事件名稱 說明
ready 在用戶端準備好通訊時發出

已淘汰的事件

為確保回溯相容性,SDK 會繼續發出下列事件。請勿在新的整合項目中使用。

事件名稱 說明 指引
authenticated 用戶端使用使用者權杖完成驗證時,系統會發出這個事件 已淘汰,並已排定移除。現有整合項目在移除前,仍可繼續接收這項資訊。
chat.connected 在即時通訊與供應商連線時發出。 已淘汰,使用 chat.connectedMessages,這也會提供初始訊息酬載。

Chat 事件

事件名稱 說明
chat.ongoing 在對話進行時發出。
chat.updated 在聊天執行個體更新時發出。
chat.message 有新訊息時發出。
chat.memberJoined 新成員加入對話時發出。
chat.memberLeft 成員離開對話時觸發。
chat.typingStarted 成員開始輸入時發出。
chat.typingEnded 在成員停止輸入時發出。
chat.connectedMessages SDK 連上與供應商的對話時,系統會發出這個事件。
chat.disconnected 在即時通訊與供應商中斷連線時發出。
chat.dismissed 在即時通訊狀態變更為 dismissed 時發出。
chat.timeout 因「逾時」而結束即時通訊時,系統會發出這個事件。
chat.ended 在即時通訊結束時發出。
chat.destroyed 呼叫 destroyChat 時發出。
chat.checkInRequested 在可以簽到聊天室時發出。
chat.checkInTimedOut 在聊天室簽到逾時時發出

共同瀏覽事件

事件名稱 說明
cobrowse.request 使用者或服務專員要求共同瀏覽時發出
cobrowse.loaded 在共同瀏覽工作階段載入時發出
cobrowse.updated 在共同瀏覽工作階段更新時發出
cobrowse.ended 在共同瀏覽工作階段結束時發出

使用範例

client.on("ready", () => {
  // Client is ready
})
client.on("chat.message", message => {
  console.log(message)
})
client.on("chat.connectedMessages", async ({ chat, messages }) => {
  // Handle chat and messages
})
client.on("cobrowse.request", ({ from }) => {
  console.log("Cobrowse requested by", from)
})

自訂資料設定

custom_data 選項可讓您將任意鍵/值組合資料傳遞至網頁 SDK。

用量

建立即時通訊時,您可以提供物件 (未簽署) 或字串 (已簽署) 形式的自訂資料:

interface ChatRequest {
  lang?: string;
  trigger_id?: string;
  ticket_id?: string;
  email?: string;
  greeting?: string;
  cobrowsable?: boolean;
  custom_data?: {
    signed?: string;
    unsigned?: Record<string, any>;
  };
}

範例

請參閱以下範例:

const custom_data = {
  unsigned: {
    version: {
      label: 'Version',
      value: '1.0.0'
    }
  },
  signed: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...' // JWT or other signed payload
}

try {
  const chat: Chat = await client.createChat(123, { custom_data })
} catch (error) {
  // handle error
}

外部聊天機器人轉移

在某些情況下,您可能想保留其他即時通訊服務供應商的記錄和互動。方法是使用 custom_data object。

請參閱以下範例:

cconst custom_data = {
  unsigned: {
    external_chat_transfer: {
      greeting_override: "Please hold while we connect you with a human agent.",
      agent: {
        name: "Agent Name",
        avatar: "https://ujet.s3.amazonaws.com/default-virtual-agent-avatar-1.png"
      },
      transcript: [
        {
          sender: "agent",
          timestamp: "2021-03-15 12:00:00Z",
          content: [
            {
              type: "text",
              text: "Hello! How can I help you today?"
            },
            {
              type: "buttons",
              buttons: [
                {
                  label: "Create New Order",
                  selected: false
                },
                {
                  label: "Check Order Status",
                  selected: true
                },
                {
                  label: "Check Account Balance",
                  selected: false
                },
              ]
            }
          ]
        },
        {
          sender: "end_user",
          timestamp: "2021-03-15 12:00:15Z",
          content: [
            {
              type: "text",
              text: "Check Order Status"
            }
          ]
        },
        {
          sender: "agent",
          timestamp: "2021-03-15 12:00:16Z",
          content: [
            {
              type: "text",
              text: "I can help you with that, what's your order number?"
            }
          ]
        },
        {
          sender: "end_user",
          timestamp: "2021-03-15 12:00:20Z",
          content: [
            {
              type: "media",
              media: {
                type: "image",
                url: "https://ujet.s3.amazonaws.com/default-virtual-agent-avatar-1.png"
              }
            }
          ]
        }
      ]
    }
  },
  signed: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...' // JWT or other signed payload
}

try {
  const chat: Chat = await client.createChat(123, { custom_data })
} catch (error) {
  // handle error
}