telephony.incoming / web.incoming

เมื่อสายโทรศัพท์ขาเข้ามาถึงหมายเลขที่ยังไม่ได้กำหนด เอเจนต์ หรือเมื่อเซสชันวิดเจ็ตเว็บเริ่มต้นด้วย publishable key ใน mode="webhook" ThunderPhone จะส่งคำขอ telephony.incoming / web.incoming แบบบล็อกการทำงานไปยัง URL webhook เดิม ของคุณ และรอการตอบกลับการกำหนดค่าสูงสุด 10 วินาที ใช้การแลกเปลี่ยนนี้เพื่อเลือกพรอมป์ต์ เสียง และเครื่องมือแบบไดนามิกสำหรับแต่ละสาย — ดูรูปแบบการทำงานแบบครบวงจรได้ที่ คู่มือการกำหนดค่าสายแบบไดนามิก

การแลกเปลี่ยนแบบบล็อกการทำงานไม่มีทางเลือกสำรอง: หากตัวจัดการของคุณส่งคืนสถานะ ที่ไม่ใช่ 2xx หมดเวลา หรือส่งคืนการกำหนดค่าที่ไม่ผ่านการตรวจสอบ สายจะถูกปฏิเสธ (สายโทรศัพท์จะไม่เชื่อมต่อ ส่วนคำขอเซสชันวิดเจ็ต จะล้มเหลวด้วย 502/422) ตอบกลับให้รวดเร็ว — ผู้โทรจะได้ยินเสียงเรียกเข้าระหว่างที่คุณตัดสินใจ

เพย์โหลดคำขอ

สำหรับสายโทรศัพท์ (telephony.incoming):

{
  "type": "telephony.incoming",
  "data": {
    "call_id":     987654321,
    "from_number": "+14155550199",
    "to_number":   "+15551234567"
  }
}
ฟิลด์ประเภทคำอธิบาย
call_idintegerรหัสสาย — คงที่ตลอดทุกเหตุการณ์ของสายนี้
from_numberstringหมายเลขผู้โทรรูปแบบ E.164
to_numberstringปลายทางรูปแบบ 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_idintegerรหัสสาย
origin_domainstringต้นทางของหน้าที่โฮสต์วิดเจ็ต
publishable_key_prefixstringอักขระแรกของ publishable key ที่เปิดเซสชัน
language, primary_languagestringมีอยู่เมื่อเซสชันวิดเจ็ตขอแทนที่ภาษาที่ใช้
voicestringมีอยู่เมื่อเซสชันวิดเจ็ตขอแทนที่เสียงที่ใช้
website_contextstringมีอยู่เมื่อวิดเจ็ตส่งบริบทหน้าสำหรับแต่ละเซสชัน

สคีมาการตอบกลับ

ส่งคืนออบเจ็กต์ JSON ที่อธิบายการกำหนดค่าเอเจนต์สำหรับสายนี้ ต้องระบุ prompt และ voice; ส่วนอื่นทั้งหมดเป็นตัวเลือก

{
  "prompt":  "You are a helpful booking assistant for Acme Restaurant.",
  "voice":   "john",
  "product": "spark",
  "background_track": null,
  "tools":   []
}
ฟิลด์ประเภทจำเป็นคำอธิบาย
promptstringใช่พรอมป์ตระบบที่ขับเคลื่อนเอเจนต์
voicestringใช่รหัสเสียงจาก GET /v1/voices เช่น john ยอมรับ voice_name เป็นชื่อแทน เสียงที่ไม่รู้จักจะไม่ผ่านการตรวจสอบและปฏิเสธสาย
productstringไม่ค่าเริ่มต้นคือ spark อนุญาต: spark, bolt, storm-base, storm-base-with-ack, storm-extra, storm-extra-with-ack
thinking_levelstringไม่minimal, base (ค่าเริ่มต้น) หรือ extra ถูกแทนที่สำหรับผลิตภัณฑ์ Storm: storm-extra* บังคับใช้ extra ส่วน storm-* อื่นบังคับใช้ base
audio_context_modestringไม่full (ค่าเริ่มต้น) หรือ reduced
watchdog_enabledbooleanไม่เปิดใช้การกำกับดูแลสำหรับสายนี้ ค่าเริ่มต้นคือ false
storm_feedback_modestringไม่none, acknowledgement (ค่าเริ่มต้น) หรือ tick
languagestringไม่ชื่อย่อสำหรับ primary_language
primary_languagestringไม่รหัสภาษาแบบปรับมาตรฐานแล้ว (ค่าเริ่มต้น en) รหัสที่ไม่สามารถแก้ไขได้จะปฏิเสธสาย
has_additional_languagesbooleanไม่ค่าเริ่มต้นคือ false
additional_languagesarray of stringไม่ภาษาเพิ่มเติมที่เอเจนต์สามารถสลับไปใช้ได้
background_trackstring | nullไม่รหัสเสียงบรรยากาศหรือ null
acknowledgement_prompt_modestringไม่auto (ค่าเริ่มต้น) หรือ manual (ผลิตภัณฑ์ Storm-with-ack)
acknowledgement_promptstringไม่ใช้เมื่อ acknowledgement_prompt_mode="manual"
silence_interval_secondsinteger | nullไม่5–120 จำนวนวินาทีที่ผู้โทรเงียบก่อนการตรวจสอบสถานะ
silence_max_checkinsinteger | nullไม่1–10
silence_checkins_enabledbooleanไม่ค่าเริ่มต้นคือ true
connect_tone_enabledbooleanไม่ค่าเริ่มต้นคือ false
voicemail_actionstringไม่prompt (ค่าเริ่มต้น), hangup หรือ message
voicemail_messagestringไม่ใช้เมื่อ voicemail_action="message"
agent_namestringไม่ชื่อที่แสดงซึ่งรายงานไปยังแดชบอร์ดและวิดเจ็ต
org_namestringไม่ชื่อที่แสดงขององค์กรสำหรับบุคลิกของเอเจนต์
toolsarrayไม่สคีมาเครื่องมือฟังก์ชันแบบอินไลน์ (ดู เครื่องมือฟังก์ชัน)
call_idintegerไม่การส่งกลับรหัสสายจากคำขอแบบเลือกใช้ จะถูกละเว้น

เนื่องจากต้องระบุ 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สูงกว่าเชิงลึกเสียงเติมอัตโนมัติระหว่างคิด

ที่เกี่ยวข้อง