From e6e177a114da65895511cf0539eb64e75e494a41 Mon Sep 17 00:00:00 2001 From: jknapp Date: Sat, 30 Aug 2025 15:49:31 -0700 Subject: [PATCH] Improve call recording functionality with better debugging and Call SID detection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Recording Issues Fixed - Enhanced Call SID detection for browser phone calls - Try multiple methods to get Call SID: parameters.CallSid, customParameters.CallSid, outgoingConnectionId, sid - Added comprehensive console logging for debugging recording issues - Better error handling and user feedback ## Backend Improvements - Verify call exists and is in-progress before attempting recording - Enhanced error logging with call status and details - Improved AJAX error responses with specific error messages - Database insert error checking and logging ## Frontend Improvements - Multiple Call SID detection methods for browser phone compatibility - Console logging of call object and Call SID for debugging - Enhanced error handling with xhr response details - Validation of active call before recording attempt ## Debugging Features - Detailed error logs in PHP error log - Console logging in browser for JavaScript debugging - Specific error messages for different failure scenarios - Call status validation to ensure recording is possible These changes should resolve the "recording not working" issue by properly detecting the Call SID for browser phone calls and providing detailed debugging information. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- admin/class-twp-admin.php | 59 +++++++++++++++++++++++++---- assets/js/browser-phone-frontend.js | 26 +++++++++++-- 2 files changed, 75 insertions(+), 10 deletions(-) diff --git a/admin/class-twp-admin.php b/admin/class-twp-admin.php index 2c23fbc..173fd2d 100644 --- a/admin/class-twp-admin.php +++ b/admin/class-twp-admin.php @@ -6771,7 +6771,25 @@ class TWP_Admin { } function adminStartRecording() { - var callSid = currentCall.parameters.CallSid || currentCall.customParameters.CallSid; + if (!currentCall) { + showNotice('No active call to record', 'error'); + return; + } + + // Try multiple ways to get the call SID for browser phone calls + var callSid = currentCall.parameters.CallSid || + currentCall.customParameters.CallSid || + currentCall.outgoingConnectionId || + currentCall.sid; + + console.log('Current call object:', currentCall); + console.log('Attempting to record call SID:', callSid); + + if (!callSid) { + showNotice('Could not determine call SID for recording', 'error'); + return; + } + var $recordBtn = $('#admin-record-btn'); $.post(ajaxurl, { @@ -6787,8 +6805,9 @@ class TWP_Admin { } else { showNotice('Failed to start recording: ' + (response.data || 'Unknown error'), 'error'); } - }).fail(function() { - showNotice('Failed to start recording', 'error'); + }).fail(function(xhr, status, error) { + console.error('Recording start failed:', xhr.responseText); + showNotice('Failed to start recording: ' + error, 'error'); }); } @@ -7034,10 +7053,32 @@ class TWP_Admin { $call_sid = sanitize_text_field($_POST['call_sid']); $user_id = get_current_user_id(); + if (empty($call_sid)) { + wp_send_json_error('Call SID is required for recording'); + return; + } + + error_log("TWP: Starting recording for call SID: $call_sid"); + try { $twilio = new TWP_Twilio_API(); $client = $twilio->get_client(); + // First, verify the call exists and is in progress + try { + $call = $client->calls($call_sid)->fetch(); + error_log("TWP: Call found - Status: {$call->status}, From: {$call->from}, To: {$call->to}"); + + if (!in_array($call->status, ['in-progress', 'ringing'])) { + wp_send_json_error("Cannot record call in status: {$call->status}. Call must be in-progress."); + return; + } + } catch (Exception $call_error) { + error_log("TWP: Error fetching call details: " . $call_error->getMessage()); + wp_send_json_error("Call not found or not accessible: " . $call_error->getMessage()); + return; + } + // Start recording the call $recording = $client->calls($call_sid)->recordings->create([ 'recordingStatusCallback' => home_url('/wp-json/twilio-webhook/v1/recording-status'), @@ -7045,14 +7086,13 @@ class TWP_Admin { 'recordingChannels' => 'dual' ]); + error_log("TWP: Recording created with SID: {$recording->sid}"); + // Store recording info in database global $wpdb; $recordings_table = $wpdb->prefix . 'twp_call_recordings'; - // Get call details - $call = $client->calls($call_sid)->fetch(); - - $wpdb->insert($recordings_table, [ + $insert_result = $wpdb->insert($recordings_table, [ 'call_sid' => $call_sid, 'recording_sid' => $recording->sid, 'from_number' => $call->from, @@ -7062,11 +7102,16 @@ class TWP_Admin { 'started_at' => current_time('mysql') ]); + if ($insert_result === false) { + error_log("TWP: Database insert failed: " . $wpdb->last_error); + } + wp_send_json_success([ 'message' => 'Recording started', 'recording_sid' => $recording->sid ]); } catch (Exception $e) { + error_log("TWP: Recording start error: " . $e->getMessage()); wp_send_json_error('Failed to start recording: ' . $e->getMessage()); } } diff --git a/assets/js/browser-phone-frontend.js b/assets/js/browser-phone-frontend.js index 187ef6d..19b40fd 100644 --- a/assets/js/browser-phone-frontend.js +++ b/assets/js/browser-phone-frontend.js @@ -1668,6 +1668,25 @@ * Start recording the current call */ function startRecording() { + if (!currentCall || currentCall.status() !== 'open') { + showMessage('No active call to record', 'error'); + return; + } + + // Try multiple ways to get the call SID + const callSid = currentCall.parameters.CallSid || + currentCall.customParameters.CallSid || + currentCall.outgoingConnectionId || + currentCall.sid; + + console.log('Frontend currentCall object:', currentCall); + console.log('Frontend attempting to record call SID:', callSid); + + if (!callSid) { + showMessage('Could not determine call SID for recording', 'error'); + return; + } + const $recordBtn = $('#twp-record-btn'); $.ajax({ @@ -1675,7 +1694,7 @@ method: 'POST', data: { action: 'twp_start_recording', - call_sid: currentCall.parameters.CallSid, + call_sid: callSid, nonce: twp_frontend_ajax.nonce }, success: function(response) { @@ -1688,8 +1707,9 @@ showMessage('Failed to start recording: ' + (response.data || 'Unknown error'), 'error'); } }, - error: function() { - showMessage('Failed to start recording', 'error'); + error: function(xhr, status, error) { + console.error('Frontend recording start failed:', xhr.responseText); + showMessage('Failed to start recording: ' + error, 'error'); } }); }