Skip to main content

概述

语音通话扩展为 OpenClaw 添加了语音通话能力,支持多个电话服务提供商。你可以让 AI 助手拨打电话、接听来电,并通过语音进行自然对话。
语音通话扩展支持 Twilio、Telnyx 和 Plivo 作为电话服务提供商,同时提供 mock 模式用于开发测试。

安装

npm install @openclaw/voice-call

配置

在你的配置文件中添加语音通话设置:
voiceCall:
  enabled: true
  provider: twilio  # 或 telnyx、mock
  fromNumber: "+15550001234"
  toNumber: "+15550005678"
  
  # Twilio 配置
  twilio:
    accountSid: "your_account_sid"
    authToken: "your_auth_token"
  
  # Telnyx 配置
  telnyx:
    apiKey: "your_api_key"
    connectionId: "your_connection_id"
    publicKey: "your_public_key"
  
  # 通话模式
  outbound:
    defaultMode: notify  # notify 或 conversation
    notifyHangupDelaySec: 3
  
  # Webhook 设置
  serve:
    port: 3000
    bind: "0.0.0.0"
    path: "/voice"

扩展结构

语音通话扩展实现了完整的插件生命周期:
extensions/voice-call/index.ts
const voiceCallPlugin = {
  id: "voice-call",
  name: "Voice Call",
  description: "Voice-call plugin with Telnyx/Twilio/Plivo providers",
  configSchema: voiceCallConfigSchema,
  
  register(api: OpenClawPluginApi) {
    const config = resolveVoiceCallConfig(
      voiceCallConfigSchema.parse(api.pluginConfig)
    );
    
    // 注册网关方法
    api.registerGatewayMethod("voicecall.initiate", async ({ params, respond }) => {
      const message = typeof params?.message === "string" ? params.message.trim() : "";
      const to = params?.to || config.toNumber;
      const result = await runtime.manager.initiateCall(to, undefined, { message });
      respond(result.success, { callId: result.callId });
    });
    
    // 注册 AI 工具
    api.registerTool({
      name: "voice_call",
      label: "Voice Call",
      description: "Make phone calls and have voice conversations",
      parameters: VoiceCallToolSchema,
      async execute(toolCallId, params) {
        // 处理拨打电话、继续对话等操作
      }
    });
    
    // 注册 CLI 命令
    api.registerCli(({ program }) => {
      registerVoiceCallCli({ program, config, ensureRuntime });
    }, { commands: ["voicecall"] });
    
    // 注册服务
    api.registerService({
      id: "voicecall",
      start: async () => {
        if (config.enabled) {
          await ensureRuntime();
        }
      },
      stop: async () => {
        if (runtimePromise) {
          const rt = await runtimePromise;
          await rt.stop();
        }
      }
    });
  }
};

export default voiceCallPlugin;

使用方式

1

启动语音服务

启动 OpenClaw 网关,语音通话服务会自动初始化:
openclaw gateway run
2

拨打电话

使用 CLI 命令拨打电话:
openclaw voicecall start +15550005678 --message "你好,我是你的 AI 助手"
3

通过工具调用

AI 助手可以使用 voice_call 工具主动拨打电话:
{
  "action": "initiate_call",
  "to": "+15550005678",
  "message": "提醒你今天下午有个会议",
  "mode": "notify"
}

网关方法

扩展注册了以下网关方法供其他服务调用:

voicecall.initiate

发起新的通话
{
  to: "+15550005678",
  message: "初始消息",
  mode: "notify" | "conversation"
}

voicecall.continue

在通话中继续对话
{
  callId: "call_123",
  message: "后续消息"
}

voicecall.speak

向通话中播放消息
{
  callId: "call_123",
  message: "要播放的内容"
}

voicecall.status

查询通话状态
{
  callId: "call_123"
}

配置选项

基础设置

选项类型描述
enabledboolean是否启用语音通话功能
providerstring服务提供商:twilio/telnyx/mock
fromNumberstring呼出号码
toNumberstring默认接收号码

来电策略

选项类型描述
inboundPolicystring来电策略:allow/deny
allowFromstring[]来电白名单
inboundGreetingstring来电欢迎语

TTS 设置

语音通话扩展支持自定义 TTS 提供商:
voiceCall:
  tts:
    provider: openai  # 或 elevenlabs
    openai:
      model: tts-1
      voice: alloy
      apiKey: your_key
    elevenlabs:
      modelId: eleven_monolingual_v1
      voiceId: your_voice_id
      apiKey: your_key

流式对话

扩展支持使用 OpenAI Realtime API 进行流式语音对话:
voiceCall:
  streaming:
    enabled: true
    openaiApiKey: your_openai_key
    sttModel: whisper-1
    streamPath: "/stream"
流式模式可实现更低延迟的实时对话,但需要 OpenAI Realtime API 密钥。

工具参数

voice_call 工具支持以下操作:
{
  "action": "initiate_call",
  "to": "+15550005678",
  "message": "你好,这是一个测试电话",
  "mode": "conversation"  // notify 或 conversation
}

Webhook 配置

电话服务提供商需要配置 Webhook URL 来接收通话事件:
https://your-domain.com/voice
如果使用隧道服务(如 ngrok),扩展可以自动配置:
voiceCall:
  tunnel:
    provider: ngrok
    ngrokAuthToken: your_ngrok_token
    ngrokDomain: your-domain.ngrok.io  # 可选

调试和测试

使用 mock 提供商进行本地测试:
voiceCall:
  provider: mock
  enabled: true
Mock 模式会模拟电话服务的行为,但不会真正拨打电话,适合开发和测试。

相关资源