#outbound call using twilio with elvenlabs conversational ai python

1 messages · Page 1 of 1 (latest)

sweet sentinelBOT
#
AI-generated Answer for supriya_06985

To make outbound calls using Twilio and ElevenLabs Conversational AI in Python, you can integrate the two systems by following these steps:

1. Set Up Your Environment

  • Create accounts with Twilio and ngrok.
  • Ensure you have Python 3.7+ installed.
  • Install the required Python packages:
    pip install elevenlabs twilio flask flask-socketio
    

2. Configure Your Agent

  • Set the audio format to "μ-law 8000 Hz" in both the TTS Output and Input Format settings of your ElevenLabs agent.

3. Create the Python Script

Below is an example of how to set up the integration:

import os
from flask import Flask, request
from flask_socketio import SocketIO
from elevenlabs import ElevenLabs
from twilio.twiml.voice_response import VoiceResponse

app = Flask(__name__)
socketio = SocketIO(app)

# Load environment variables
ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
TWILIO_PHONE_NUMBER = os.getenv("TWILIO_PHONE_NUMBER")
SERVER_DOMAIN = os.getenv("SERVER_DOMAIN")

elevenlabs = ElevenLabs(api_key=ELEVENLABS_API_KEY)
voice_id = "your_voice_id"
output_format = "ulaw_8000"

@app.route("/call/outbound", methods=["POST"])
def outbound_call():
    # Twilio will call this endpoint to initiate the call
    response = VoiceResponse()
    response.connect().stream(url=f"wss://{SERVER_DOMAIN}/call/connection")
    return str(response)

@socketio.on("message")
def handle_message(data):
    # Handle WebSocket messages from Twilio
    if data.get("event") == "start":
        stream_sid = data["start"]["streamSid"]
        text = "Hello, this is a test call from ElevenLabs AI."
        audio = elevenlabs.text_to_speech.convert(
            voice_id, {"text": text, "output_format": output_format}
        )
        socketio.emit(
            "media",
            {
                "streamSid": stream_sid,
                "event": "media",
                "media": {"payload": audio.read().encode("base64")},
            },
        )

if __name__ == "__main__":
    socketio.run(app, host="0.0.0.0", port=5000)

4. Set Up ngrok

Run the following command to expose your local server:

ngrok http 5000

Copy the ngrok domain and set it as the SERVER_DOMAIN in your environment variables.

5. Configure Twilio

  • In the Twilio Console, create a new phone number.
  • Set the "A call comes in" webhook to your ngrok URL with the /call/outbound path.

6. Test the Setup

  • Use Twilio's API to initiate an outbound call to a phone number:
    from twilio.rest import Client
    
    account_sid = "your_account_sid"
    auth_token = "your_auth_token"
    client = Client(account_sid, auth_token)
    
    call = client.calls.create(
        to="+1234567890",  # Replace with the recipient's phone number
        from_=TWILIO_PHONE_NUMBER,
        url=f"https://{SERVER_DOMAIN}/call/outbound"
    )
    print(f"Call SID: {call.sid}")
    

7. Deploy to Production

  • Replace ngrok with a production server and update the Twilio webhook URL accordingly.

This setup allows you to make outbound calls using ElevenLabs Conversational AI and Twilio. Let me know if you need further assistance!

pine loom
#

how to call specific number