async function callNewLead(lead) {
const res = await fetch("https://api.telleo.ai/v1/calls", {
method: "POST",
headers: {
"X-API-Key": process.env.TELLEO_API_KEY,
"Content-Type": "application/json",
"Idempotency-Key": `lead-${lead.id}`,
},
body: JSON.stringify({
agent_id: process.env.TELLEO_AGENT_ID,
phone_number: lead.phone, // +91XXXXXXXXXX
contact: { name: lead.firstName, email: lead.email, external_id: String(lead.id) },
variables: { course: lead.courseName, city: lead.city },
metadata: { owner_id: lead.ownerId },
}),
});
const body = await res.json();
if (res.status === 201 || res.status === 200) {
await db.leads.update(lead.id, { telleoCallId: body.id, callStatus: body.status });
} else if (body.error?.code === "call_already_queued") {
// A call for this number is already waiting: nothing to do.
} else {
throw new Error(`${res.status} ${body.error?.code}: ${body.error?.message}`);
}
}