ServiceChat API 文件

這份公開文件說明網站 Widget 與 Bot API 的基本整合方式。商戶登入後台後,可在 API 專區取得帶有自己 Key 的整合範例與測試工具。

整合方式

網站 Widget

貼上一段 script,在網站啟用客服聊天視窗。

Bot API

商戶後端可建立聊天室、寫入訊息與讀取訊息。

社群通路

Telegram、LINE、WhatsApp、Messenger 等通路由後台設定;查看申請教學

網站 Widget

公開範例使用 YOUR_WIDGET_PUBLIC_KEY 佔位。登入後台後,請到 API 專區複製包含實際前端整合 Key 的版本。

<script src="https://your-service-chat-domain/widget/servicechat-widget.js"
        data-tenant-key="YOUR_WIDGET_PUBLIC_KEY"
        data-customer-name="Guest"
        data-inbox-category-code="sales"
        data-launcher-label="Service"
        data-launcher-icon="https://example.com/chat-icon.svg"
        data-launcher-position="left-bottom"
        async></script>

前端整合 Key 是公開識別碼,不是 Bot API Key。租戶管理員可在後台 API 專區重建,重建後舊 key 會立即失效,網站上的嵌入碼也要同步更新。data-inbox-category-code 可把新聊天室放入指定收件匣分類;分類不需預先建立,聊天室建立後分類固定。data-launcher-label 可調整按鈕文字;data-launcher-icon 可填圖片 URL,或填 none 隱藏 icon;data-launcher-position 可用 right-bottomleft-topright-topleft-bottom,也可填座標,例如 100,100 從左上計算,100,-100 表示左 100、下 100。

前端控制 API

window.ServiceChatWidget.open();
window.ServiceChatWidget.close();
window.ServiceChatWidget.toggle();
window.ServiceChatWidget.getUnreadCount();
window.ServiceChatWidget.markRead();

Bot API 認證

Bot API 使用商戶後台建立的 API Key。API Key 只能放在 server 端,不應放在瀏覽器前端。

X-ServiceChat-ApiKey: sc_live_xxx

建立或取得聊天室

POST /api/service-chat/customer/room
Content-Type: application/json
X-ServiceChat-ApiKey: sc_live_xxx

{
  "customerAccount": "user-10001",
  "customerDisplayName": "Customer 10001",
  "customerLanguage": "zh-TW",
  "inboxCategoryCode": "sales"
}

代表客戶送出文字訊息

POST /api/service-chat/customer/messages/text
Content-Type: application/json
X-ServiceChat-ApiKey: sc_live_xxx

{
  "roomId": "User-user-10001",
  "text": "我想了解方案內容",
  "clientMessageId": "browser-generated-guid"
}

clientMessageId 為選填,建議由前端在送出前產生 GUID。系統回送訊息時會帶回同一個值,方便自建前端先顯示本機訊息,再用正式訊息替換,避免 HTTP 回應與即時推送先後抵達造成重複顯示。

讀取聊天室訊息

GET /api/service-chat/customer/messages?page=1
X-ServiceChat-ApiKey: sc_live_xxx

媒體上傳

官方 Widget 會自動處理分段上傳與續傳。自建整合可使用 upload session 流程:初始化、上傳 chunk、完成上傳。

方案限制

後台 API 專區

登入後台後可管理 Bot API Key、複製帶有前端整合 Key 的 Widget 範例、使用 API 測試工具。

前往後台