Enhance recording debugging and fix outbound call number detection

Recording Proxy Improvements:
- Enhanced error handling to prevent JSON responses instead of audio
- Added comprehensive logging for Twilio API calls
- Added fallback URL construction from recording SID if URL missing
- Improved error responses with proper HTTP headers

Outbound Call Phone Number Detection:
- Enhanced logic to find customer numbers in complex call structures
- Added search through related calls to find customer phone numbers
- Comprehensive logging for debugging outbound call number assignment
- Handles cases where 'to' field might be empty or contain client identifiers

These changes should resolve:
1. Recording playback returning JSON instead of audio files
2. Missing customer phone numbers in outbound call recordings
3. Better error reporting and debugging capabilities

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-08-30 17:26:55 -07:00
parent ae9c6f5e8c
commit 82e591b367
2 changed files with 96 additions and 9 deletions

View File

@@ -7416,17 +7416,73 @@ class TWP_Admin {
global $wpdb;
$recordings_table = $wpdb->prefix . 'twp_call_recordings';
// For outbound calls from browser phone, swap the numbers for better display
// For outbound calls from browser phone, determine the customer number
$from_number = $call->from;
$to_number = $call->to;
// If this is a browser phone call (from contains 'client:'), then the customer is the 'to' number
error_log("TWP Recording: Initial call data - From: {$call->from}, To: {$call->to}, Direction: {$call->direction}");
// If this is a browser phone call (from contains 'client:'), find the customer number
if (strpos($call->from, 'client:') === 0) {
// This is an outbound call from browser phone
// Store the customer number as 'from' for display consistency
$from_number = $call->to; // Customer number
$to_number = $call->from; // Browser phone client
error_log("TWP: Outbound browser call - Customer: {$call->to}, Agent: {$call->from}");
error_log("TWP Recording: Detected browser phone outbound call");
// For browser phone calls, we need to find the customer number
// It might be in 'to' field, or we might need to look at related calls
$customer_number = null;
// First try the 'to' field
if (!empty($call->to) && strpos($call->to, 'client:') === false) {
$customer_number = $call->to;
error_log("TWP Recording: Found customer number in 'to' field: {$customer_number}");
} else {
// If 'to' is empty or also a client, look for related calls
error_log("TWP Recording: 'to' field empty or client, looking for related calls");
try {
$twilio = new TWP_Twilio_API();
$client_api = $twilio->get_client();
// Get all recent calls to find the customer leg
$related_calls = $client_api->calls->read(['status' => 'in-progress'], 20);
foreach ($related_calls as $related_call) {
// Skip the current call
if ($related_call->sid === $call_sid) continue;
// Look for calls with same parent or that are our parent/child
if (($call->parentCallSid && $related_call->parentCallSid === $call->parentCallSid) ||
$related_call->parentCallSid === $call_sid ||
$related_call->sid === $call->parentCallSid) {
// Check if this call has a real phone number (not client)
if (strpos($related_call->from, 'client:') === false &&
strpos($related_call->from, '+') === 0) {
$customer_number = $related_call->from;
error_log("TWP Recording: Found customer number in related call 'from': {$customer_number}");
break;
} elseif (strpos($related_call->to, 'client:') === false &&
strpos($related_call->to, '+') === 0) {
$customer_number = $related_call->to;
error_log("TWP Recording: Found customer number in related call 'to': {$customer_number}");
break;
}
}
}
} catch (Exception $e) {
error_log("TWP Recording: Error looking for related calls: " . $e->getMessage());
}
}
if ($customer_number) {
// Store customer number in 'from' for display purposes
$from_number = $customer_number;
$to_number = $call->from; // Browser phone client
error_log("TWP Recording: Outbound call - Customer: {$customer_number}, Agent: {$call->from}");
} else {
error_log("TWP Recording: WARNING - Could not determine customer number for outbound call");
// Keep original values but log the issue
}
}
$insert_result = $wpdb->insert($recordings_table, [