Fix mobile app: AccessToken for voice, Agent Manager for status, caller ID support
All checks were successful
Create Release / build (push) Successful in 3s
All checks were successful
Create Release / build (push) Successful in 3s
- Voice token: use AccessToken + VoiceGrant instead of browser-only ClientToken - Agent status: delegate to TWP_Agent_Manager matching browser phone behavior - Queue loading: add missing require_once for TWP_User_Queue_Manager - Add /phone-numbers endpoint for caller ID selection - Webhook: support CallerId param from mobile extraOptions - Flutter: caller ID dropdown in dialer, error logging in all catch blocks Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -106,6 +106,13 @@ class TWP_Mobile_API {
|
||||
'callback' => array($this, 'get_voice_token'),
|
||||
'permission_callback' => array($this->auth, 'verify_token')
|
||||
));
|
||||
|
||||
// Phone numbers for caller ID
|
||||
register_rest_route('twilio-mobile/v1', '/phone-numbers', array(
|
||||
'methods' => 'GET',
|
||||
'callback' => array($this, 'get_phone_numbers'),
|
||||
'permission_callback' => array($this->auth, 'verify_token')
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -162,39 +169,16 @@ class TWP_Mobile_API {
|
||||
return new WP_Error('invalid_status', 'Status must be available, busy, or offline', array('status' => 400));
|
||||
}
|
||||
|
||||
global $wpdb;
|
||||
$table = $wpdb->prefix . 'twp_agent_status';
|
||||
|
||||
// Check if status exists
|
||||
$exists = $wpdb->get_var($wpdb->prepare(
|
||||
"SELECT COUNT(*) FROM $table WHERE user_id = %d",
|
||||
$user_id
|
||||
));
|
||||
|
||||
$data = array(
|
||||
'status' => $new_status,
|
||||
'last_activity' => current_time('mysql')
|
||||
);
|
||||
require_once plugin_dir_path(__FILE__) . 'class-twp-agent-manager.php';
|
||||
require_once plugin_dir_path(__FILE__) . 'class-twp-user-queue-manager.php';
|
||||
|
||||
// Handle login status change first (matches browser phone behavior)
|
||||
if ($is_logged_in !== null) {
|
||||
$data['is_logged_in'] = $is_logged_in ? 1 : 0;
|
||||
if ($is_logged_in) {
|
||||
$data['logged_in_at'] = current_time('mysql');
|
||||
}
|
||||
TWP_Agent_Manager::set_agent_login_status($user_id, (bool)$is_logged_in);
|
||||
}
|
||||
|
||||
if ($exists) {
|
||||
$wpdb->update(
|
||||
$table,
|
||||
$data,
|
||||
array('user_id' => $user_id),
|
||||
array('%s', '%s'),
|
||||
array('%d')
|
||||
);
|
||||
} else {
|
||||
$data['user_id'] = $user_id;
|
||||
$wpdb->insert($table, $data);
|
||||
}
|
||||
// Set agent status (handles auto_busy_at and all status fields)
|
||||
TWP_Agent_Manager::set_agent_status($user_id, $new_status);
|
||||
|
||||
return new WP_REST_Response(array(
|
||||
'success' => true,
|
||||
@@ -221,6 +205,7 @@ class TWP_Mobile_API {
|
||||
));
|
||||
|
||||
if (!$existing_extension) {
|
||||
require_once plugin_dir_path(__FILE__) . 'class-twp-user-queue-manager.php';
|
||||
TWP_User_Queue_Manager::create_user_queues($user_id);
|
||||
}
|
||||
|
||||
@@ -693,18 +678,29 @@ class TWP_Mobile_API {
|
||||
$identity = 'agent' . $user_id . $clean_name;
|
||||
|
||||
try {
|
||||
// Ensure Twilio SDK autoloader is loaded
|
||||
require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-twp-twilio-api.php';
|
||||
$twilio = new TWP_Twilio_API();
|
||||
$result = $twilio->generate_capability_token($identity);
|
||||
new TWP_Twilio_API();
|
||||
|
||||
if (!$result['success']) {
|
||||
return new WP_Error('token_error', $result['error'], array('status' => 500));
|
||||
$account_sid = get_option('twp_twilio_account_sid');
|
||||
$auth_token = get_option('twp_twilio_auth_token');
|
||||
$twiml_app_sid = get_option('twp_twiml_app_sid');
|
||||
|
||||
if (empty($account_sid) || empty($auth_token) || empty($twiml_app_sid)) {
|
||||
return new WP_Error('token_error', 'Twilio credentials not configured', array('status' => 500));
|
||||
}
|
||||
|
||||
// AccessToken for mobile Voice SDK (not ClientToken which is browser-only)
|
||||
$token = new \Twilio\Jwt\AccessToken($account_sid, $account_sid, $auth_token, 3600, $identity);
|
||||
$voiceGrant = new \Twilio\Jwt\Grants\VoiceGrant();
|
||||
$voiceGrant->setOutgoingApplicationSid($twiml_app_sid);
|
||||
$voiceGrant->setIncomingAllow(true);
|
||||
$token->addGrant($voiceGrant);
|
||||
|
||||
return new WP_REST_Response(array(
|
||||
'token' => $result['data']['token'],
|
||||
'identity' => $result['data']['client_name'],
|
||||
'expires_in' => $result['data']['expires_in']
|
||||
'token' => $token->toJWT(),
|
||||
'identity' => $identity,
|
||||
'expires_in' => 3600
|
||||
), 200);
|
||||
|
||||
} catch (Exception $e) {
|
||||
@@ -712,6 +708,37 @@ class TWP_Mobile_API {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get available Twilio phone numbers for caller ID
|
||||
*/
|
||||
public function get_phone_numbers($request) {
|
||||
try {
|
||||
require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-twp-twilio-api.php';
|
||||
$twilio = new TWP_Twilio_API();
|
||||
$result = $twilio->get_phone_numbers();
|
||||
|
||||
if (!$result['success']) {
|
||||
return new WP_Error('twilio_error', $result['error'], array('status' => 500));
|
||||
}
|
||||
|
||||
$phone_numbers = array();
|
||||
foreach ($result['data']['incoming_phone_numbers'] as $number) {
|
||||
$phone_numbers[] = array(
|
||||
'phone_number' => $number['phone_number'],
|
||||
'friendly_name' => $number['friendly_name'],
|
||||
);
|
||||
}
|
||||
|
||||
return new WP_REST_Response(array(
|
||||
'success' => true,
|
||||
'phone_numbers' => $phone_numbers
|
||||
), 200);
|
||||
|
||||
} catch (Exception $e) {
|
||||
return new WP_Error('twilio_error', $e->getMessage(), array('status' => 500));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if user has access to a queue
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user