109 lines
3.2 KiB
PHP
109 lines
3.2 KiB
PHP
<?php
|
|
/**
|
|
* API - Enviar mensaje multimedia
|
|
* Fecha: 13 de enero de 2026
|
|
*/
|
|
|
|
require_once '../config/config.php';
|
|
|
|
// Verificar autenticación
|
|
requireAuthentication();
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
header('Access-Control-Allow-Origin: *');
|
|
header('Access-Control-Allow-Methods: POST');
|
|
header('Access-Control-Allow-Headers: Content-Type');
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
http_response_code(405);
|
|
echo json_encode(['success' => false, 'error' => 'Método no permitido']);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (!$input) {
|
|
throw new Exception('Datos no válidos');
|
|
}
|
|
|
|
// Validar campos requeridos
|
|
if (empty($input['recipient']) || empty($input['media_url']) || empty($input['media_type'])) {
|
|
throw new Exception('Faltan campos requeridos: recipient, media_url, media_type');
|
|
}
|
|
|
|
$recipient = $input['recipient'];
|
|
$mediaUrl = $input['media_url'];
|
|
$mediaType = $input['media_type']; // image, video, audio, document
|
|
$caption = $input['caption'] ?? null;
|
|
$filename = $input['filename'] ?? null;
|
|
|
|
// Inicializar servicio de WhatsApp
|
|
$whatsapp = new WhatsAppService();
|
|
|
|
// Enviar según tipo de media
|
|
$response = null;
|
|
|
|
switch ($mediaType) {
|
|
case 'image':
|
|
$response = $whatsapp->sendImageMessage($recipient, $mediaUrl, $caption);
|
|
break;
|
|
|
|
case 'video':
|
|
$response = $whatsapp->sendVideoMessage($recipient, $mediaUrl, $caption);
|
|
break;
|
|
|
|
case 'audio':
|
|
$response = $whatsapp->sendAudioMessage($recipient, $mediaUrl);
|
|
break;
|
|
|
|
case 'document':
|
|
$response = $whatsapp->sendDocumentMessage($recipient, $mediaUrl, $filename, $caption);
|
|
break;
|
|
|
|
default:
|
|
throw new Exception('Tipo de media no soportado: ' . $mediaType);
|
|
}
|
|
|
|
if ($response && isset($response['messages'][0]['id'])) {
|
|
// Guardar en base de datos
|
|
$db = Database::getInstance();
|
|
|
|
// Obtener usuario
|
|
$user = $db->fetch(
|
|
"SELECT id FROM users WHERE phone_number = ?",
|
|
[$recipient]
|
|
);
|
|
|
|
if ($user) {
|
|
$db->insert('conversations', [
|
|
'user_id' => $user['id'],
|
|
'message_id' => $response['messages'][0]['id'],
|
|
'direction' => 'outgoing',
|
|
'message_type' => $mediaType,
|
|
'content' => $caption ?? $filename ?? '',
|
|
'media_url' => $mediaUrl,
|
|
'status' => 'sent',
|
|
'created_at' => date('Y-m-d H:i:s')
|
|
]);
|
|
}
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'message' => 'Mensaje multimedia enviado correctamente',
|
|
'data' => $response
|
|
]);
|
|
} else {
|
|
throw new Exception('Error al enviar mensaje: ' . json_encode($response));
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
error_log("Error en send_media_message.php: " . $e->getMessage());
|
|
http_response_code(500);
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => $e->getMessage()
|
|
]);
|
|
}
|
|
?>
|