Welcome to WordPress. This is your first post. Edit or delete it, then start writing!
/* --- CÓDIGO DIRECTO PARA GEMINI (SIN ARCHIVOS EXTERNOS) --- */ add_action('wp_ajax_chat_gemini', 'conectar_gemini_directo'); add_action('wp_ajax_nopriv_chat_gemini', 'conectar_gemini_directo'); function conectar_gemini_directo() { // 1. Limpieza básica $mensaje = isset($_POST['mensaje']) ? sanitize_text_field($_POST['mensaje']) : ''; // 2. TU CLAVE API (¡Asegúrate de que está dentro de las comillas!) $api_key = 'AIzaSyBCB7oqC7awJti7mwo4gYt9ARaRzsh_HEc'; if (empty($mensaje)) { wp_send_json_error('El mensaje estaba vacío.'); } // 3. Preparar llamada a Google $url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=' . $api_key; $body = json_encode([ 'contents' => [ ['parts' => [['text' => $mensaje]]] ] ]); // 4. Enviar petición $response = wp_remote_post($url, [ 'body' => $body, 'headers' => ['Content-Type' => 'application/json'], 'timeout' => 20 ]); // 5. Comprobar errores de conexión if (is_wp_error($response)) { wp_send_json_error('Error de conexión WP: ' . $response->get_error_message()); } // 6. Decodificar lo que dice Google $raw_body = wp_remote_retrieve_body($response); $data = json_decode($raw_body, true); // 7. Extraer texto o devolver error real de Google if (isset($data['error'])) { // Si Google devuelve error (ej: API Key inválida) wp_send_json_error('Error de Google: ' . $data['error']['message']); } if (isset($data['candidates'][0]['content']['parts'][0]['text'])) { $texto = $data['candidates'][0]['content']['parts'][0]['text']; wp_send_json_success($texto); } else { // Si no hay texto y no hay error claro wp_send_json_error('Respuesta inesperada: ' . substr($raw_body, 0, 100)); } }
Welcome to WordPress. This is your first post. Edit or delete it, then start writing!
Hi, this is a comment.
To get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.
Commenter avatars come from Gravatar.