const express = require("express");
const axios = require("axios");
const app = express();
app.use(express.json());
const TELNYX_API_KEY = process.env.TELNYX_API_KEY;
const telnyxApi = axios.create({
baseURL: "https://api.telnyx.com/v2",
headers: { Authorization: `Bearer ${TELNYX_API_KEY}` },
});
// Answer incoming calls with deepfake detection enabled
app.post("/webhooks/call-initiated", async (req, res) => {
const { call_control_id } = req.body.payload;
await telnyxApi.post(
`/calls/${call_control_id}/actions/answer`,
{
deepfake_detection: {
enabled: true,
timeout: 15,
rtp_timeout: 30,
},
}
);
res.sendStatus(200);
});
// Handle deepfake detection results
app.post("/webhooks/deepfake-result", async (req, res) => {
const { call_control_id, result, score, consistency } =
req.body.payload;
console.log(
`Detection result: ${result}, score: ${score}, consistency: ${consistency}%`
);
if (result === "fake" && score > 0.8) {
// High-confidence deepfake — hang up or route to fraud queue
await telnyxApi.post(
`/calls/${call_control_id}/actions/hangup`,
{}
);
console.log("Deepfake detected — call terminated.");
} else if (result === "real") {
// Human caller — proceed normally
await telnyxApi.post(
`/calls/${call_control_id}/actions/speak`,
{
payload: "Welcome! How can we help you today?",
language: "en-US",
voice: "female",
}
);
}
// silence_timeout — caller didn't speak; handle as needed
res.sendStatus(200);
});
// Handle detection errors gracefully
app.post("/webhooks/deepfake-error", async (req, res) => {
const { call_control_id, error_message } = req.body.payload;
console.log(`Deepfake detection error: ${error_message}`);
// Continue the call normally if detection fails
await telnyxApi.post(
`/calls/${call_control_id}/actions/speak`,
{
payload: "Welcome! How can we help you today?",
language: "en-US",
voice: "female",
}
);
res.sendStatus(200);
});
app.listen(3000, () => console.log("Webhook server running on port 3000"));