ThunderPhone 2.0 正式上線。自助開通,每分鐘 2¢ 起。查看公告

Widget

CDN/Script Tag

無需打包工具,即可將 ThunderPhone 語音小工具加入任何網站

CDN 版本會在內部打包 React,因此你可以在靜態網站、WordPress、Webflow,或任何可加入 HTML 的頁面上使用小工具。不需要 npm、打包工具或框架。

CDN 網址

Latest (recommended)
<link rel="stylesheet" href="https://cdn.thunderphone.com/widget/latest/style.css" />
<script src="https://cdn.thunderphone.com/widget/latest/widget.js"></script>
Pinned version
<!-- Each stable release also publishes an immutable /widget/vX.Y.Z/ path
     matching the @thunderphone/widget npm version (v1.1.2 at the time of
     writing). If you pin, you own updating the URL for new releases. -->
<link rel="stylesheet" href="https://cdn.thunderphone.com/widget/v1.1.2/style.css" />
<script src="https://cdn.thunderphone.com/widget/v1.1.2/widget.js"></script>

基本用法

<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="https://cdn.thunderphone.com/widget/latest/style.css" />
</head>
<body>
 
  <div id="thunderphone"></div>
 
  <script src="https://cdn.thunderphone.com/widget/latest/widget.js"></script>
  <script>
    ThunderPhone.mount({
      element: '#thunderphone',
      publishableKey: 'pk_live_your_publishable_key',
    })
  </script>
 
</body>
</html>

小工具會掛載至目標元素,並顯示為視窗角落的固定定位列(預設為 bottom-right,由 position 選項控制)。掛載元素僅是 React 根節點——它在頁面中的位置不會影響小工具顯示的位置。


掛載選項

ThunderPhone.mount() 接受與 React 元件相同的選項,另加上 element 屬性:

選項類型必填預設值說明
elementstring | HTMLElement是--CSS 選擇器(例如 '#thunderphone')或 DOM 元素參照。
publishableKeystring是--可發布的 API 金鑰(pk_live_...)。系統會根據金鑰的小工具設定自動解析智慧體。
theme'light' | 'dark'否'light'色彩配置。
primaryColorstring否'#000000'(淺色)/ '#ffffff'(深色)作為強調色使用的 CSS 色彩字串。
titlestring否'Voice assistant'顯示於小工具列中的文字。
position'bottom-right' | 'bottom-left' | 'top-right' | 'top-left'否'bottom-right'固定於檢視區的位置。
apiBasestring否'https://api.thunderphone.com/v1'覆寫 API 基礎 URL。
languagestring否--每個工作階段的語言覆寫設定——可使用語言代碼或地區設定,例如 en、es 或 fr-FR。未設定時,會套用智慧體設定的語言。需要在金鑰的瀏覽器覆寫設定下啟用 language。
voicestring否--每個工作階段的語音覆寫設定——可使用語音名稱,例如 maria。未設定時,會套用智慧體設定的語音。需要在金鑰的瀏覽器覆寫設定下啟用 voice。
contextstring否--傳遞給智慧體的每個工作階段頁面或網站事實背景資訊。伺服器端會截斷為 12,000 個字元。需要在金鑰的瀏覽器覆寫設定下啟用 context。
onConnect() => void否--語音工作階段連線時呼叫。
onDisconnect() => void否--工作階段結束時呼叫。
onError(error) => void否--發生錯誤時呼叫。錯誤物件包含 error(代碼)和 message 欄位。
ringtoneboolean | string否false連線期間播放鈴聲。true 使用預設鈴聲,或使用 URL 字串指定自訂音訊。

清理

ThunderPhone.mount() 會傳回具有清理方法的小工具執行個體:

<script>
  var widget = ThunderPhone.mount({
    element: '#thunderphone',
    publishableKey: 'pk_live_your_publishable_key',
  })
 
  // Later, when you want to remove the widget:
  widget.unmount()
 
  // Or equivalently:
  widget.destroy()
</script>

unmount() 和 destroy() 的作用相同——兩者都會中斷任何進行中的語音工作階段,並從 DOM 中移除小工具。請依照最適合你程式碼可讀性的方式使用。


範例

深色主題搭配自訂顏色

<div id="thunderphone"></div>
 
<link rel="stylesheet" href="https://cdn.thunderphone.com/widget/latest/style.css" />
<script src="https://cdn.thunderphone.com/widget/latest/widget.js"></script>
<script>
  ThunderPhone.mount({
    element: '#thunderphone',
    publishableKey: 'pk_live_your_publishable_key',
    theme: 'dark',
    primaryColor: '#8b5cf6',
    title: 'Talk to our AI',
  })
</script>

自訂位置

<div id="thunderphone"></div>
 
<script src="https://cdn.thunderphone.com/widget/latest/widget.js"></script>
<script>
  ThunderPhone.mount({
    element: '#thunderphone',
    publishableKey: 'pk_live_your_publishable_key',
    position: 'bottom-left',
  })
</script>

搭配事件回呼

<div id="thunderphone"></div>
 
<link rel="stylesheet" href="https://cdn.thunderphone.com/widget/latest/style.css" />
<script src="https://cdn.thunderphone.com/widget/latest/widget.js"></script>
<script>
  ThunderPhone.mount({
    element: '#thunderphone',
    publishableKey: 'pk_live_your_publishable_key',
    onConnect: function () {
      console.log('Call started')
    },
    onDisconnect: function () {
      console.log('Call ended')
    },
    onError: function (error) {
      console.error('Widget error:', error.error, error.message)
    },
  })
</script>

搭配鈴聲

<div id="thunderphone"></div>
 
<link rel="stylesheet" href="https://cdn.thunderphone.com/widget/latest/style.css" />
<script src="https://cdn.thunderphone.com/widget/latest/widget.js"></script>
<script>
  ThunderPhone.mount({
    element: '#thunderphone',
    publishableKey: 'pk_live_your_publishable_key',
    ringtone: true, // or a custom URL: 'https://example.com/ringtone.mp3'
  })
</script>

使用 DOM 元素參照

<div id="thunderphone"></div>
 
<script src="https://cdn.thunderphone.com/widget/latest/widget.js"></script>
<script>
  var container = document.getElementById('thunderphone')
 
  ThunderPhone.mount({
    element: container,
    publishableKey: 'pk_live_your_publishable_key',
  })
</script>

WordPress/CMS 整合

將以下內容加入自訂 HTML 區塊或佈景主題的頁尾:

<link rel="stylesheet" href="https://cdn.thunderphone.com/widget/latest/style.css" />
<script src="https://cdn.thunderphone.com/widget/latest/widget.js"></script>
 
<div id="thunderphone-widget"></div>
<script>
  ThunderPhone.mount({
    element: '#thunderphone-widget',
    publishableKey: 'pk_live_your_publishable_key',
  })
</script>

疑難排解

小工具未顯示

確認 CSS 和 JS 檔案都已載入。檢查瀏覽器主控台是否有網路錯誤。確認在呼叫 ThunderPhone.mount() 前,目標元素已存在於 DOM 中。

ThunderPhone 未定義

指令碼尚未載入。確認 widget.js 的 <script> 標籤位於掛載呼叫之前,或將掛載呼叫包裝在 DOMContentLoaded 監聽器中。

不允許網域錯誤

前往 app.thunderphone.com 的 網頁小工具 頁面,將你的網域新增至小工具的允許網域。正式環境不會自動允許 localhost;請將其新增至清單以進行本機測試。


後續步驟