Skip to content

Commit 52cc23f

Browse files
voice works, tool called, but weather api not actually connected
1 parent a0c1552 commit 52cc23f

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

main.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,17 @@ async def handle_media_stream(websocket: WebSocket):
195195
print("Client connected")
196196
await websocket.accept()
197197

198+
# Initialize variables
199+
stream_sid = None
200+
latest_media_timestamp = 0
201+
202+
# Set up OpenAI Realtime client
198203
client = RealtimeClient(api_key=OPENAI_API_KEY)
199204
await client.connect()
200205

201206
async def receive_from_twilio():
202207
"""Receive audio data from Twilio and send it to the OpenAI Realtime API."""
208+
nonlocal stream_sid, latest_media_timestamp
203209
try:
204210
async for message in websocket.iter_text():
205211
data = json.loads(message)
@@ -211,13 +217,43 @@ async def receive_from_twilio():
211217
}
212218
await client.ws.send(json.dumps(audio_append))
213219
elif data['event'] == 'start':
214-
print(f"Incoming stream has started {data['start']['streamSid']}")
220+
stream_sid = data['start']['streamSid']
221+
print(f"Incoming stream has started {stream_sid}")
215222
except WebSocketDisconnect:
216223
print("Client disconnected.")
217224
await client.close()
218225

219226
async def send_to_twilio():
220-
await client.handle_messages()
227+
"""Receive events from the OpenAI Realtime API and handle audio responses."""
228+
nonlocal stream_sid
229+
try:
230+
async for openai_message in client.ws:
231+
response = json.loads(openai_message)
232+
233+
# Log received event
234+
if response.get("type"):
235+
print(f"Received event: {response['type']}", response)
236+
237+
# Process audio responses
238+
if response.get("type") == "response.audio.delta" and "delta" in response:
239+
# Decode and prepare the audio payload
240+
audio_payload = base64.b64encode(
241+
base64.b64decode(response["delta"])
242+
).decode("utf-8")
243+
audio_delta = {
244+
"event": "media",
245+
"streamSid": stream_sid,
246+
"media": {"payload": audio_payload},
247+
}
248+
# Send the audio payload back to Twilio
249+
await websocket.send_json(audio_delta)
250+
251+
# Handle other response types as needed
252+
if response.get("type") == "response.done":
253+
print(f"Response completed: {response}")
254+
255+
except Exception as e:
256+
print(f"Error in send_to_twilio: {e}")
221257

222258
await asyncio.gather(receive_from_twilio(), send_to_twilio())
223259

0 commit comments

Comments
 (0)