import express from 'express';
import https from 'https';
import twilio from 'twilio';
const app = express();
const port = 3000;
// Configuration
const ULTRAVOX_API_KEY = 'your_ultravox_api_key_here';
const ULTRAVOX_API_URL = 'https://api.ultravox.ai/api/calls';
// Ultravox configuration
const SYSTEM_PROMPT = 'Your name is Steve. You are receiving a phone call. Ask them their name and see how they are doing.';
const ULTRAVOX_CALL_CONFIG = {
systemPrompt: SYSTEM_PROMPT,
model: 'fixie-ai/ultravox',
voice: 'Mark',
temperature: 0.3,
firstSpeaker: 'FIRST_SPEAKER_AGENT',
medium: { "twilio": {} }
};
// Create Ultravox call and get join URL
async function createUltravoxCall() {
const request = https.request(ULTRAVOX_API_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': ULTRAVOX_API_KEY
}
});
return new Promise((resolve, reject) => {
let data = '';
request.on('response', (response) => {
response.on('data', chunk => data += chunk);
response.on('end', () => resolve(JSON.parse(data)));
});
request.on('error', reject);
request.write(JSON.stringify(ULTRAVOX_CALL_CONFIG));
request.end();
});
}
// Handle incoming calls
app.post('/incoming', async (req, res) => {
try {
console.log('Incoming call received');
const response = await createUltravoxCall();
const twiml = new twilio.twiml.VoiceResponse();
const connect = twiml.connect();
connect.stream({
url: response.joinUrl,
name: 'ultravox'
});
const twimlString = twiml.toString();
res.type('text/xml');
res.send(twimlString);
} catch (error) {
console.error('Error handling incoming call:', error);
const twiml = new twilio.twiml.VoiceResponse();
twiml.say('Sorry, there was an error connecting your call.');
res.type('text/xml');
res.send(twiml.toString());
}
});
// Start server
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});