
Chatbot de WhatsApp con ChatGPT con PHP
Para comenzar a crear nuestro Chatbot de WhatsApp con ChatGPT con PHP debemos tener una cuenta de OpenAI (la empresa dueña de ChatGPT) además una API Key siguiendo estos pasos:
Crear una API Key de ChatGPT



Instalar PHP

Escribir código

<?php function preguntaChatgpt($pregunta){ //API KEY DE CHATGPT $apiKey='tu-api-key'; //INICIAMOS LA CONSULTA DE CURL $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/completions'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Authorization: Bearer '.$apiKey, ]); //INICIAMOS EL JSON QUE SE ENVIARA A META curl_setopt($ch, CURLOPT_POSTFIELDS, "{ \"model\": \"text-davinci-003\", \"prompt\": \"".$pregunta."\", \"max_tokens\": 4000, \"temperature\": 1.0 }"); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //OBTENEMOS EL JSON COMPLETO CON LA RESPUESTA $response = curl_exec($ch); curl_close($ch); $decoded_json = json_decode($response, false); //RETORNAMOS LA RESPUESTA QUE EXTRAEMOS DEL JSON return $decoded_json->choices[0]->text; } $respuesta=preguntaChatgpt("Cuanto es 6 mas 6"); echo $respuesta;

🔗 Codigo: programadornovato/chatgptWA (github.com)