%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /home/tjamichg/chatbot.tjamich.gob.mx/conexion/
Upload File :
Create Path :
Current File : /home/tjamichg/chatbot.tjamich.gob.mx/conexion/responder-asesor.php

<?php
//session_start();
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json charset=utf-8");

require("conexion.php");

$input = json_decode(file_get_contents("php://input"), true);

if (!$input || !isset($input["mensaje_asesor"]) || !isset($input["telefono"])) {
    echo json_encode(["mensaje_asesor" => "error", "error" => "Datos incompletos."]);
    exit;
}

$mensaje_asesor = trim($input["mensaje_asesor"]);
$telefono = trim($input["telefono"]);


//trae el historial
if ($mensaje_asesor === "") {
    $sqlHistorial = "SELECT mensaje_usuario, mensaje_asesor FROM mensajes WHERE telefono = ? ORDER BY fecha ASC";
    $stmt = $conexion->prepare($sqlHistorial);
    $stmt->bind_param("s", $telefono);
    $stmt->execute();
    $resultado = $stmt->get_result();

    $historial = [];
    while ($fila = $resultado->fetch_assoc()) {
        $historial[] = [
            "usuario" => $fila["mensaje_usuario"],
            "asesor" => $fila["mensaje_asesor"]
        ];
    }

    echo json_encode(["historial" => $historial]);
    $stmt->close();
    $conexion->close();
    exit;
}

//guardamos el mensajes del usuario en la base de datos
$sql = "INSERT INTO mensajes (telefono, mensaje_usuario) VALUES (?, ?)";
$stmt = $conexion->prepare($sql);
$stmt->bind_param("ss", $telefono, $mensaje_asesor);

if ($stmt->execute()) {
    echo json_encode(["status" => "ok"]);
} else {
    echo json_encode(["status" => "error", "error" => $stmt->error]);
}

$stmt->close();
$conexion->close();

Zerion Mini Shell 1.0