// Farvision Smatbot Webhook add_action('rest_api_init', function() { register_rest_route('farvision/v1', '/smatbot', array( 'methods' => 'POST', 'callback' => 'handle_smatbot_webhook', 'permission_callback' => '__return_true' )); }); function handle_smatbot_webhook($request) { $body = $request->get_json_params(); $data = $body['data'] ?? []; $name = ''; $phone = ''; $email = ''; foreach ($data as $item) { $q = strtolower($item['question_text'] ?? ''); $a = $item['answer'] ?? ''; if (strpos($q, 'name') !== false) $name = sanitize_text_field($a); if (strpos($q, 'mobile') !== false || strpos($q, 'phone') !== false) $phone = sanitize_text_field($a); if (strpos($q, 'email') !== false) $email = sanitize_email($a); } if (empty($phone)) return new WP_REST_Response(['status' => 'skipped'], 200); $name_parts = explode(' ', $name, 2); $first_name = $name_parts[0] ?? ''; $last_name = $name_parts[1] ?? ''; $payload = array( 'firstName' => $first_name, 'lastName' => $last_name, 'email' => $email, 'countryCode1' => '91', 'mobilePhone' => $phone, 'comments' => 'Lead from Smatbot', 'originFrom' => 'Website', 'product' => 'ANANTAM', 'campaign' => 'SmatBot', 'externalAPIObjectId' => 'Website', 'DumpdataObjectId' => date('YmdHis'), 'tenantId' => '996' ); wp_remote_post('https://fvintegration.farvisioncloud.com/LeadSync/api/SyncLeadsV2/RawLeads', array( 'method' => 'POST', 'headers' => array('Content-Type' => 'application/json'), 'body' => json_encode($payload), 'timeout' => 15 )); return new WP_REST_Response(['status' => 'success'], 200); }