progress made

This commit is contained in:
2025-08-11 20:31:48 -07:00
parent 805af2f199
commit 304b5de40b
15 changed files with 4028 additions and 404 deletions

View File

@@ -12,7 +12,29 @@ class TWP_Twilio_API {
*/
public function __construct() {
$this->init_sdk_client();
$this->phone_number = get_option('twp_twilio_phone_number');
// Try to get the SMS notification number first, or get the first available Twilio number
$this->phone_number = get_option('twp_sms_notification_number');
// If no SMS number configured, try to get the first phone number from the account
if (empty($this->phone_number)) {
$this->phone_number = $this->get_default_phone_number();
}
}
/**
* Get the default phone number from the account
*/
private function get_default_phone_number() {
try {
// Get the first phone number from the account
$numbers = $this->client->incomingPhoneNumbers->read([], 1);
if (!empty($numbers)) {
return $numbers[0]->phoneNumber;
}
} catch (\Exception $e) {
error_log('TWP: Unable to get default phone number: ' . $e->getMessage());
}
return null;
}
/**
@@ -72,6 +94,10 @@ class TWP_Twilio_API {
if ($status_callback) {
$params['statusCallback'] = $status_callback;
$params['statusCallbackEvent'] = ['initiated', 'ringing', 'answered', 'completed'];
$params['statusCallbackMethod'] = 'POST';
$params['timeout'] = 20; // Ring for 20 seconds before giving up
$params['machineDetection'] = 'Enable'; // Detect if voicemail answers
$params['machineDetectionTimeout'] = 30; // Wait 30 seconds to detect machine
}
$call = $this->client->calls->create(
@@ -238,10 +264,22 @@ class TWP_Twilio_API {
*/
public function send_sms($to_number, $message, $from_number = null) {
try {
// Determine the from number
$from = $from_number ?: $this->phone_number;
// Validate we have a from number
if (empty($from)) {
error_log('TWP SMS Error: No from number available. Please configure SMS notification number in settings.');
return [
'success' => false,
'error' => 'No SMS from number configured. Please set SMS notification number in plugin settings.'
];
}
$sms = $this->client->messages->create(
$to_number,
[
'from' => $from_number ?: $this->phone_number,
'from' => $from,
'body' => $message
]
);
@@ -282,6 +320,7 @@ class TWP_Twilio_API {
'friendly_name' => $number->friendlyName ?: $number->phoneNumber ?: 'Unknown',
'voice_url' => $number->voiceUrl ?: '',
'sms_url' => $number->smsUrl ?: '',
'status_callback_url' => $number->statusCallback ?: '',
'capabilities' => [
'voice' => $number->capabilities ? (bool)$number->capabilities->getVoice() : false,
'sms' => $number->capabilities ? (bool)$number->capabilities->getSms() : false,
@@ -372,6 +411,11 @@ class TWP_Twilio_API {
$params['smsMethod'] = 'POST';
}
// Add status callback for real-time call state tracking
$status_callback_url = home_url('/wp-json/twilio-webhook/v1/status');
$params['statusCallback'] = $status_callback_url;
$params['statusCallbackMethod'] = 'POST';
$number = $this->client->incomingPhoneNumbers->create($params);
return [
@@ -430,6 +474,11 @@ class TWP_Twilio_API {
$params['smsMethod'] = 'POST';
}
// Add status callback for real-time call state tracking
$status_callback_url = home_url('/wp-json/twilio-webhook/v1/status');
$params['statusCallback'] = $status_callback_url;
$params['statusCallbackMethod'] = 'POST';
$number = $this->client->incomingPhoneNumbers($phone_sid)->update($params);
return [
@@ -464,6 +513,34 @@ class TWP_Twilio_API {
return $this->client;
}
/**
* Get SMS from number with proper priority
*/
public static function get_sms_from_number($workflow_id = null) {
// Priority 1: If we have a workflow_id, get the workflow's phone number
if ($workflow_id) {
$workflow = TWP_Workflow::get_workflow($workflow_id);
if ($workflow && !empty($workflow->phone_number)) {
return $workflow->phone_number;
}
}
// Priority 2: Use default SMS number setting
$default_sms_number = get_option('twp_default_sms_number');
if (!empty($default_sms_number)) {
return $default_sms_number;
}
// Priority 3: Fall back to first available Twilio number
$twilio = new self();
$phone_numbers = $twilio->get_phone_numbers();
if ($phone_numbers['success'] && !empty($phone_numbers['data']['incoming_phone_numbers'])) {
return $phone_numbers['data']['incoming_phone_numbers'][0]['phone_number'];
}
return null;
}
/**
* Validate webhook signature
*/
@@ -471,4 +548,110 @@ class TWP_Twilio_API {
$validator = new \Twilio\Security\RequestValidator(get_option('twp_twilio_auth_token'));
return $validator->validate($signature, $url, $params);
}
/**
* Get call information from Twilio
*/
public function get_call_info($call_sid) {
try {
$call = $this->client->calls($call_sid)->fetch();
return [
'sid' => $call->sid,
'status' => $call->status,
'from' => $call->from,
'to' => $call->to,
'duration' => $call->duration,
'start_time' => $call->startTime ? $call->startTime->format('Y-m-d H:i:s') : null,
'end_time' => $call->endTime ? $call->endTime->format('Y-m-d H:i:s') : null,
'direction' => $call->direction,
'price' => $call->price,
'priceUnit' => $call->priceUnit
];
} catch (\Twilio\Exceptions\TwilioException $e) {
error_log('TWP: Error fetching call info for ' . $call_sid . ': ' . $e->getMessage());
return null;
}
}
/**
* Toggle status callback for a specific phone number
*/
public function toggle_number_status_callback($phone_sid, $enable = true) {
try {
$params = [];
if ($enable) {
$params['statusCallback'] = home_url('/wp-json/twilio-webhook/v1/status');
$params['statusCallbackMethod'] = 'POST';
} else {
// Clear the status callback
$params['statusCallback'] = '';
}
$number = $this->client->incomingPhoneNumbers($phone_sid)->update($params);
return [
'success' => true,
'data' => [
'sid' => $number->sid,
'phone_number' => $number->phoneNumber,
'status_callback' => $number->statusCallback,
'enabled' => !empty($number->statusCallback)
]
];
} catch (\Twilio\Exceptions\TwilioException $e) {
return [
'success' => false,
'error' => $e->getMessage()
];
}
}
/**
* Update all existing phone numbers to include status callbacks
*/
public function enable_status_callbacks_for_all_numbers() {
try {
$numbers = $this->get_phone_numbers();
if (!$numbers['success']) {
return [
'success' => false,
'error' => 'Failed to retrieve phone numbers: ' . $numbers['error']
];
}
$status_callback_url = home_url('/wp-json/twilio-webhook/v1/status');
$updated_count = 0;
$errors = [];
foreach ($numbers['data']['incoming_phone_numbers'] as $number) {
try {
$this->client->incomingPhoneNumbers($number['sid'])->update([
'statusCallback' => $status_callback_url,
'statusCallbackMethod' => 'POST'
]);
$updated_count++;
error_log('TWP: Added status callback to phone number: ' . $number['phone_number']);
} catch (\Twilio\Exceptions\TwilioException $e) {
$errors[] = 'Failed to update ' . $number['phone_number'] . ': ' . $e->getMessage();
error_log('TWP: Error updating phone number ' . $number['phone_number'] . ': ' . $e->getMessage());
}
}
return [
'success' => true,
'data' => [
'updated_count' => $updated_count,
'total_numbers' => count($numbers['data']['incoming_phone_numbers']),
'errors' => $errors
]
];
} catch (\Twilio\Exceptions\TwilioException $e) {
return [
'success' => false,
'error' => $e->getMessage()
];
}
}
}