From 82b735f5dfe294688e00790fd716f95bd7253c50 Mon Sep 17 00:00:00 2001 From: jknapp Date: Thu, 18 Sep 2025 18:47:24 -0700 Subject: [PATCH] Fix phone number speech formatting for agent announcements MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Updated the format_phone_number_for_speech function to properly read phone numbers digit by digit instead of as large numbers. Changes: - Phone number 9095737372 now reads as "9 0 9, 5 7 3, 7 3 7 2" - Instead of "nine hundred nine, five hundred seventy three" - Added proper digit separation with commas for natural speech flow - Handles both 10-digit and 11-digit (with country code) numbers - Strips all non-numeric characters before processing This makes the agent announcement much clearer when receiving forwarded calls. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- includes/class-twp-webhooks.php | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/includes/class-twp-webhooks.php b/includes/class-twp-webhooks.php index ea085b1..e94b45e 100644 --- a/includes/class-twp-webhooks.php +++ b/includes/class-twp-webhooks.php @@ -2975,17 +2975,32 @@ class TWP_Webhooks { } /** - * Format phone number for speech (adds pauses between digits) + * Format phone number for speech (reads each digit individually) */ private function format_phone_number_for_speech($number) { - // Remove +1 country code and format for speech + // Remove +1 country code and any formatting $cleaned = preg_replace('/^\+1/', '', $number); + $cleaned = preg_replace('/[^0-9]/', '', $cleaned); if (strlen($cleaned) == 10) { - // Format as (xxx) xxx-xxxx with pauses - return substr($cleaned, 0, 3) . ' ' . substr($cleaned, 3, 3) . ' ' . substr($cleaned, 6, 4); + // Break into individual digits with pauses for natural speech + // Area code: 9-0-9, Main number: 5-7-3-7-3-7-2 + $area_code = str_split(substr($cleaned, 0, 3)); + $exchange = str_split(substr($cleaned, 3, 3)); + $number_part = str_split(substr($cleaned, 6, 4)); + + // Join with spaces so TTS reads each digit individually + return implode(' ', $area_code) . ', ' . implode(' ', $exchange) . ', ' . implode(' ', $number_part); + } elseif (strlen($cleaned) == 11 && substr($cleaned, 0, 1) == '1') { + // Handle numbers that still have the 1 prefix + $area_code = str_split(substr($cleaned, 1, 3)); + $exchange = str_split(substr($cleaned, 4, 3)); + $number_part = str_split(substr($cleaned, 7, 4)); + + return implode(' ', $area_code) . ', ' . implode(' ', $exchange) . ', ' . implode(' ', $number_part); } - return $number; + // Fallback: just break into individual digits + return implode(' ', str_split($cleaned)); } } \ No newline at end of file