feat: Implement Discord translation feature with ephemeral messages and refined content filtering

This commit introduces a comprehensive message translation system for the Discord bot, featuring:
- Interactive language selection buttons for each translatable message.
- Ephemeral translation responses visible only to the requesting user.
- Robust filtering to prevent translation buttons for messages consisting solely of emojis, stickers, GIFs, or other non-translatable content.
- Preservation of non-textual elements (images, video thumbnails, stickers) alongside translated text in embeds.
- Full compatibility with DiscordPHP v7, addressing various API usage and error handling specifics (e.g.,  then , correct handling of null message properties,  intent).

Additionally, this commit resolves an incompatibility introduced in the shared  class, ensuring that the Telegram bot's translation functionality remains fully operational by correctly parsing  return values across both platforms.

The following files were modified:
- : Main Discord bot logic for message handling, button generation, and interaction processing.
- : Adjusted  to return full API response including confidence score.
- : Updated environment variable loading and added new constants for service URLs and tokens.
- : Updated constructor to include  for absolute URL generation.
- : Adjusted all calls to  to correctly extract language codes from the new array return format, resolving Telegram bot's translation issues.
This commit is contained in:
2026-02-08 16:28:17 -06:00
parent 3f0727984a
commit bc77082c20
5 changed files with 242 additions and 83 deletions

View File

@@ -7,45 +7,37 @@ require_once __DIR__ . '/../vendor/autoload.php';
// Verificar si estamos en un contenedor Docker
$is_docker = (getenv('DOCKER_CONTAINER') === '1') || ($_SERVER['DOCKER_CONTAINER'] ?? null) === '1';
if ($is_docker) {
// Docker: cargar archivo .env creado por el entrypoint
$dotenv = null;
if (file_exists(dirname(__DIR__) . '/.env')) {
$dotenv = Dotenv\Dotenv::createImmutable(dirname(__DIR__));
try {
$dotenv->load();
} catch (Exception $e) {
die('Error al cargar el archivo de entorno en Docker: ' . $e->getMessage());
}
}
} else {
// Entorno local: cargar archivo .env según APP_ENVIRONMENT
$env = getenv('APP_ENVIRONMENT') ?: ($_SERVER['APP_ENVIRONMENT'] ?? 'pruebas');
// Entorno: cargar archivo .env según APP_ENVIRONMENT
$env = getenv('APP_ENVIRONMENT') ?: ($_SERVER['APP_ENVIRONMENT'] ?? 'pruebas');
if ($env === 'reod') {
$envFile = '.env';
if ($env) {
$envFile = '.env.' . $env;
}
$dotenv = null;
if (file_exists(dirname(__DIR__) . '/' . $envFile)) {
$dotenv = Dotenv\Dotenv::createImmutable(dirname(__DIR__), $envFile);
} elseif (file_exists(dirname(__DIR__) . '/.env')) {
$dotenv = Dotenv\Dotenv::createImmutable(dirname(__DIR__));
}
if ($dotenv) {
try {
$dotenv->load();
} catch (Exception $e) {
die('Error al cargar el archivo de entorno: ' . $e->getMessage());
}
$dotenv->required([
'DB_HOST', 'DB_NAME', 'DB_USER', 'DB_PASS',
'JWT_SECRET', 'APP_URL'
]);
}
} else {
$envFile = '.env.' . $env;
}
$dotenv = null;
if (file_exists(dirname(__DIR__) . '/' . $envFile)) {
$dotenv = Dotenv\Dotenv::createImmutable(dirname(__DIR__), $envFile);
} elseif (file_exists(dirname(__DIR__) . '/.env')) {
$dotenv = Dotenv\Dotenv::createImmutable(dirname(__DIR__));
}
if ($dotenv) {
try {
$dotenv->load();
} catch (Exception $e) {
die('Error al cargar el archivo de entorno: ' . $e->getMessage());
}
// ... el resto de la configuración ...
// Aquí es donde definimos las variables obligatorias
$dotenv->required([
'DB_HOST', 'DB_NAME', 'DB_USER', 'DB_PASS',
'JWT_SECRET', 'APP_URL'
]);
}
// Environment Configuration
define('ENVIRONMENT', $_ENV['APP_ENV'] ?? $_SERVER['APP_ENV'] ?? 'production');
@@ -54,7 +46,9 @@ $is_cli = (php_sapi_name() === 'cli' || defined('STDIN'));
// Helper function to get env vars
function getEnvVar($name, $default = null) {
return $_ENV[$name] ?? $_SERVER[$name] ?? getenv($name) ?? $default;
// Priorizamos getenv() para las variables cargadas por Dotenv,
// ya que sabemos que funciona y $_ENV puede estar corrupto o contener Array en algunos entornos Docker.
return getenv($name) ?: ($_ENV[$name] ?? $_SERVER[$name] ?? $default);
}
// Configurar la URL base y el protocolo