ThunderPhone 2.0 正式上线。全程自助,2 美分/分钟起。查看发布公告

Widget

CDN / 脚本标签

无需打包工具,即可将 ThunderPhone 语音小部件添加到任何网站

CDN 构建在内部捆绑了 React,因此您可以在静态网站、WordPress、Webflow,或任何可以添加 HTML 的页面上使用该小组件。无需 npm、打包工具或框架。

CDN URL

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--每个会话的语言覆盖设置——语言代码或区域设置,例如 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>

故障排除

小组件未显示

请确保 CSS 和 JS 文件均已加载。检查浏览器控制台中是否存在网络错误。请确认在调用 ThunderPhone.mount() 前,目标元素已存在于 DOM 中。

ThunderPhone 未定义

脚本尚未加载。请确保 widget.js<script> 标签位于挂载调用之前,或者将挂载调用包装在 DOMContentLoaded 监听器中。

不允许的域名错误

app.thunderphone.com开发者 设置中,将您的域名添加到允许的域名列表。请注意,始终允许使用 localhost


后续步骤