03. ChatBot RiveScript almacenar mensajes

ChatBot guardar los WhatsApp que enviamos y recibimos

Ya que recibimos WhatsApp’s y tambien generamos una respuesta con inteligencia artificial, vamos a guardar los WhatsApp’s enviados y recibidos. Seguiremos estos pasos:

Creamos la Base de Datos y la tabla

Creamos la base de datos y creamos la tabla con el codigo de abajo

-- phpMyAdmin SQL Dump
-- version 5.1.1
-- https://www.phpmyadmin.net/
--
-- Servidor: 127.0.0.1
-- Tiempo de generación: 23-01-2023 a las 21:24:48
-- Versión del servidor: 10.4.20-MariaDB
-- Versión de PHP: 7.3.29

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Base de datos: `chat`
--

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `registro`
--

CREATE TABLE `registro` (
  `id` int(5) NOT NULL,
  `fecha_hora` datetime DEFAULT current_timestamp(),
  `mensaje_recibido` varchar(1000) DEFAULT '',
  `mensaje_enviado` varchar(1000) DEFAULT '',
  `id_wa` varchar(1000) DEFAULT '',
  `timestamp_wa` int(15) DEFAULT NULL,
  `telefono_wa` varchar(50) DEFAULT ''
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Índices para tablas volcadas
--

--
-- Indices de la tabla `registro`
--
ALTER TABLE `registro`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT de las tablas volcadas
--

--
-- AUTO_INCREMENT de la tabla `registro`
--
ALTER TABLE `registro`
  MODIFY `id` int(5) NOT NULL AUTO_INCREMENT;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

Codigo de php para guardar los WhatsApp

Creamos el archivo enviar.php y colocamos este código

/*
 * RECIBIMOS LA RESPUESTA
*/
function enviar($recibido, $enviado, $idWA,$timestamp,$telefonoCliente) {
    require_once './conexion.php';

    //INSERTAMOS LOS REGISTROS DEL ENVIO DEL WHATSAPP
    $sql = "INSERT INTO registro "
           . "(mensaje_recibido    ,mensaje_enviado   ,id_wa        ,timestamp_wa        ,     telefono_wa) VALUES "
           . "('" . $recibido . "' ,'" . $enviado . "','" . $idWA . "','" . $timestamp . "','" . $telefonoCliente . "');";
    $conn->query($sql);
    $conn->close();
}

En index.php agregamos este codigo
    //LLAMAMMOS A LA FUNCION DE ENVIAR RESPUESTA
    require_once './enviar.php';
    //ENVIAMOS LA RESPUESTA VIA WHATSAPP
    enviar($mensaje,$respuesta,$id,$timestamp,$telefonoCliente);

Creamos el archivo conexion.php con este codigo

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "chat";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

Si enviamos un mensaje se guardar los WhatsApp’s

Salir de la versión móvil

Ads Blocker Detected!!!

En Programador Novato vivimos de los anuncios, hemos detectado que está utilizando extensiones para bloquear anuncios. Ayudanos deshabilitando tu bloqueador de anuncios. :)