Install Express if you haven't already:
npm install express
Then add a POST route that parses the event body and switches on the event type:
import express from 'express';
const app = express();
app.use(express.json());
app.post('/api/webhook', async (req, res) => {
const { type, call, timestamp } = req.body;
console.log(`Webhook received: ${type} at ${timestamp}`);
switch (type) {
case 'call-started':
console.log(`Call ${call.id} started with ${call.customer.number}`);
break;
case 'speech-update':
console.log(`User said: ${req.body.transcript}`);
break;
case 'function-call': {
const { functionName, parameters } = req.body.functionCall;
const result = await processFunction(functionName, parameters);
return res.json({ result });
}
case 'assistant-request': {
// Return a transient assistant configuration or reject the call
return res.json({
assistant: {
name: 'Inbound Receptionist',
firstMessage: 'Hi there! How can I help you today?'
}
});
}
case 'call-ended':
console.log(`Call ended. Duration: ${call.duration}s`);
break;
default:
console.log(`Unhandled event type: ${type}`);
}
res.status(200).send();
});
app.listen(3000, () => console.log('Server listening on port 3000'));
async function processFunction(name, params) {
// Your custom function logic here
return { success: true };
}