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:
		@@ -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, [
 | 
			
		||||
 
 | 
			
		||||
@@ -1203,15 +1203,34 @@ class TWP_Webhooks {
 | 
			
		||||
        $table_name = $wpdb->prefix . 'twp_call_recordings';
 | 
			
		||||
        
 | 
			
		||||
        $recording = $wpdb->get_row($wpdb->prepare(
 | 
			
		||||
            "SELECT recording_url FROM $table_name WHERE id = %d",
 | 
			
		||||
            "SELECT recording_url, recording_sid FROM $table_name WHERE id = %d",
 | 
			
		||||
            $recording_id
 | 
			
		||||
        ));
 | 
			
		||||
        
 | 
			
		||||
        if (!$recording || !$recording->recording_url) {
 | 
			
		||||
        error_log("TWP Recording Proxy: Looking for recording ID: $recording_id");
 | 
			
		||||
        
 | 
			
		||||
        if (!$recording) {
 | 
			
		||||
            error_log("TWP Recording Proxy: No recording found in database for ID: $recording_id");
 | 
			
		||||
            header('HTTP/1.0 404 Not Found');
 | 
			
		||||
            exit('Recording not found');
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        if (!$recording->recording_url) {
 | 
			
		||||
            error_log("TWP Recording Proxy: Recording found but no URL. Recording SID: " . ($recording->recording_sid ?: 'none'));
 | 
			
		||||
            
 | 
			
		||||
            // If we have a recording SID but no URL, try to construct the URL
 | 
			
		||||
            if ($recording->recording_sid) {
 | 
			
		||||
                $account_sid = get_option('twp_twilio_account_sid');
 | 
			
		||||
                $recording->recording_url = "https://api.twilio.com/2010-04-01/Accounts/$account_sid/Recordings/{$recording->recording_sid}";
 | 
			
		||||
                error_log("TWP Recording Proxy: Constructed URL from SID: " . $recording->recording_url);
 | 
			
		||||
            } else {
 | 
			
		||||
                header('HTTP/1.0 404 Not Found');
 | 
			
		||||
                exit('Recording URL not available');
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        error_log("TWP Recording Proxy: Recording URL: " . $recording->recording_url);
 | 
			
		||||
        
 | 
			
		||||
        // Fetch the audio from Twilio using authenticated request
 | 
			
		||||
        $twilio = new TWP_Twilio_API();
 | 
			
		||||
        $account_sid = get_option('twp_twilio_account_sid');
 | 
			
		||||
@@ -1232,7 +1251,19 @@ class TWP_Webhooks {
 | 
			
		||||
        ));
 | 
			
		||||
        
 | 
			
		||||
        if (is_wp_error($response)) {
 | 
			
		||||
            return new WP_Error('fetch_error', 'Unable to fetch recording', array('status' => 500));
 | 
			
		||||
            error_log('TWP Recording Proxy: Failed to fetch audio - ' . $response->get_error_message());
 | 
			
		||||
            header('HTTP/1.0 500 Internal Server Error');
 | 
			
		||||
            exit('Unable to fetch recording: ' . $response->get_error_message());
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        // Log the response for debugging
 | 
			
		||||
        $response_code = wp_remote_retrieve_response_code($response);
 | 
			
		||||
        error_log("TWP Recording Proxy: Twilio response code: $response_code for URL: $audio_url");
 | 
			
		||||
        
 | 
			
		||||
        if ($response_code !== 200) {
 | 
			
		||||
            error_log('TWP Recording Proxy: Non-200 response from Twilio: ' . wp_remote_retrieve_body($response));
 | 
			
		||||
            header('HTTP/1.0 404 Not Found');
 | 
			
		||||
            exit('Recording not available from Twilio');
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        $body = wp_remote_retrieve_body($response);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user