funcional
This commit is contained in:
+64
-4
@@ -18,21 +18,38 @@ if (!$debugMode) {
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
header('Access-Control-Allow-Methods: POST');
|
||||
header('Access-Control-Allow-Methods: POST, OPTIONS');
|
||||
header('Access-Control-Allow-Headers: Content-Type');
|
||||
|
||||
// Manejar preflight OPTIONS request
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
||||
http_response_code(200);
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
http_response_code(405);
|
||||
echo json_encode(['error' => 'Método no permitido']);
|
||||
echo json_encode(['error' => 'Método no permitido. Use POST.', 'method_received' => $_SERVER['REQUEST_METHOD']]);
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$rawInput = file_get_contents('php://input');
|
||||
$input = json_decode($rawInput, true);
|
||||
|
||||
// Debug logging
|
||||
if ($debugMode) {
|
||||
error_log("=== SEND MESSAGE DEBUG ===");
|
||||
error_log("Method: " . $_SERVER['REQUEST_METHOD']);
|
||||
error_log("Content-Type: " . ($_SERVER['CONTENT_TYPE'] ?? 'not set'));
|
||||
error_log("Raw input: " . $rawInput);
|
||||
error_log("Parsed JSON: " . json_encode($input));
|
||||
error_log("========================");
|
||||
}
|
||||
|
||||
if (!$input) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'JSON inválido']);
|
||||
echo json_encode(['error' => 'JSON inválido o vacío', 'raw_input' => $rawInput]);
|
||||
exit;
|
||||
}
|
||||
|
||||
@@ -241,6 +258,49 @@ try {
|
||||
error_log("Response: " . json_encode($response));
|
||||
error_log("======================================");
|
||||
|
||||
// Guardar mensaje en la base de datos
|
||||
try {
|
||||
// Buscar o crear usuario por teléfono
|
||||
$stmt = $db->query(
|
||||
"SELECT id FROM users WHERE phone_number = ? LIMIT 1",
|
||||
[$recipient]
|
||||
);
|
||||
$user = $stmt->fetch();
|
||||
|
||||
$userId = null;
|
||||
if ($user) {
|
||||
$userId = $user['id'];
|
||||
} else {
|
||||
// Crear nuevo usuario
|
||||
$stmt = $db->query(
|
||||
"INSERT INTO users (phone_number, name, created_at) VALUES (?, ?, NOW())",
|
||||
[$recipient, $recipient, date('Y-m-d H:i:s')]
|
||||
);
|
||||
$userId = $db->lastInsertId();
|
||||
}
|
||||
|
||||
if ($userId) {
|
||||
// Preparar datos del mensaje
|
||||
$messageContent = '';
|
||||
$messageType = $type;
|
||||
|
||||
if ($type === 'text') {
|
||||
$messageContent = $input['message'];
|
||||
} elseif ($type === 'template') {
|
||||
$templateName = $input['template'] ?? $input['template_name'] ?? '';
|
||||
$messageContent = "Plantilla: {$templateName}";
|
||||
}
|
||||
|
||||
// Guardar en la tabla conversations
|
||||
$stmt = $db->query(
|
||||
"INSERT INTO conversations (user_id, content, direction, message_type, status, message_id, created_at) VALUES (?, ?, 'outgoing', ?, 'sent', ?, NOW())",
|
||||
[$userId, $messageContent, $messageType, $response['messages'][0]['id'] ?? null]
|
||||
);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
error_log("Error guardando mensaje en base de datos: " . $e->getMessage());
|
||||
}
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'message' => 'Mensaje enviado correctamente',
|
||||
|
||||
Reference in New Issue
Block a user