Mejoras en el envío de plantillas con imágenes

- Agregar detección de URLs de Discord gifts para evitar botones de traducción
- Enviar imágenes en orden correcto (texto-imagen-texto-imagen) en Discord y Telegram
- Usar APP_URL del .env para las URLs de imágenes
- Agregar funciones sendContentWithOrderedImagesAndButtons en ambos bots
This commit is contained in:
2026-02-19 19:12:28 -06:00
parent a55c45ef94
commit e8912bdb63
5 changed files with 130 additions and 89 deletions

View File

@@ -95,34 +95,29 @@ class DiscordSender
$result = null;
if (!empty($images)) {
// Verificar si las imágenes son locales o URLs
$localImages = [];
$remoteImages = [];
$appUrl = $_ENV['APP_URL'] ?? getenv('APP_URL') ?? '';
$baseUrl = rtrim($appUrl, '/');
foreach ($images as $imageUrl) {
if (strpos($imageUrl, 'http') === 0) {
// Es una URL remota
$remoteImages[] = $imageUrl;
} elseif (file_exists($imageUrl)) {
// Es un archivo local
$localImages[] = $imageUrl;
} else {
// Convertir a URL usando APP_URL
$remoteImages[] = $baseUrl . '/' . ltrim($imageUrl, '/');
}
}
// Enviar imágenes locales como adjuntos
if (!empty($localImages)) {
$result = $this->sendMessageWithAttachments($channelId, $content, $localImages);
} else {
$result = $this->sendMessage($channelId, $content, null, $buttons);
}
// Enviar todas las imágenes como embeds
$result = $this->sendMessage($channelId, $content, null, $buttons);
// Enviar imágenes remotas como embeds
foreach ($remoteImages as $imageUrl) {
$embed = [
'image' => ['url' => $imageUrl]
];
$result = $this->sendMessage($channelId, '', $embed, $buttons);
$buttons = null; // Solo enviar botones en el primer mensaje
$result = $this->sendMessage($channelId, '', $embed);
$buttons = null;
}
} else {
$result = $this->sendMessage($channelId, $content, null, $buttons);
@@ -136,26 +131,48 @@ class DiscordSender
* Divide el contenido en segmentos y los envía manteniendo el orden
*/
public function sendContentWithOrderedImages(string $channelId, array $segments): void
{
$this->sendContentWithOrderedImagesAndButtons($channelId, $segments, null);
}
public function sendContentWithOrderedImagesAndButtons(string $channelId, array $segments, ?array $buttons = null): void
{
$channelId = $this->resolveUserToDmChannel($channelId);
$appUrl = $_ENV['APP_URL'] ?? getenv('APP_URL') ?? '';
$baseUrl = rtrim($appUrl, '/');
$totalSegments = count($segments);
$currentSegment = 0;
foreach ($segments as $segment) {
$currentSegment++;
$isLastSegment = ($currentSegment === $totalSegments);
if ($segment['type'] === 'text') {
// Enviar texto
if (!empty(trim($segment['content']))) {
$this->sendMessage($channelId, $segment['content']);
// Solo enviar botones en el último segmento de texto
if ($isLastSegment && $buttons) {
$this->sendMessage($channelId, $segment['content'], null, $buttons);
} else {
$this->sendMessage($channelId, $segment['content']);
}
}
} elseif ($segment['type'] === 'image') {
// Enviar imagen
$imagePath = $segment['src'];
if (strpos($imagePath, 'http') === 0) {
// URL remota - enviar como embed
$embed = ['image' => ['url' => $imagePath]];
} else {
$imageUrl = $baseUrl . '/' . ltrim($imagePath, '/');
$embed = ['image' => ['url' => $imageUrl]];
}
// Solo enviar botones en el último segmento
if ($isLastSegment && $buttons) {
$this->sendMessage($channelId, '', $embed, $buttons);
} else {
$this->sendMessage($channelId, '', $embed);
} elseif (file_exists($imagePath)) {
// Archivo local - enviar como adjunto
$this->sendMessageWithAttachments($channelId, '', [$imagePath]);
}
}
}