Update send_message.php
This commit is contained in:
+55
-1
@@ -217,7 +217,61 @@ try {
|
||||
error_log("Method: sendTemplateMessage()");
|
||||
error_log("========================================================");
|
||||
|
||||
$response = $whatsappService->sendTemplateMessage($recipient, $template, $language, $parameters);
|
||||
// Intentar envío y, si falla por traducción no encontrada, probar variantes de idioma
|
||||
try {
|
||||
$response = $whatsappService->sendTemplateMessage($recipient, $template, $language, $parameters);
|
||||
} catch (Exception $e) {
|
||||
$message = $e->getMessage();
|
||||
|
||||
if (strpos($message, 'Template name does not exist in the translation') !== false || strpos($message, '#132001') !== false) {
|
||||
error_log("Template translation missing for language: $language. Attempting fallbacks...");
|
||||
|
||||
$candidates = [];
|
||||
|
||||
// Añadir language_code de la BD si existe y es distinto
|
||||
if (!empty($templateRecord['language_code']) && $templateRecord['language_code'] !== $language) {
|
||||
$candidates[] = $templateRecord['language_code'];
|
||||
}
|
||||
|
||||
// Si viene con formato regional (es_ES) intentar la base (es)
|
||||
if (strpos($language, '_') !== false) {
|
||||
$base = explode('_', $language)[0];
|
||||
if ($base !== $language) $candidates[] = $base;
|
||||
} else {
|
||||
// Si es código corto (es) intentar variantes comunes
|
||||
if (strlen($language) === 2) {
|
||||
$candidates[] = strtoupper($language) === $language ? $language : $language . '_ES';
|
||||
$candidates[] = $language . '_MX';
|
||||
$candidates[] = $language . '_PE';
|
||||
}
|
||||
}
|
||||
|
||||
// Eliminar duplicados y vacíos
|
||||
$candidates = array_values(array_unique(array_filter($candidates)));
|
||||
|
||||
foreach ($candidates as $cand) {
|
||||
try {
|
||||
error_log("Retrying template send with language candidate: $cand");
|
||||
$response = $whatsappService->sendTemplateMessage($recipient, $template, $cand, $parameters);
|
||||
if ($response) {
|
||||
error_log("Template send succeeded with fallback language: $cand");
|
||||
break;
|
||||
}
|
||||
} catch (Exception $e2) {
|
||||
error_log("Fallback language $cand failed: " . $e2->getMessage());
|
||||
// seguir intentando con siguientes candidatos
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($response)) {
|
||||
// Re-lanzar el error original si no encontramos nada que funcione
|
||||
throw $e;
|
||||
}
|
||||
} else {
|
||||
// Re-lanzar si es otro tipo de error
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Formato 2: WhatsApp Business API estándar
|
||||
elseif (isset($input['template']) && is_array($input['template'])) {
|
||||
|
||||
Reference in New Issue
Block a user