testing progress

This commit is contained in:
2025-08-12 07:05:47 -07:00
parent 304b5de40b
commit 51dd3077d2
5 changed files with 2698 additions and 0 deletions

View File

@@ -87,6 +87,12 @@ class TWP_Workflow {
$stop_after_step = true; // Queue ends the workflow
break;
case 'browser_call':
error_log('TWP Workflow: Processing browser call step: ' . json_encode($step));
$step_twiml = self::create_browser_call_twiml($step, $elevenlabs);
$stop_after_step = true; // Browser call ends the workflow
break;
case 'ring_group':
$step_twiml = self::create_ring_group_twiml($step);
$stop_after_step = true; // Ring group ends the workflow
@@ -380,6 +386,90 @@ class TWP_Workflow {
return $twiml->asXML();
}
/**
* Create browser call TwiML
*/
private static function create_browser_call_twiml($step, $elevenlabs) {
$step_data = $step['data'];
$twiml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><Response></Response>');
// Get announcement message
$message = '';
if (isset($step_data['announce_message']) && !empty($step_data['announce_message'])) {
$message = $step_data['announce_message'];
} else {
$message = 'Connecting you to our agent.';
}
// Handle audio type for announcement
$audio_type = isset($step_data['audio_type']) ? $step_data['audio_type'] : 'say';
switch ($audio_type) {
case 'tts':
$voice_id = isset($step_data['voice_id']) ? $step_data['voice_id'] : null;
$audio_result = $elevenlabs->text_to_speech($message, [
'voice_id' => $voice_id
]);
if ($audio_result['success']) {
$play = $twiml->addChild('Play', $audio_result['file_url']);
} else {
$say = $twiml->addChild('Say', $message);
$say->addAttribute('voice', 'alice');
}
break;
case 'audio':
$audio_url = isset($step_data['audio_url']) ? $step_data['audio_url'] : null;
if ($audio_url && !empty($audio_url)) {
$play = $twiml->addChild('Play', $audio_url);
} else {
$say = $twiml->addChild('Say', $message);
$say->addAttribute('voice', 'alice');
}
break;
default: // 'say'
$say = $twiml->addChild('Say', $message);
$say->addAttribute('voice', 'alice');
break;
}
// Dial browser clients
$dial = $twiml->addChild('Dial');
$dial->addAttribute('timeout', '30');
// Get browser client names (agents who might be online)
$browser_clients = isset($step_data['browser_clients']) ? $step_data['browser_clients'] : [];
if (empty($browser_clients)) {
// Default: try to call any available browser clients
// Get all users with agent capabilities
$agents = get_users(array(
'meta_key' => 'twp_phone_number',
'meta_compare' => 'EXISTS'
));
foreach ($agents as $agent) {
$client_name = 'agent_' . $agent->ID . '_' . sanitize_title($agent->display_name);
$client = $dial->addChild('Client', $client_name);
}
} else {
// Call specific browser clients
foreach ($browser_clients as $client_name) {
$client = $dial->addChild('Client', $client_name);
}
}
// Add fallback if no browser clients answer
$action_url = home_url('/wp-json/twilio-webhook/v1/browser-fallback');
$dial->addAttribute('action', $action_url);
$dial->addAttribute('method', 'POST');
return $twiml->asXML();
}
/**
* Create forward TwiML
*/
@@ -980,6 +1070,19 @@ class TWP_Workflow {
return $wpdb->get_results("SELECT * FROM $table_name ORDER BY created_at DESC");
}
/**
* Get workflow by phone number
*/
public static function get_workflow_by_phone_number($phone_number) {
global $wpdb;
$table_name = $wpdb->prefix . 'twp_workflows';
return $wpdb->get_row($wpdb->prepare(
"SELECT * FROM $table_name WHERE phone_number = %s AND is_active = 1 ORDER BY created_at DESC LIMIT 1",
$phone_number
));
}
/**
* Update workflow
*/