One Hat Cyber Team
Your IP :
104.23.197.102
Server IP :
104.21.51.23
Server :
Linux 128-201-239-36.cprapid.com 3.10.0-1160.41.1.el7.x86_64 #1 SMP Tue Aug 31 14:52:47 UTC 2021 x86_64
Server Software :
Apache
PHP Version :
7.4.33
Buat File
|
Buat Folder
Eksekusi
Dir :
~
/
home
/
juscatamarca
/
www
/
subdomains
/
justabot
/
View File Name :
asistente_openai.php
<?php date_default_timezone_set('America/Argentina/Buenos_Aires'); function enviarPreguntaAlAsistente($mensaje,$threadId = null) { $apiKey = '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'; $assistantId = 'asst_DdNvVBD2zFKG04stWQQBvIEL'; // Crear un nuevo thread // $thread = curl_init('https://api.openai.com/v1/threads'); // curl_setopt($thread, CURLOPT_RETURNTRANSFER, true); // curl_setopt($thread, CURLOPT_POST, true); // curl_setopt($thread, CURLOPT_HTTPHEADER, [ // "Authorization: Bearer $apiKey", // "OpenAI-Beta: assistants=v2", // "Content-Type: application/json" // ]); // curl_setopt($thread, CURLOPT_POSTFIELDS, '{}'); // $response = curl_exec($thread); // $threadData = json_decode($response, true); // curl_close($thread); // if (!isset($threadData['id'])) { // return "Error al crear el thread."; // } // $threadId = $threadData['id']; if (!$threadId) { $thread = curl_init('https://api.openai.com/v1/threads'); curl_setopt($thread, CURLOPT_RETURNTRANSFER, true); curl_setopt($thread, CURLOPT_POST, true); curl_setopt($thread, CURLOPT_HTTPHEADER, [ "Authorization: Bearer $apiKey", "OpenAI-Beta: assistants=v2", "Content-Type: application/json" ]); curl_setopt($thread, CURLOPT_POSTFIELDS, '{}'); $response = curl_exec($thread); $threadData = json_decode($response, true); curl_close($thread); if (!isset($threadData['id'])) { return "Error al crear el thread."; } $threadId = $threadData['id']; } // Agregar el mensaje al thread $messageUrl = "https://api.openai.com/v1/threads/$threadId/messages"; $messageData = json_encode([ "role" => "user", "content" => $mensaje ]); $ch = curl_init($messageUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Authorization: Bearer $apiKey", "OpenAI-Beta: assistants=v2", "Content-Type: application/json" ]); curl_setopt($ch, CURLOPT_POSTFIELDS, $messageData); curl_exec($ch); curl_close($ch); // Lanzar el run $runUrl = "https://api.openai.com/v1/threads/$threadId/runs"; $runData = json_encode([ "assistant_id" => $assistantId ]); $ch = curl_init($runUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Authorization: Bearer $apiKey", "OpenAI-Beta: assistants=v2", "Content-Type: application/json" ]); curl_setopt($ch, CURLOPT_POSTFIELDS, $runData); $runResponse = curl_exec($ch); $runInfo = json_decode($runResponse, true); curl_close($ch); $runId = $runInfo['id']; // Esperar a que el run esté completo (polling simple) $status = "queued"; while ($status !== "completed" && $status !== "failed") { sleep(2); $ch = curl_init("https://api.openai.com/v1/threads/$threadId/runs/$runId"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Authorization: Bearer $apiKey", "OpenAI-Beta: assistants=v2" ]); $runStatus = curl_exec($ch); $statusData = json_decode($runStatus, true); $status = $statusData['status'] ?? 'failed'; curl_close($ch); } if ($status === "completed") { // Obtener la respuesta $ch = curl_init("https://api.openai.com/v1/threads/$threadId/messages"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Authorization: Bearer $apiKey", "OpenAI-Beta: assistants=v2" ]); $result = curl_exec($ch); $resultData = json_decode($result, true); curl_close($ch); $respuesta_final = "Sin respuesta."; foreach ($resultData['data'] as $mensaje) { if ($mensaje['role'] === 'assistant') { $contenido = $mensaje['content'][0]['text']['value'] ?? null; if ($contenido) { $respuesta_final = preg_replace('/【.*?】/', '', $contenido); break; } } } return $respuesta_final; } return "El assistant falló en responder."; } //$respuesta = enviarPreguntaAlAsistente("quien es el presidente de la corte??"); //echo $respuesta; ?>