-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
726 lines (592 loc) · 27.9 KB
/
app.py
File metadata and controls
726 lines (592 loc) · 27.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
import asyncio
import base64
import json
import requests
import os
import shutil
import datetime
from typing import Dict, Optional, List
from contextlib import asynccontextmanager
from fastapi import FastAPI, WebSocket, WebSocketDisconnect, Request, Depends, HTTPException, status, BackgroundTasks
from fastapi.responses import Response, HTMLResponse, FileResponse, JSONResponse
from pydantic import BaseModel
import secrets
from twilio.twiml.voice_response import VoiceResponse, Connect
import utils
from dotenv import load_dotenv
load_dotenv()
import services
# Configuration
USER_DATA_DIR = "user_data"
CALLS_DIR = os.path.join(USER_DATA_DIR, "calls")
RECORDING_FILENAME = "recording.wav"
# Endpoints
CALLS_ENDPOINT = "calls"
TWILIO_ENDPOINT = "twilio"
CURRENT_CALL_ENDPOINT = "current"
QUEUE_ENDPOINT = "queue"
# --- FastAPI App & Lifespan ---
async def verify_public_url(public_url: str):
if not public_url:
return
try:
url = f"{public_url.rstrip('/')}/verify"
headers = {}
if utils.args.enable_auth:
username = os.environ.get("HTTP_USERNAME") or ""
password = os.environ.get("HTTP_PASSWORD") or ""
credentials = f"{username}:{password}"
encoded_credentials = base64.b64encode(credentials.encode("utf-8")).decode("utf-8")
headers["Authorization"] = f"Basic {encoded_credentials}"
response = await asyncio.to_thread(requests.get, url, headers=headers, timeout=10)
if response.status_code == 200:
data = response.json()
if data.get("status") == "success":
print(f"Public URL verified successfully.")
else:
print(f"Warning: Public URL verification received unexpected JSON: {data}")
else:
print(f"Warning: Public URL verification failed with status {response.status_code}: {response.text}")
except Exception as e:
print(f"Warning: Public URL verification failed: {e}")
@asynccontextmanager
async def lifespan(app: FastAPI):
if utils.args.server_public_url:
asyncio.create_task(verify_public_url(utils.args.server_public_url))
yield
for task in scheduled_calls.values():
task.cancel()
app = FastAPI(lifespan=lifespan)
@app.get("/verify")
async def verify_endpoint():
return {"status": "success"}
# Authentication
@app.middleware("http")
async def verify_password_middleware(request: Request, call_next):
# Protect endpoints starting with /calls and the /verify endpoint
protected_paths = [f"/{CALLS_ENDPOINT}", "/verify"]
if utils.args.enable_auth and any(request.url.path.startswith(path) for path in protected_paths):
auth_header = request.headers.get("Authorization")
is_authorized = False
if auth_header and auth_header.startswith("Basic "):
try:
# Decode the base64 credentials
decoded_credentials = base64.b64decode(auth_header[6:]).decode("utf-8")
username, password = decoded_credentials.split(":", 1)
# Check against environment variables
if secrets.compare_digest(username, os.environ.get("HTTP_USERNAME") or "") and secrets.compare_digest(password, os.environ.get("HTTP_PASSWORD") or ""):
is_authorized = True
except Exception:
pass
if not is_authorized:
return JSONResponse(
status_code=status.HTTP_401_UNAUTHORIZED,
headers={"WWW-Authenticate": "Basic"},
content={"detail": "Incorrect username or password"}
)
response = await call_next(request)
return response
# Group 1: Queue Management
scheduled_calls: Dict[int, asyncio.Task] = {}
scheduled_calls_data: Dict[int, Dict] = {}
next_queue_id = 1
async def schedule_call(queue_id: int, delay_seconds: float, call_data: dict):
try:
print(f"Call {queue_id} scheduled in {delay_seconds} seconds.")
await asyncio.sleep(delay_seconds)
print(f"Executing queued call {queue_id}")
if utils.args.server_public_url:
sid = services.twilio.make_outbound_call(utils.args.server_public_url, call_data["to_number"])
if sid:
full_prompt = services.gemini.GEMINI_PROMPTS["outbound_init"] + call_data["prompt"]
call_data_store[sid] = {"prompt": full_prompt}
print(f"Initiated queued call {queue_id}, SID: {sid}")
else:
print(f"Failed to initiate queued call {queue_id}")
else:
print("Server public URL not set, skipping queued call execution.")
except asyncio.CancelledError:
print(f"Scheduled call {queue_id} cancelled.")
finally:
if queue_id in scheduled_calls:
del scheduled_calls[queue_id]
if queue_id in scheduled_calls_data:
del scheduled_calls_data[queue_id]
# List all queued calls IDs
@app.get(f"/{CALLS_ENDPOINT}/{QUEUE_ENDPOINT}")
async def get_queue_ids():
return list(scheduled_calls_data.keys())
# Retrieve details of a specific queued call
@app.get(f"/{CALLS_ENDPOINT}/{QUEUE_ENDPOINT}/{{queue_id}}")
async def get_queue_item(queue_id: int):
item = scheduled_calls_data.get(queue_id)
if item:
return item
return Response(content=json.dumps({"error": "Item not found"}), status_code=404, media_type="application/json")
# Remove a call from the queue
@app.delete(f"/{CALLS_ENDPOINT}/{QUEUE_ENDPOINT}/{{queue_id}}")
async def delete_queue_item(queue_id: int):
if queue_id in scheduled_calls:
scheduled_calls[queue_id].cancel()
if queue_id in scheduled_calls_data:
del scheduled_calls_data[queue_id]
if queue_id in scheduled_calls:
del scheduled_calls[queue_id]
return {"status": "deleted", "id": queue_id}
return Response(content=json.dumps({"error": "Item not found"}), status_code=404, media_type="application/json")
# Group 2: Outbound Call Management
call_data_store: Dict[str, Dict] = {}
class CallRequest(BaseModel):
to_number: str
prompt: str
datetime: Optional[str] = None
async def handle_scheduled_call(call_request: CallRequest):
global next_queue_id, scheduled_calls, scheduled_calls_data
try:
scheduled_time = datetime.datetime.strptime(call_request.datetime, "%Y-%m-%d-%H-%M")
now = datetime.datetime.now()
delay = (scheduled_time - now).total_seconds()
if delay < 0:
return Response(content=json.dumps({"error": "Scheduled time is in the past"}), status_code=400, media_type="application/json")
q_id = next_queue_id
next_queue_id += 1
call_data = {
"to_number": call_request.to_number,
"prompt": call_request.prompt,
"datetime": call_request.datetime
}
task = asyncio.create_task(schedule_call(q_id, delay, call_data))
scheduled_calls[q_id] = task
scheduled_calls_data[q_id] = call_data
return {"status": "queued", "queue_id": q_id}
except ValueError:
return Response(content=json.dumps({"error": "Invalid datetime format. Use YYYY-MM-DD-HH-MM"}), status_code=400, media_type="application/json")
async def handle_immediate_call(call_request: CallRequest):
global call_data_store
public_url = utils.args.server_public_url
print("Using Public URL for callback: ", public_url)
call_sid = services.twilio.make_outbound_call(public_url, call_request.to_number)
if call_sid:
call_data_store[call_sid] = {"prompt": services.gemini.GEMINI_PROMPTS["outbound_init"] + call_request.prompt}
return {"status": "success", "call_sid": call_sid}
else:
return Response(content=json.dumps({"status": "error", "message": "Failed to initiate call."}), status_code=500, media_type="application/json")
# Initiate a new outbound call
@app.post(f"/{CALLS_ENDPOINT}/make")
async def make_outbound_call_handler(request: Request, call_request: CallRequest):
if call_request.datetime:
return await handle_scheduled_call(call_request)
return await handle_immediate_call(call_request)
# Group 3: Active Call Control
current_call_sid: Optional[str] = None
active_cm: Optional[services.gemini.GeminiConversationManager] = None
# Helper to get active call sid
def get_current_call_sid():
global current_call_sid
return current_call_sid
async def send_prompt_to_call(call_sid: str, payload: dict):
prompt = payload.get("prompt")
if not prompt:
return Response(content=json.dumps({"error": "Prompt is required"}), status_code=400, media_type="application/json")
global active_cm
if active_cm and current_call_sid == call_sid:
await active_cm.send_text_prompt(prompt)
return {"status": "sent", "prompt": prompt}
return Response(content=json.dumps({"error": "Active call not found"}), status_code=404, media_type="application/json")
# Get current call info
@app.get(f"/{CALLS_ENDPOINT}/{CURRENT_CALL_ENDPOINT}/info")
async def get_current_call_info():
global active_cm
sid = get_current_call_sid()
return {
"active": active_cm is not None,
"call_sid": sid
}
# Serve the audio-only preview page for the currently active call
@app.get(f"/{CALLS_ENDPOINT}/{CURRENT_CALL_ENDPOINT}/audio")
async def audio_preview_current_call(request: Request):
call_sid = get_current_call_sid()
if not call_sid:
return HTMLResponse("<h1>No active call to preview</h1>")
scheme = "wss" if request.url.scheme == "https" else "ws"
ws_url = f"{scheme}://{request.url.netloc}/{CALLS_ENDPOINT}/{CURRENT_CALL_ENDPOINT}/ws"
html_content = f"""
<html>
<head>
<title>Audio Preview: {call_sid}</title>
<style>
body {{ font-family: sans-serif; padding: 20px; display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh; background: #f0f0f0; margin: 0; }}
.container {{ background: white; padding: 40px; border-radius: 10px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); text-align: center; width: 300px; }}
.status {{ margin-bottom: 20px; font-weight: bold; color: #333; }}
button {{
padding: 15px 30px;
font-size: 18px;
cursor: pointer;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
transition: background-color 0.3s;
width: 100%;
}}
button:hover {{ background-color: #0056b3; }}
.active {{ color: green; }}
.inactive {{ color: red; }}
</style>
</head>
<body>
<div class="container">
<h2>Live Audio</h2>
<div class="status">
Status: <span id="status-text" class="{'active' if call_sid == current_call_sid else 'inactive'}">{'Active' if call_sid == current_call_sid else 'Inactive'}</span>
</div>
<button onclick="toggleAudio()" id="audio-btn">Enable Audio</button>
</div>
<script>
const wsUrl = "{ws_url}";
let ws;
let audioCtx;
let nextStartTime = 0;
function initAudio() {{
if (!audioCtx) {{
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
}}
if (audioCtx.state === 'suspended') {{
audioCtx.resume();
}}
}}
function toggleAudio() {{
initAudio();
const btn = document.getElementById('audio-btn');
if (btn.innerText === "Enable Audio") {{
btn.innerText = "Audio Enabled";
btn.style.backgroundColor = "#28a745";
}}
}}
function playPcm(base64Data) {{
if (!audioCtx) return;
const binaryString = window.atob(base64Data);
const len = binaryString.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {{
bytes[i] = binaryString.charCodeAt(i);
}}
const int16 = new Int16Array(bytes.buffer);
const float32 = new Float32Array(int16.length);
for (let i = 0; i < int16.length; i++) {{
float32[i] = int16[i] / 32768.0;
}}
const buffer = audioCtx.createBuffer(1, float32.length, 8000);
buffer.copyToChannel(float32, 0);
const source = audioCtx.createBufferSource();
source.buffer = buffer;
source.connect(audioCtx.destination);
const BUFFER_DELAY = 0.15;
if (nextStartTime < audioCtx.currentTime) {{
nextStartTime = audioCtx.currentTime + BUFFER_DELAY;
}}
source.start(nextStartTime);
nextStartTime += buffer.duration;
}}
function connect() {{
ws = new WebSocket(wsUrl);
const statusText = document.getElementById('status-text');
ws.onopen = function() {{
statusText.innerText = "Connected (Active)";
statusText.className = "active";
}};
ws.onmessage = function(event) {{
const data = JSON.parse(event.data);
if (data.type === 'audio') {{
playPcm(data.data);
}}
}};
ws.onclose = function() {{
statusText.innerText = "Disconnected";
statusText.className = "inactive";
}};
}}
connect();
// Autoplay audio
window.addEventListener('load', () => {{
initAudio();
const btn = document.getElementById('audio-btn');
if (btn) {{
btn.innerText = "Audio Enabled";
btn.style.backgroundColor = "#28a745";
}}
// Attempt to resume immediately
if (audioCtx) {{
audioCtx.resume().catch(e => console.log("Autoplay blocked:", e));
}}
}});
</script>
</body>
</html>
"""
return HTMLResponse(content=html_content)
# WebSocket endpoint for streaming call audio and logs
@app.websocket(f"/{CALLS_ENDPOINT}/{CURRENT_CALL_ENDPOINT}/ws")
async def preview_websocket(websocket: WebSocket):
global active_cm
if not active_cm:
await websocket.close(code=1000, reason="Call not active")
return
await websocket.accept()
await active_cm.add_observer(websocket)
try:
while True:
receive_task = asyncio.create_task(websocket.receive_text())
call_ended_task = asyncio.create_task(active_cm.call_ended_event.wait())
done, pending = await asyncio.wait(
[receive_task, call_ended_task],
return_when=asyncio.FIRST_COMPLETED
)
if call_ended_task in done:
receive_task.cancel()
print("Call ended, closing preview websocket")
await websocket.close(code=1000, reason="Call ended")
break
if receive_task in done:
try:
message = receive_task.result()
print(f"Received prompt via websocket: {message}")
await active_cm.send_text_prompt(message)
except Exception as e:
print(f"Error processing message: {e}")
except WebSocketDisconnect:
pass
except Exception as e:
print(f"Preview websocket error: {e}")
finally:
if active_cm:
active_cm.remove_observer(websocket)
# Group 4: Twilio Integration (Webhooks & Streams)
# Twilio webhook for call status updates
@app.post(f"/{TWILIO_ENDPOINT}/status_callback")
async def status_callback_handler(request: Request):
global current_call_sid, active_cm
form_data = await request.form()
call_sid = form_data.get("CallSid")
call_status = form_data.get("CallStatus")
answered_by = form_data.get("AnsweredBy")
print(f"Call {call_sid} status: {call_status}, AnsweredBy: {answered_by}")
if call_status in ["busy", "no-answer", "failed", "canceled"] or (answered_by and answered_by.startswith("machine")):
if call_sid in call_data_store:
del call_data_store[call_sid]
if call_sid == current_call_sid:
current_call_sid = None
active_cm = None
try:
call_dict = services.twilio.get_twilio_call_data(call_sid)
process_call(call_sid, call_dict, "None", [])
except Exception as e:
print(f"Error in status callback processing: {e}")
else:
print(f"Call {call_sid} status '{call_status}' is not considered a failure. Webhook not sent.")
return Response(content="OK", status_code=200)
# Twilio webhook for incoming voice calls
@app.post(f"/{TWILIO_ENDPOINT}/voice")
async def voice_handler(request: Request):
response = VoiceResponse()
form_data = await request.form()
answered_by = form_data.get("AnsweredBy")
forwarded_from = form_data.get("ForwardedFrom")
print(f"Incoming call answered by: {answered_by}, ForwardedFrom: {forwarded_from}")
if answered_by and answered_by.startswith("machine"):
print("Answering machine detected. Hanging up.")
response.hangup()
return Response(content=str(response), media_type="application/xml")
if forwarded_from:
print(f"Call forwarded from {forwarded_from}. Rejecting potential voicemail loop.")
response.hangup()
return Response(content=str(response), media_type="application/xml")
connect = Connect()
websocket_url = f"wss://{request.headers['host']}/{TWILIO_ENDPOINT}/ws"
connect.stream(url=websocket_url)
response.append(connect)
return Response(content=str(response), media_type="application/xml")
# Main WebSocket handler for Twilio Media Streams
@app.websocket(f"/{TWILIO_ENDPOINT}/ws")
async def websocket_handler(websocket: WebSocket):
await websocket.accept()
print("WebSocket connection established.")
global current_call_sid, active_cm
call_state = {"stream_sid": None}
conversation_manager = None
gemini_task = None
call_sid = None
try:
while True:
receive_task = asyncio.create_task(websocket.receive_text())
wait_tasks = [receive_task]
if gemini_task:
wait_tasks.append(gemini_task)
done, pending = await asyncio.wait(wait_tasks, return_when=asyncio.FIRST_COMPLETED)
if gemini_task and gemini_task in done:
print("Gemini assistant ended the conversation.")
if not receive_task.done():
receive_task.cancel()
break
try:
message = receive_task.result()
except WebSocketDisconnect:
print("WebSocket disconnected.")
break
except asyncio.CancelledError:
break
data = json.loads(message)
if data['event'] == 'start':
print(data)
call_state['stream_sid'] = data['start']['streamSid']
call_sid = data['start']['callSid']
os.makedirs(os.path.join(CALLS_DIR, call_sid), exist_ok=True)
recording_path = os.path.join(CALLS_DIR, call_sid, RECORDING_FILENAME)
print(f"Twilio stream started: {call_state['stream_sid']}")
if call_sid in call_data_store:
prompt = call_data_store[call_sid]["prompt"]
print(f"Using custom prompt for call {call_sid}")
del call_data_store[call_sid]
else:
prompt = services.gemini.GEMINI_PROMPTS["inbound_init"]
print(f"Using default inbound prompt for call {call_sid}")
if active_cm is not None:
print(f"Rejecting new call {call_sid} because {current_call_sid} is active")
pass
current_call_sid = call_sid
conversation_manager = services.gemini.GeminiConversationManager(websocket, call_state, prompt)
active_cm = conversation_manager
gemini_task = asyncio.create_task(conversation_manager.run())
if not utils.args.no_recording:
conversation_manager.start_recording(recording_path)
conversation_manager.initial_prompt_sent.set()
# Send Webhook that call started
if utils.args.notify:
try:
call_info = services.twilio.get_twilio_call_data(call_sid)
# We use fire-and-forget logic or just await with short timeout
requests.post(utils.args.notify, json=call_info, timeout=2)
print(f"Sent start webhook for {call_sid}")
except Exception as e:
print(f"Failed to send start webhook: {e}")
elif data['event'] == 'media':
if not conversation_manager:
continue
mulaw_audio = base64.b64decode(data['media']['payload'])
# Convert mulaw to 16-bit PCM for Gemini
pcm_audio = services.gemini.AudioUtils.ulaw2lin(mulaw_audio, 2)
if conversation_manager and conversation_manager.recorder:
conversation_manager.recorder.writeframes(pcm_audio)
if conversation_manager:
await conversation_manager.input_queue.put(('audio', pcm_audio))
# Broadcast audio for preview (we use the same PCM chunks)
await conversation_manager.broadcast_audio(pcm_audio)
elif data['event'] == 'stop':
print("Twilio stream stopped.")
if conversation_manager:
await conversation_manager.input_queue.put(None)
break
if gemini_task:
await gemini_task
except WebSocketDisconnect:
print("WebSocket disconnected.")
except Exception as e:
print(f"An error occurred: {e}")
finally:
if call_sid and call_sid == current_call_sid:
current_call_sid = None
active_cm = None
if gemini_task and not gemini_task.done():
gemini_task.cancel()
print("Closing WebSocket connection.")
if conversation_manager and conversation_manager.recorder:
conversation_manager.recorder.close()
print("Recording saved.")
if websocket.client_state != 3:
await websocket.close()
if call_sid:
try:
call_dict = services.twilio.get_twilio_call_data(call_sid)
call_dir = os.path.join(CALLS_DIR, call_sid)
os.makedirs(call_dir, exist_ok=True)
answered_by = call_dict.get('answered_by')
if answered_by and answered_by.startswith("machine"):
print(f"Call answered by {answered_by}. Skipping success webhook and deleting recording.")
if os.path.exists(call_dir):
shutil.rmtree(call_dir)
print(f"Deleted call directory: {call_dir}")
else:
recording_path = os.path.join(call_dir, RECORDING_FILENAME)
transcription_history = []
if conversation_manager:
transcription_history = conversation_manager.transcription_history
process_call(call_sid, call_dict, transcription_history)
except Exception as e:
print(f"Error in post-call processing: {e}")
# Group 5: Call Management
def process_call(call_sid: str, call_dict: dict, transcription: list = []):
try:
call_dir = os.path.join(CALLS_DIR, call_sid)
os.makedirs(call_dir, exist_ok=True)
twilio_call_data_path = os.path.join(call_dir, "twilio_call.json")
with open(twilio_call_data_path, 'w', encoding='utf-8') as f:
json.dump(call_dict, f, default=utils.json_datetime_serializer, ensure_ascii=False)
transcript_path = os.path.join(call_dir, "transcript.json")
with open(transcript_path, 'w', encoding='utf-8') as f:
json.dump(transcription, f, default=utils.json_datetime_serializer, ensure_ascii=False)
utils.cleanup_calls(CALLS_DIR, utils.args.keep_calls)
except Exception as e:
print(f"Error in process_call for {call_sid}: {e}")
# List all historical calls
@app.get(f"/{CALLS_ENDPOINT}")
async def get_calls_list():
if not os.path.exists(CALLS_DIR):
return []
calls = [d for d in os.listdir(CALLS_DIR) if os.path.isdir(os.path.join(CALLS_DIR, d))]
return calls
# Retrieve a list of files for a specific call
@app.get(f"/{CALLS_ENDPOINT}/{{call_sid}}")
async def get_call_files(call_sid: str):
call_dir = os.path.join(CALLS_DIR, call_sid)
if os.path.exists(call_dir) and os.path.isdir(call_dir):
return os.listdir(call_dir)
return Response(content=json.dumps({"error": "Call not found"}), status_code=404, media_type="application/json")
# Retrieve a specific file (audio or JSON) for a call
@app.get(f"/{CALLS_ENDPOINT}/{{call_sid}}/{{filename}}")
async def get_call_data(call_sid: str, filename: str, raw: bool = False):
"""Serves the call data for a specific call."""
file_path = os.path.join(CALLS_DIR, call_sid, filename)
if os.path.exists(file_path):
# Handle audio preview
if filename == RECORDING_FILENAME and not raw:
html_content = f"""
<html>
<head>
<title>Audio Preview</title>
<style>
body {{ display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background: #1a1a1a; color: white; font-family: system-ui, sans-serif; }}
audio {{ width: 80%; max-width: 800px; }}
</style>
</head>
<body>
<audio controls autoplay>
<source src="{filename}?raw=true" type="audio/wav">
Your browser does not support the audio element.
</audio>
</body>
</html>
"""
return HTMLResponse(content=html_content)
return FileResponse(file_path)
return Response(content="File not found", status_code=404)
# Delete a call recording and metadata
@app.delete(f"/{CALLS_ENDPOINT}/{{call_sid}}")
async def delete_call(call_sid: str):
call_dir = os.path.join(CALLS_DIR, call_sid)
if os.path.exists(call_dir):
try:
shutil.rmtree(call_dir)
return {"status": "deleted", "call_sid": call_sid}
except Exception as e:
return Response(content=json.dumps({"error": f"Failed to delete: {e}"}), status_code=500, media_type="application/json")
return Response(content=json.dumps({"error": "Call not found"}), status_code=404, media_type="application/json")