This commit is contained in:
lizandrogd
2026-01-21 09:48:09 -05:00
parent 86347914e0
commit b1e291bf05
4 changed files with 79 additions and 2 deletions
+1 -1
View File
@@ -69,7 +69,7 @@ try {
'direction' => $msg['direction'],
'message_type' => $msg['message_type'],
'content' => $msg['content'] ?? '',
'media_url' => $msg['media_url'] ?? null,
'media_url' => (isset($msg['media_url']) && $msg['media_url']) ? (preg_match('#^https?://#i', $msg['media_url']) ? $msg['media_url'] : ("api/get_media.php?id=" . urlencode($msg['media_url']))) : null,
'status' => $msg['status'] ?? 'sent',
'created_at' => $msg['created_at'],
'time' => date('H:i', strtotime($msg['created_at'])),
+69
View File
@@ -0,0 +1,69 @@
<?php
/**
* API - Obtener URL temporal del media de WhatsApp
* Parámetros: id (media id)
*/
require_once '../config/config.php';
requireAuthentication();
header('Content-Type: application/json; charset=utf-8');
$mediaId = $_GET['id'] ?? ($_POST['id'] ?? null);
if (!$mediaId) {
http_response_code(400);
echo json_encode(['error' => 'media id requerido']);
exit;
}
$token = getConfigFromDB('whatsapp_token', '');
$apiUrl = rtrim(getConfigFromDB('whatsapp_api_url', 'https://graph.facebook.com/v22.0/'), '/');
$url = "{$apiUrl}/{$mediaId}";
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [ 'Authorization: Bearer ' . $token ],
CURLOPT_TIMEOUT => 10,
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$err = curl_error($ch);
curl_close($ch);
if ($err) {
http_response_code(502);
echo json_encode(['error' => 'Error comunicando con Graph API', 'detail' => $err]);
exit;
}
$decoded = json_decode($response, true);
if ($httpCode >= 400 || !$decoded) {
http_response_code($httpCode ?: 500);
echo json_encode(['error' => 'Graph API error', 'response' => $decoded]);
exit;
}
// Normalmente Graph devuelve field 'url'
$mediaUrl = $decoded['url'] ?? ($decoded['data'][0]['url'] ?? null);
if (!$mediaUrl) {
http_response_code(404);
echo json_encode(['error' => 'No se recibió URL del media', 'raw' => $decoded]);
exit;
}
// Si es una petición GET y el cliente no espera JSON, redirigir al URL real para que `<img src="api/get_media.php?id=...">` funcione directamente
$isGet = $_SERVER['REQUEST_METHOD'] === 'GET';
$accept = $_SERVER['HTTP_ACCEPT'] ?? '';
if ($isGet && strpos($accept, 'application/json') === false) {
// Cache corto para navegadores
header('Cache-Control: public, max-age=3600');
header('Location: ' . $mediaUrl, true, 302);
exit;
}
// Retornar URL en JSON (cliente puede solicitar directamente)
header('Content-Type: application/json; charset=utf-8');
echo json_encode(['success' => true, 'url' => $mediaUrl]);
+1 -1
View File
@@ -86,7 +86,7 @@ try {
'user_id' => intval($msg['user_id']),
'content' => $msg['content'] ?? '',
'message_id' => $msg['message_id'] ?? null,
'media_url' => $msg['media_url'] ?? null,
'media_url' => (isset($msg['media_url']) && $msg['media_url']) ? (preg_match('#^https?://#i', $msg['media_url']) ? $msg['media_url'] : ("api/get_media.php?id=" . urlencode($msg['media_url']))) : null,
'user_phone' => $msg['user_phone'] ?? null,
'direction' => $msg['direction'] ?? 'incoming',
'message_type' => $msg['message_type'] ?? 'text',
+8
View File
@@ -0,0 +1,8 @@
<?php
require_once __DIR__ . '/../config/config.php';
$id = $argv[1] ?? null;
if (!$id) { echo "Usage: php show_conversation.php <id>\n"; exit(1); }
$db = Database::getInstance();
$r = $db->fetch('SELECT id, message_id, message_type, content, media_url FROM conversations WHERE id = ?', [$id]);
header('Content-Type: application/json');
echo json_encode($r, JSON_PRETTY_PRINT);