I’m working with OpenAI’s Realtime API and Twilio for voice calls. I want the AI agent to be able to end the call when it decides the conversation is over.
Here’s the relevant part of my WebSocket handler:
async def handle_media_stream(websocket: WebSocket):
async with websockets.connect(
'wss://api.openai.com/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01',
extra_headers={
"Authorization": f"Bearer {OPENAI_API_KEY}",
"OpenAI-Beta": "realtime=v1"
}
) as openai_ws:
# ... other code ...
async def send_to_twilio():
async for openai_message in openai_ws:
response = json.loads(openai_message)
if response['type'] == 'response.audio.delta' and response.get('delta'):
# handle audio response
pass
I tried creating a function to end the call:
async def end_twilio_call(call_sid):
client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
call = client.calls(call_sid).update(status="completed")
But I’m not sure:
- How to properly detect when the AI wants to end the call
- How to get the call_sid in the WebSocket handler
- What’s the best way to implement this without breaking the stream
Any help would be appreciated! Has anyone implemented something similar