// NOTE: Twilio is now used only for the in-browser dial pad. This webhook // handles the browser-Device outbound-call leg only — everything else (SMS, // inbound/AI voice) moved to Telnyx, see routes/telnyx-webhooks.ts. import express, { Request, Response } from "express"; import { prisma as db } from "@repo/db/client"; const router = express.Router(); function escapeXml(text: string): string { return text .replace(/&/g, "&").replace(//g, ">") .replace(/"/g, """).replace(/'/g, "'"); } // POST /api/twilio/webhook/voice-browser // Called by Twilio when a browser Device (Voice SDK) places an outbound call. // Looks up the office's Twilio phone number by AccountSid and dials the patient. router.post("/webhook/voice-browser", async (req: Request, res: Response): Promise => { try { const { To, AccountSid } = req.body as { To?: string; AccountSid?: string }; if (!To || !AccountSid) { res.set("Content-Type", "text/xml"); return res.send("Missing call parameters."); } const settings = await db.twilioSettings.findFirst({ where: { accountSid: AccountSid } }); const callerId = settings?.phoneNumber || ""; res.set("Content-Type", "text/xml"); return res.send( `${escapeXml(To)}` ); } catch { res.set("Content-Type", "text/xml"); return res.send("An error occurred. Please try again."); } }); export default router;