CDN/Script Tag

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

CDN 網址

<link rel="stylesheet" href="https://cdn.thunderphone.com/widget/latest/style.css" />
<script src="https://cdn.thunderphone.com/widget/latest/widget.js"></script>
<!-- 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' (light) / '#ffffff' (dark)用作強調色的 CSS 色彩字串。
titlestring'Voice assistant'顯示於小工具列的文字。
position'bottom-right' | 'bottom-left' | 'top-right' | 'top-left''bottom-right'固定於檢視區的位置。
apiBasestring'https://api.thunderphone.com/v1'覆寫 API 基礎網址。
languagestring--按工作階段覆寫語言——可使用語言代碼或地區設定,例如 enesfr-FR。如未設定,將使用智能體已設定的語言。
voicestring--按工作階段覆寫語音——可使用語音名稱,例如 maria。如未設定,將使用智能體已設定的語音。
contextstring--按工作階段將頁面或網站的事實內容傳送至智能體。伺服器端會截斷至 12,000 個字元。
onConnect() => void--語音工作階段連線時呼叫。
onDisconnect() => void--工作階段結束時呼叫。
onError(error) => void--發生錯誤時呼叫。錯誤包含 error(代碼)及 message 欄位。
ringtoneboolean | stringfalse連線期間播放鈴聲。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>

疑難排解

Widget 未有顯示

確保 CSS 及 JS 檔案均已載入。在瀏覽器主控台檢查網絡錯誤。呼叫 ThunderPhone.mount() 前,確認目標元素已存在於 DOM 中。

ThunderPhone 未有定義

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

不允許網域錯誤

app.thunderphone.com開發人員 設定中,將你的網域加入允許網域清單。請注意,localhost 永遠獲允許。


下一步