-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook_server.py
More file actions
215 lines (184 loc) · 8.21 KB
/
webhook_server.py
File metadata and controls
215 lines (184 loc) · 8.21 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
from flask import Flask, request, jsonify
import json
import os
import subprocess
import threading
from datetime import datetime
app = Flask(__name__)
def run_orchestrator(phone_number=None):
"""Run the orchestrator in a separate thread
Args:
phone_number: Optional phone number to process specific user's webhook
"""
try:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
phone_suffix = f"_{phone_number}" if phone_number else ""
log_file = f"logs/pipeline{phone_suffix}_{timestamp}.log"
os.makedirs("logs", exist_ok=True)
print(f"[{timestamp}] Starting Voice Vibe Coding pipeline for {phone_number or 'test'}...")
print(f"[{timestamp}] Logging to: {log_file}")
# Build command with optional phone parameter
cmd = ["python", "orchestrator.py"]
if phone_number:
cmd.extend(["--phone", phone_number])
# Run orchestrator and capture output
with open(log_file, "w") as log:
result = subprocess.run(
cmd,
capture_output=False,
stdout=log,
stderr=subprocess.STDOUT,
text=True
)
if result.returncode == 0:
print(f"[{timestamp}] Pipeline completed successfully for {phone_number or 'test'}!")
else:
print(f"[{timestamp}] Pipeline failed with code: {result.returncode}")
except Exception as e:
print(f"[{timestamp}] Error running orchestrator: {e}")
@app.route('/elevenlabs-webhook', methods=['POST'])
def handle_webhook():
"""Post-call webhook - receives transcript after call ends"""
data = request.json
print("Received POST from ElevenLabs")
# Extract phone number from the webhook data
# ElevenLabs stores it in conversation_initiation_client_data.dynamic_variables.system__caller_id
phone = None
# Primary extraction path (ElevenLabs standard format)
if "data" in data and "conversation_initiation_client_data" in data["data"]:
dynamic_vars = data["data"]["conversation_initiation_client_data"].get("dynamic_variables", {})
phone = dynamic_vars.get("system__caller_id", None)
if phone:
print(f"[PHONE EXTRACTION] Found phone in dynamic_variables.system__caller_id: {phone}")
# Fallback extraction paths (for different webhook formats)
if not phone:
# Try alternate locations
if 'call' in data and 'customer_number' in data['call']:
phone = data['call']['customer_number']
print(f"[PHONE EXTRACTION] Found phone in call.customer_number: {phone}")
elif 'customer_number' in data:
phone = data['customer_number']
print(f"[PHONE EXTRACTION] Found phone in customer_number: {phone}")
elif 'phone' in data:
phone = data['phone']
print(f"[PHONE EXTRACTION] Found phone in phone field: {phone}")
elif 'from' in data:
phone = data['from']
print(f"[PHONE EXTRACTION] Found phone in from field: {phone}")
# Clean phone number (remove +, -, spaces, parentheses, etc.)
if phone:
original_phone = phone
phone = ''.join(filter(str.isdigit, str(phone)))
print(f"[PHONE EXTRACTION] Cleaned phone: '{original_phone}' -> '{phone}'")
print(f"Processing webhook for phone: {phone}")
else:
print("[PHONE EXTRACTION] WARNING: No phone number found in webhook!")
print("[PHONE EXTRACTION] Webhook structure keys:", list(data.keys()) if isinstance(data, dict) else "Not a dict")
if isinstance(data, dict) and "data" in data:
print("[PHONE EXTRACTION] data keys:", list(data["data"].keys()) if isinstance(data["data"], dict) else "Not a dict")
print("Warning: No phone number found in webhook, using legacy path only")
# Save to user-specific directory if phone is available (atomic write)
if phone:
user_dir = f"projects/{phone}"
os.makedirs(user_dir, exist_ok=True)
webhook_path = f"{user_dir}/latest_webhook.json"
temp_path = f"{webhook_path}.tmp.{os.getpid()}"
try:
# Write to temp file first (atomic operation)
with open(temp_path, "w") as f:
json.dump(data, f, indent=2)
# Atomically rename to final location
os.replace(temp_path, webhook_path)
print(f"[FILE WRITE] Successfully saved webhook to: {webhook_path}")
except Exception as e:
print(f"[FILE WRITE] ERROR saving user webhook: {e}")
# Clean up temp file if it exists
if os.path.exists(temp_path):
try:
os.remove(temp_path)
except:
pass
# Also save to legacy location for backwards compatibility (atomic write)
os.makedirs("webhooks", exist_ok=True)
legacy_path = "webhooks/latest_raw.json"
legacy_temp = f"{legacy_path}.tmp.{os.getpid()}"
try:
with open(legacy_temp, "w") as f:
json.dump(data, f, indent=2)
os.replace(legacy_temp, legacy_path)
print(f"[FILE WRITE] Successfully saved legacy webhook to: {legacy_path}")
except Exception as e:
print(f"[FILE WRITE] ERROR saving legacy webhook: {e}")
if os.path.exists(legacy_temp):
try:
os.remove(legacy_temp)
except:
pass
# Automatically trigger the orchestrator in background with phone number
print(f"Triggering Voice Vibe Coding pipeline for {phone or 'unknown'}...")
thread = threading.Thread(target=run_orchestrator, args=(phone,))
thread.daemon = True # Don't wait for thread to complete
thread.start()
return jsonify({"status": "received", "pipeline": "started", "phone": phone}), 200
@app.route('/precall', methods=['POST'])
def precall_context():
"""Pre-call webhook - provides context BEFORE conversation starts"""
caller_id = request.json.get('caller_id', '').replace('+', '').replace('-', '')
context_file = f"projects/{caller_id}/VOICE_CONTEXT.md"
print(f"Pre-call webhook for caller: {caller_id}")
if os.path.exists(context_file):
with open(context_file) as f:
content = f.read()
print(f"Found voice context for {caller_id}")
else:
content = "No projects yet."
print(f"No voice context found for {caller_id}")
return jsonify({
"dynamic_variables": {
"projects_context": content
}
})
@app.route('/health', methods=['GET'])
def health_check():
"""Health check endpoint"""
return jsonify({"status": "healthy"}), 200
@app.route('/status', methods=['GET'])
def pipeline_status():
"""Check pipeline status and recent logs"""
logs_dir = "logs"
recent_logs = []
if os.path.exists(logs_dir):
# Get the 5 most recent log files
log_files = sorted(
[f for f in os.listdir(logs_dir) if f.startswith("pipeline_")],
reverse=True
)[:5]
for log_file in log_files:
log_path = os.path.join(logs_dir, log_file)
try:
# Get file size and last few lines
size = os.path.getsize(log_path)
with open(log_path, 'r') as f:
lines = f.readlines()
last_lines = lines[-10:] if len(lines) > 10 else lines
recent_logs.append({
"file": log_file,
"size_bytes": size,
"last_lines": "".join(last_lines)[-500:] # Last 500 chars
})
except Exception as e:
recent_logs.append({
"file": log_file,
"error": str(e)
})
return jsonify({
"status": "running",
"webhook_endpoint": "/elevenlabs-webhook",
"recent_pipelines": recent_logs
}), 200
if __name__ == '__main__':
print("🚀 Voice Vibe Coding Webhook Server")
print("📞 Webhook URL: http://localhost:5001/elevenlabs-webhook")
print("🔍 Status URL: http://localhost:5001/status")
print("=" * 50)
app.run(host='0.0.0.0', port=5001)