This commit is contained in:
Lizandro Guarnizo
2026-01-27 14:51:41 -05:00
parent c1f3524466
commit 1f224044f7
21 changed files with 2997 additions and 47 deletions
+94 -7
View File
@@ -313,7 +313,7 @@ class WhatsAppService
/**
* Enviar video
*/
public function sendVideoMessage($to, $videoUrl, $caption = null)
public function sendVideoMessage($to, $videoUrl, $caption = null, $skipAutoSave = false)
{
$video = ['link' => $videoUrl];
@@ -328,13 +328,17 @@ class WhatsAppService
'video' => $video
];
if ($skipAutoSave) {
$data['__skip_auto_save'] = true;
}
return $this->sendMessage($data);
}
/**
* Enviar audio
*/
public function sendAudioMessage($to, $audioUrl)
public function sendAudioMessage($to, $audioUrl, $skipAutoSave = false)
{
$data = [
'messaging_product' => 'whatsapp',
@@ -345,13 +349,17 @@ class WhatsAppService
]
];
if ($skipAutoSave) {
$data['__skip_auto_save'] = true;
}
return $this->sendMessage($data);
}
/**
* Enviar documento
*/
public function sendDocumentMessage($to, $documentUrl, $filename = null, $caption = null)
public function sendDocumentMessage($to, $documentUrl, $filename = null, $caption = null, $skipAutoSave = false)
{
$document = ['link' => $documentUrl];
@@ -370,6 +378,10 @@ class WhatsAppService
'document' => $document
];
if ($skipAutoSave) {
$data['__skip_auto_save'] = true;
}
return $this->sendMessage($data);
}
@@ -423,7 +435,7 @@ class WhatsAppService
/**
* Enviar mensaje usando media_id (archivo ya subido a WhatsApp)
*/
public function sendMediaById($to, $mediaId, $mediaType, $caption = null, $filename = null)
public function sendMediaById($to, $mediaId, $mediaType, $caption = null, $filename = null, $skipAutoSave = false)
{
$data = [
'messaging_product' => 'whatsapp',
@@ -443,6 +455,11 @@ class WhatsAppService
$data[$mediaType] = $mediaData;
// Marcar para evitar guardado automático si se solicita
if ($skipAutoSave) {
$data['__skip_auto_save'] = true;
}
return $this->sendMessage($data);
}
@@ -468,6 +485,9 @@ class WhatsAppService
$response = $this->makeRequest('POST', $url, $payload);
// Guardar mensaje enviado en la base de datos (respuesta puede contener 'messages' o 'conversations')
// Solo guardar si no se solicitó skipAutoSave
$skipAutoSave = isset($data['__skip_auto_save']) && $data['__skip_auto_save'];
$sentMessageId = null;
if ($response) {
if (isset($response['messages'][0]['id'])) {
@@ -477,7 +497,7 @@ class WhatsAppService
}
}
if ($response && $sentMessageId) {
if ($response && $sentMessageId && !$skipAutoSave) {
// Asegurar que la estructura de respuesta para saveOutgoingMessage se mantenga pasando el id
$responseWrapper = $response;
// Normalizar para compatibilidad con saveOutgoingMessage
@@ -590,6 +610,39 @@ class WhatsAppService
'created_at' => date('Y-m-d H:i:s')
];
// Para mensajes multimedia, guardar el media_id si está disponible
if (in_array($data['type'], ['image', 'video', 'audio', 'document'])) {
$mediaId = $data[$data['type']]['id'] ?? null;
$mediaLink = $data[$data['type']]['link'] ?? null;
// Prioridad: usar media_id de WhatsApp, sino link
if ($mediaId) {
$messageData['media_url'] = $mediaId;
$messageData['whatsapp_media_id'] = $mediaId;
} elseif ($mediaLink) {
$messageData['media_url'] = $mediaLink;
}
// Asegurar que content tenga el filename si está disponible
$filename = $data[$data['type']]['filename'] ?? null;
if ($filename) {
// Si content está vacío o es una descripción genérica, usar filename
if (empty($messageData['content']) ||
in_array($messageData['content'], ['[Imagen]', '[Video]', '[Audio]', '[Documento]'])) {
$messageData['content'] = $filename;
}
}
}
// Log detallado para multimedia
if (in_array($data['type'], ['image', 'video', 'audio', 'document'])) {
error_log("WhatsAppService.saveOutgoingMessage - Multimedia:");
error_log(" - type: " . $data['type']);
error_log(" - content: " . ($messageData['content'] ?? 'empty'));
error_log(" - media_url: " . ($messageData['media_url'] ?? 'empty'));
error_log(" - whatsapp_media_id: " . ($messageData['whatsapp_media_id'] ?? 'empty'));
}
// Si es reply (context)
if (isset($data['context']['message_id'])) {
// WhatsApp message IDs can be alphanumeric (e.g., wamid...), store as string
@@ -637,9 +690,43 @@ class WhatsAppService
}
break;
case 'reaction':
return isset($data['reaction']['emoji']) ? $data['reaction']['emoji'] : json_encode($data['reaction']);
return isset($data['reaction']['emoji']) ? $data['reaction']['emoji'] : '';
case 'image':
// Prioridad: caption, filename, descripción por defecto
if (!empty($data['image']['caption'])) {
return $data['image']['caption'];
}
if (!empty($data['image']['filename'])) {
return $data['image']['filename'];
}
return '[Imagen]';
case 'video':
if (!empty($data['video']['caption'])) {
return $data['video']['caption'];
}
if (!empty($data['video']['filename'])) {
return $data['video']['filename'];
}
return '[Video]';
case 'audio':
if (!empty($data['audio']['filename'])) {
return $data['audio']['filename'];
}
if (!empty($data['audio']['caption'])) {
return $data['audio']['caption'];
}
return '[Audio]';
case 'document':
// Prioridad: filename, caption, descripción por defecto
if (!empty($data['document']['filename'])) {
return $data['document']['filename'];
}
if (!empty($data['document']['caption'])) {
return $data['document']['caption'];
}
return '[Documento]';
}
return json_encode($data);
return '';
}
/**