telephony.incoming / web.incoming

當未指派智能體嘅電話號碼收到來電,或網頁小工具工作階段以 mode="webhook" 可公開金鑰啟動時,ThunderPhone 會向你的 舊版 webhook URL 發送一個阻塞式 telephony.incoming / web.incoming 請求, 並最多等候 10 秒以接收設定回應。你可透過此交換,為每通電話動態選擇提示詞、語音及工具—— 完整模式請參閱動態通話設定指南

此阻塞式交換沒有後備方案:如你的處理程式傳回非 2xx 狀態、逾時, 或傳回未能通過驗證的設定,通話將被拒絕(電話不會接通;小工具工作階段 請求會以 502/422 失敗)。請迅速回應——當你作出決定時,來電者正聽到回鈴音。

請求負載

電話通話(telephony.incoming):

{
  "type": "telephony.incoming",
  "data": {
    "call_id":     987654321,
    "from_number": "+14155550199",
    "to_number":   "+15551234567"
  }
}
欄位類型說明
call_idinteger通話 ID——在此通話的所有事件中保持不變
from_numberstringE.164 來電者號碼
to_numberstringE.164 目的地號碼(你的其中一個 ThunderPhone 號碼)

網頁小工具工作階段(web.incoming)的 data 會識別嵌入頁面, 而非電話號碼:

{
  "type": "web.incoming",
  "data": {
    "call_id": 987654322,
    "origin_domain": "https://example.com",
    "publishable_key_prefix": "pk_live_a1b2"
  }
}
欄位類型說明
call_idinteger通話 ID
origin_domainstring託管小工具的頁面來源
publishable_key_prefixstring開啟工作階段的可公開金鑰首幾個字元
language, primary_languagestring小工具工作階段要求覆寫語言時提供
voicestring小工具工作階段要求覆寫語音時提供
website_contextstring小工具傳送每個工作階段的頁面內容時提供

回應結構

傳回一個 JSON 物件,描述此通話的智能體設定。 promptvoice 為必填;其餘全部為選填。

{
  "prompt":  "You are a helpful booking assistant for Acme Restaurant.",
  "voice":   "john",
  "product": "spark",
  "background_track": null,
  "tools":   []
}
欄位類型必填說明
promptstring驅動智能體的系統提示詞
voicestring來自 GET /v1/voices 的語音 ID,例如 johnvoice_name 可作為別名使用。未知語音會無法通過驗證並拒絕通話
productstring預設為 spark。允許值:sparkboltstorm-basestorm-base-with-ackstorm-extrastorm-extra-with-ack
thinking_levelstringminimalbase(預設)或 extra。Storm 產品會覆寫此設定:storm-extra* 強制使用 extra,其他 storm-* 強制使用 base
audio_context_modestringfull(預設)或 reduced
watchdog_enabledboolean為此通話啟用監管。預設為 false
storm_feedback_modestringnoneacknowledgement(預設)或 tick
languagestringprimary_language 的簡寫
primary_languagestring語言代碼,會進行標準化處理(預設為 en)。無法解析的代碼會拒絕通話
has_additional_languagesboolean預設為 false
additional_languagesarray of string智能體可切換使用的其他語言
background_trackstring | null環境音效 ID 或 null
acknowledgement_prompt_modestringauto(預設)或 manual(Storm-with-ack 產品)
acknowledgement_promptstringacknowledgement_prompt_mode="manual" 時使用
silence_interval_secondsinteger | null5–120。來電者靜默多久後進行一次確認,單位為秒
silence_max_checkinsinteger | null1–10
silence_checkins_enabledboolean預設為 true
connect_tone_enabledboolean預設為 false
voicemail_actionstringprompt(預設)、hangupmessage
voicemail_messagestringvoicemail_action="message" 時使用
agent_namestring顯示於控制台及小工具的智能體名稱
org_namestring用於智能體角色設定的機構顯示名稱
toolsarray內嵌函式工具結構(請參閱函式工具
call_idinteger可選的請求通話 ID 回傳值;會被忽略

由於 promptvoice 為必填,傳回 {} 或任何 未能通過驗證的回應都會以 422 拒絕通話——此路徑 沒有靜態智能體備援(Webhook 模式中的號碼或金鑰 並無已指派的智能體)。


回應大小上限


處理程式範例

import hashlib
import hmac
import json
import os

from fastapi import FastAPI, HTTPException, Request

app = FastAPI()
WEBHOOK_SECRET = os.environ["THUNDERPHONE_WEBHOOK_SECRET"]

def verify(body: bytes, signature: str) -> bool:
    expected = hmac.new(WEBHOOK_SECRET.encode(), body, hashlib.sha256).hexdigest()
    return hmac.compare_digest(expected, signature or "")

@app.post("/thunderphone-webhook")
async def webhook(request: Request):
    body = await request.body()
    if not verify(body, request.headers.get("X-ThunderPhone-Signature", "")):
        raise HTTPException(status_code=401)

    event = json.loads(body)
    if event["type"] == "telephony.incoming":
        caller = event["data"]["from_number"]
        prompt = (
            "Greet the caller as a San Francisco local…"
            if caller.startswith("+1415")
            else "You are a friendly customer support agent…"
        )
        return {
            "prompt": prompt,
            "voice": "john",
            "product": "spark",
        }
    if event["type"] == "web.incoming":
        return {
            "prompt": "You are the website's helpful voice assistant…",
            "voice": "john",
            "product": "spark",
        }
    return {}
import crypto from "node:crypto";
import express from "express";

const app = express();
const SECRET = process.env.THUNDERPHONE_WEBHOOK_SECRET;

function verify(body, signature) {
  const expected = crypto
    .createHmac("sha256", SECRET)
    .update(body)
    .digest("hex");
  return signature &&
    crypto.timingSafeEqual(Buffer.from(expected), Buffer.from(signature));
}

app.post(
  "/thunderphone-webhook",
  express.raw({ type: "application/json" }),
  (req, res) => {
    if (!verify(req.body, req.header("X-ThunderPhone-Signature"))) {
      return res.sendStatus(401);
    }
    const event = JSON.parse(req.body.toString("utf8"));

    if (event.type === "telephony.incoming" || event.type === "web.incoming") {
      const caller = event.data.from_number || "web";
      const prompt = caller.startsWith("+1415")
        ? "Greet the caller as a San Francisco local…"
        : "You are a friendly customer support agent…";
      return res.json({
        prompt,
        voice: "john",
        product: "spark",
      });
    }
    res.json({});
  },
);

使用函式工具回應

附加工具,讓 AI 可在對話期間呼叫你的 API:

{
  "prompt":  "You are a booking assistant. Use the available tools to help customers schedule appointments.",
  "voice":   "john",
  "product": "spark",
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "search_appointments",
        "description": "Find available appointment slots",
        "parameters": {
          "type": "object",
          "properties": {
            "date": { "type": "string", "description": "YYYY-MM-DD" },
            "service": { "type": "string" }
          },
          "required": ["date"]
        }
      },
      "endpoint": {
        "url": "https://api.example.com/appointments/search",
        "method": "POST",
        "headers": {
          "X-Api-Key": "your-key"
        }
      }
    }
  ]
}

產品級別速查表

產品延遲推理能力確認回應
spark最低基本
bolt改良
storm-base中等強大
storm-base-with-ack中等強大思考期間自動填充回應
storm-extra較高深度
storm-extra-with-ack較高深度思考期間自動填充回應

相關內容