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_id | integer | 通話 ID——在此通話的所有事件中保持不變 |
from_number | string | E.164 來電者號碼 |
to_number | string | E.164 目的地號碼(你的其中一個 ThunderPhone 號碼) |
網頁小工具工作階段(web.incoming)的 data 會識別嵌入頁面,
而非電話號碼:
{
"type": "web.incoming",
"data": {
"call_id": 987654322,
"origin_domain": "https://example.com",
"publishable_key_prefix": "pk_live_a1b2"
}
}
| 欄位 | 類型 | 說明 |
|---|---|---|
call_id | integer | 通話 ID |
origin_domain | string | 託管小工具的頁面來源 |
publishable_key_prefix | string | 開啟工作階段的可公開金鑰首幾個字元 |
language, primary_language | string | 小工具工作階段要求覆寫語言時提供 |
voice | string | 小工具工作階段要求覆寫語音時提供 |
website_context | string | 小工具傳送每個工作階段的頁面內容時提供 |
回應結構
傳回一個 JSON 物件,描述此通話的智能體設定。
prompt 及 voice 為必填;其餘全部為選填。
{
"prompt": "You are a helpful booking assistant for Acme Restaurant.",
"voice": "john",
"product": "spark",
"background_track": null,
"tools": []
}
| 欄位 | 類型 | 必填 | 說明 |
|---|---|---|---|
prompt | string | 是 | 驅動智能體的系統提示詞 |
voice | string | 是 | 來自 GET /v1/voices 的語音 ID,例如 john。voice_name 可作為別名使用。未知語音會無法通過驗證並拒絕通話 |
product | string | 否 | 預設為 spark。允許值:spark、bolt、storm-base、storm-base-with-ack、storm-extra、storm-extra-with-ack |
thinking_level | string | 否 | minimal、base(預設)或 extra。Storm 產品會覆寫此設定:storm-extra* 強制使用 extra,其他 storm-* 強制使用 base |
audio_context_mode | string | 否 | full(預設)或 reduced |
watchdog_enabled | boolean | 否 | 為此通話啟用監管。預設為 false |
storm_feedback_mode | string | 否 | none、acknowledgement(預設)或 tick |
language | string | 否 | primary_language 的簡寫 |
primary_language | string | 否 | 語言代碼,會進行標準化處理(預設為 en)。無法解析的代碼會拒絕通話 |
has_additional_languages | boolean | 否 | 預設為 false |
additional_languages | array of string | 否 | 智能體可切換使用的其他語言 |
background_track | string | null | 否 | 環境音效 ID 或 null |
acknowledgement_prompt_mode | string | 否 | auto(預設)或 manual(Storm-with-ack 產品) |
acknowledgement_prompt | string | 否 | 當 acknowledgement_prompt_mode="manual" 時使用 |
silence_interval_seconds | integer | null | 否 | 5–120。來電者靜默多久後進行一次確認,單位為秒 |
silence_max_checkins | integer | null | 否 | 1–10 |
silence_checkins_enabled | boolean | 否 | 預設為 true |
connect_tone_enabled | boolean | 否 | 預設為 false |
voicemail_action | string | 否 | prompt(預設)、hangup 或 message |
voicemail_message | string | 否 | 當 voicemail_action="message" 時使用 |
agent_name | string | 否 | 顯示於控制台及小工具的智能體名稱 |
org_name | string | 否 | 用於智能體角色設定的機構顯示名稱 |
tools | array | 否 | 內嵌函式工具結構(請參閱函式工具) |
call_id | integer | 否 | 可選的請求通話 ID 回傳值;會被忽略 |
由於 prompt 及 voice 為必填,傳回 {} 或任何
未能通過驗證的回應都會以 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 | 較高 | 深度 | 思考期間自動填充回應 |