
Recibir WhatsApp y crear una respuesta con ChatGPT en PHP
Ya nos conectamos a ChatGPT y hemos obtenido respuestas a nuestras preguntas, ahora vamos a Recibir WhatsApp y crear una respuesta con ChatGPT en PHP. Recordemos que WhatsApp fue comprado por Meta y debemos conectarnos a Meta para poder recibir mensajes desde WhatsApp siguiendo estos pasos:
Creamos una app en Facebook

O podemos aceder a este link Todas las apps – Meta for Developers (facebook.com)





Configuramos el Api de WhatsApp






Código para Recibir WhatsApp y crear una respuesta con ChatGPT en PHP
Código
<?php //Codigo para Recibir WhatsApp y crear una respuesta con ChatGPT en PHP /* * VERIFICACION DEL WEBHOOK */ //TOQUEN QUE QUERRAMOS PONER $token = 'HolaNovato'; //RETO QUE RECIBIREMOS DE FACEBOOK $palabraReto = $_GET['hub_challenge']; //TOQUEN DE VERIFICACION QUE RECIBIREMOS DE FACEBOOK $tokenVerificacion = $_GET['hub_verify_token']; //SI EL TOKEN QUE GENERAMOS ES EL MISMO QUE NOS ENVIA FACEBOOK RETORNAMOS EL RETO PARA VALIDAR QUE SOMOS NOSOTROS if ($token === $tokenVerificacion) { echo $palabraReto; exit; } /* * RECEPCION DE MENSAJES */ //LEEMOS LOS DATOS ENVIADOS POR WHATSAPP $respuesta = file_get_contents("php://input"); //CONVERTIMOS EL JSON EN ARRAY DE PHP $respuesta = json_decode($respuesta, true); //EXTRAEMOS EL MENSAJE DEL ARRAY $mensaje=$respuesta['entry'][0]['changes'][0]['value']['messages'][0]['text']['body']; //EXTRAEMOS EL TELEFONO DEL ARRAY $telefonoCliente=$respuesta['entry'][0]['changes'][0]['value']['messages'][0]['from']; //EXTRAEMOS EL ID DE WHATSAPP DEL ARRAY $id=$respuesta['entry'][0]['changes'][0]['value']['messages'][0]['id']; //EXTRAEMOS EL TIEMPO DE WHATSAPP DEL ARRAY $timestamp=$respuesta['entry'][0]['changes'][0]['value']['messages'][0]['timestamp']; //SI HAY UN MENSAJE if($mensaje!=null){ require_once "chatgpt.php"; $respuesta=preguntaChatgpt($mensaje); //ESCRIBIMOS LA RESPUESTA file_put_contents("text.txt", $respuesta); //require_once "envia.php"; //ENVIAMOS LA RESPUESTA VIA WHATSAPP //enviar($mensaje,$respuesta,$id,$timestamp,$telefonoCliente); }
Activamos un servidor ngrok






🔗 Código: programadornovato/chatgptWA (github.com)