#can you tell me how to use conversational ai for outbound calls in with twilio realtime?
1 messages · Page 1 of 1 (latest)
To use Conversational AI for outbound calls with Twilio in real-time, you can follow these general steps:
-
Set Up Your Environment: Ensure you have an ElevenLabs account with a configured Conversational Agent and a Twilio account with an active phone number.
-
Use Twilio's API for Outbound Calls: Twilio provides an API to initiate outbound calls. You can use this API to trigger calls from your application.
-
Integrate with ElevenLabs: Once the call is initiated, use the ElevenLabs API to generate the speech content you want the AI agent to deliver during the call.
-
Handle Call Logic: Implement logic to manage the call flow, such as playing pre-recorded messages, handling user input, and transferring calls if needed.
For more detailed guidance, you can refer to the integration guide with Twilio. This guide will help you create a seamless outbound calling experience with your AI agent. If you need any additional help with the setup, feel free to ask!
show me the code of the point 3.
Certainly! Here's a code snippet that demonstrates how to handle incoming calls and create text-to-speech using the ElevenLabs and Twilio integration in Node.js:
import 'dotenv/config';
import express, { Response } from 'express';
import ExpressWs from 'express-ws';
import VoiceResponse from 'twilio/lib/twiml/VoiceResponse';
import { ElevenLabsClient } from 'elevenlabs';
import { type WebSocket } from 'ws';
import { Readable } from 'stream';
const app = ExpressWs(express()).app;
const PORT: number = parseInt(process.env.PORT || '5000');
const elevenlabs = new ElevenLabsClient();
const voiceId = '21m00Tcm4TlvDq8ikWAM';
const outputFormat = 'ulaw_8000';
const text = 'This is a test. You can now hang up. Thank you.';
function startApp() {
app.post('/call/incoming', (_, res: Response) => {
const twiml = new VoiceResponse();
twiml.connect().stream({
url: `wss://${process.env.SERVER_DOMAIN}/call/connection`,
});
res.writeHead(200, { 'Content-Type': 'text/xml' });
res.end(twiml.toString());
});
app.ws('/call/connection', (ws: WebSocket) => {
ws.on('message', async (data: string) => {
const message: {
event: string;
start?: { streamSid: string; callSid: string };
} = JSON.parse(data);
if (message.event === 'start' && message.start) {
const streamSid = message.start.streamSid;
const response = await elevenlabs.textToSpeech.convert(voiceId, {
model_id: 'eleven_turbo_v2_5',
output_format: outputFormat,
text,
});
const readableStream = Readable.from(response);
const audioArrayBuffer = await streamToArrayBuffer(readableStream);
ws.send(
JSON.stringify({
streamSid,
event: 'media',
media: {
payload: Buffer.from(audioArrayBuffer as any).toString('base64'),
},
}),
);
}
});
ws.on('error', console.error);
});
app.listen(PORT, () => {
console.log(`Local: http://localhost:${PORT}`);
console.log(`Remote: https://${process.env.SERVER_DOMAIN}`);
});
}
function streamToArrayBuffer(readableStream: Readable) {
return new Promise((resolve, reject) => {
const chunks: Buffer[] = [];
readableStream.on('data', (chunk) => {
chunks.push(chunk);
});
readableStream.on('end', () => {
resolve(Buffer.concat(chunks).buffer);
});
readableStream.on('error', reject);
});
}
startApp();
This code sets up a server to handle incoming calls and uses the ElevenLabs API to convert text to speech, which is then sent to Twilio via a WebSocket connection. Make sure to replace placeholders with your actual server domain and API keys.
do it in python
To implement outbound calls using ElevenLabs' Conversational AI with Twilio in Python, you can follow these general steps:
-
Set Up Your Environment: Ensure you have an ElevenLabs account with a configured Conversational Agent and a Twilio account with an active phone number. You'll also need Python 3.7+ and ngrok for local development.
-
Configure Your Agent: Set the audio format to "μ-law 8000 Hz" in both the TTS Output and Input Format settings of your agent.
-
Implement the Integration: Use Python for the backend setup. Here's a basic example of how you might set up the integration:
from flask import Flask, request
from twilio.twiml.voice_response import VoiceResponse
from elevenlabs import ElevenLabsClient
import requests
app = Flask(__name__)
elevenlabs = ElevenLabsClient(api_key='YOUR_ELEVENLABS_API_KEY')
voice_id = 'YOUR_VOICE_ID'
output_format = 'ulaw_8000'
text = 'This is a test. You can now hang up. Thank you.'
@app.route("/call/incoming", methods=['POST'])
def incoming_call():
response = VoiceResponse()
response.connect().stream(url='wss://YOUR_SERVER_DOMAIN/call/connection')
return str(response)
@app.route("/call/connection", methods=['POST'])
def call_connection():
message = request.get_json()
if message['event'] == 'start':
stream_sid = message['start']['streamSid']
response = elevenlabs.text_to_speech.convert(voice_id, {
'model_id': 'eleven_turbo_v2_5',
'output_format': output_format,
'text': text
})
audio_array_buffer = response.content
requests.post('wss://YOUR_SERVER_DOMAIN/call/connection', json={
'streamSid': stream_sid,
'event': 'media',
'media': {
'payload': audio_array_buffer
}
})
return '', 200
if __name__ == "__main__":
app.run(port=5000)
-
Set Up Twilio: Use ngrok to create a public URL for your local server. Configure Twilio to use this URL for incoming calls by setting the webhook in the Twilio Console.
-
Test the Setup: Make a call to your Twilio number to ensure the integration works and the AI voice is correctly played.
This code provides a basic framework for handling incoming calls and streaming audio using the ElevenLabs API. Make sure to replace placeholders like YOUR_ELEVENLABS_API_KEY, YOUR_VOICE_ID, and YOUR_SERVER_DOMAIN with your actual credentials and server details.
but i want it using the conversational ai agent and i want it realtime