動態逐通話設定
針對每一通來電,分別選擇接聽的智慧體,或改寫其提示詞與設定——由你控制的 Webhook 中的自訂邏輯驅動。
預設情況下,每個電話號碼和可公開金鑰都會指派一個靜態智慧體。當你需要依 每位來電者 或 每位訪客 自訂設定——VIP 路由、已登入使用者情境、A/B 提示詞測試——請切換至 Webhook 模式,讓你的伺服器決定。
運作方式
- 訂閱
telephony.incoming(電話)或web.incoming(小工具) 事件。兩者皆為 阻塞式 Webhook:ThunderPhone 會在繼續通話前,最多等待你的回應 10 秒。 - ThunderPhone 會傳送
{call_id, from_number, to_number}給你(小工具 工作階段會傳送小工具專屬欄位,而非電話號碼——請參閱 請求結構)。 - 你的伺服器會回傳智慧體設定(提示詞、語音、 產品、工具)。ThunderPhone 會在該通話中使用此設定。
- 若你回傳
{}、逾時或發生錯誤,系統會改用靜態指派的 智慧體作為備援。這是安全的預設行為。
1. 設定 Webhook 目的地
針對電話號碼,請讓你的端點訂閱 telephony.incoming:
curl -X POST https://api.thunderphone.com/v1/developer/webhook-endpoints \
-H "Authorization: Bearer sk_live_YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"label": "Prod call-incoming",
"url": "https://example.com/thunderphone/incoming",
"events": ["telephony.incoming"]
}'回應會包含一次性的 secret——請儲存它;你會使用它
進行簽章驗證。
針對小工具工作階段,建立一個 mode="webhook" 的可公開金鑰,
並將你的端點 URL 內建於其中:
curl -X POST https://api.thunderphone.com/v1/publishable-key \
-H "Authorization: Bearer sk_live_YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"name": "Dynamic widget",
"mode": "webhook",
"webhook_url": "https://example.com/thunderphone/widget-incoming",
"allowed_domains": ["example.com"]
}'小工具會在每次工作階段開始時 POST 至此 URL。
2. 實作處理常式
三項實用原則:
- 每個請求都要驗證簽章(請參閱 驗證 Webhook 簽章)。 不要在開發環境略過這一步——一次正確實作後即可重複使用。
- 快速回應。十秒是硬性上限,而且每一秒對來電者而言都是 無聲等待。你可以視需要查詢資料庫,但不要同步呼叫下游 LLM——若要動態產生提示詞,請預先運算並快取。
- 妥善備援。任何未預期的狀態都應回傳
{},讓靜態指定的智慧體處理通話。
import hashlib
import hmac
import json
import os
from fastapi import FastAPI, HTTPException, Request
app = FastAPI()
SECRET = os.environ["THUNDERPHONE_WEBHOOK_SECRET"]
def verify(body: bytes, sig: str) -> bool:
expected = hmac.new(SECRET.encode(), body, hashlib.sha256).hexdigest()
return hmac.compare_digest(expected, sig or "")
@app.post("/thunderphone/incoming")
async def incoming(request: Request):
body = await request.body()
if not verify(body, request.headers.get("X-ThunderPhone-Signature", "")):
raise HTTPException(401)
event = json.loads(body)
if event["type"] not in ("telephony.incoming", "web.incoming"):
return {} # fall back to default
caller = event["data"]["from_number"]
# Cheap DB lookup: is this a known VIP?
customer = lookup_customer(caller)
if customer and customer.tier == "vip":
return {
"prompt": f"You are a VIP concierge for {customer.name}. Be proactive…",
"voice": "john",
"product": "storm-base",
}
return {} # default agent handles non-VIPs
def lookup_customer(phone: str):
# ... your CRM integration ...
passimport crypto from "node:crypto";
import express from "express";
const app = express();
const SECRET = process.env.THUNDERPHONE_WEBHOOK_SECRET;
function verify(body, sig) {
const expected = crypto.createHmac("sha256", SECRET).update(body).digest("hex");
return sig &&
crypto.timingSafeEqual(Buffer.from(expected), Buffer.from(sig));
}
app.post(
"/thunderphone/incoming",
express.raw({ type: "application/json" }),
async (req, res) => {
if (!verify(req.body, req.header("X-ThunderPhone-Signature"))) {
return res.sendStatus(401);
}
const event = JSON.parse(req.body.toString("utf8"));
const IMPORTANT_TYPES = new Set([
"telephony.incoming",
"web.incoming",
]);
if (!IMPORTANT_TYPES.has(event.type)) return res.json({});
const customer = await lookupCustomer(event.data.from_number);
if (customer?.tier === "vip") {
return res.json({
prompt: `You are a VIP concierge for ${customer.name}. Be proactive…`,
voice: "john",
product: "storm-base",
});
}
res.json({}); // fall back to default agent
},
);3. 回應結構描述
回應主體會完全符合 來電回應結構描述。 常用欄位如下:
| 欄位 | 類型 | 說明 |
|---|---|---|
prompt | 字串(必填) | 智慧體的系統提示詞 |
voice | 字串(必填) | 來自 GET /v1/voices 的語音 ID |
product | 字串 | 預設為 spark |
background_track | 字串 | null | 環境音訊 ID |
acknowledgement_prompt_mode | 字串 | auto 或 manual(僅限具確認提示詞的 Storm) |
acknowledgement_prompt | 字串 | 當模式為 manual 時必填 |
tools | 陣列 | 內嵌函式工具結構描述——請參閱函式工具 |
模式
已登入使用者情境
在 Webhook 模式的小工具中,訪客所在頁面已經知道其身分。使用小工具 SDK 會轉送的查詢字串參數(?customer_id=123)呼叫你的 Webhook,並在伺服器端查詢客戶資料。
A/B 提示詞發布
在自行實作之前,請注意 ThunderPhone 提供原生的
實驗功能
(/dashboard/experiments 與智慧體建置器的 A/B 分頁),可定義變體、分配流量,並比較各變體的結果——無需 Webhook。
如果你仍需要在 Webhook 端控制:雜湊 call_id → 分桶;對 0..49 提供提示詞 A,對 50..99 提供提示詞 B。將你選擇的分桶記錄在自己的資料庫中,之後再與已完成通話的評分建立關聯。
依時間路由
營業時間 →「真人支援」智慧體;非營業時間 →「留言」智慧體。在處理常式中,單純根據 new Date().getUTCHours() 切換即可。