up
This commit is contained in:
lizandrogd
2026-01-20 23:29:06 -05:00
parent b5e2af7f47
commit 81708fbb22
9 changed files with 245 additions and 14 deletions
+58 -11
View File
@@ -48,22 +48,69 @@ class WhatsAppService
$templateName,
$language = 'es',
$bodyParameters = [],
$headerParameters = []
$headerParameters = [],
$rawComponents = null
) {
$components = [];
if (!empty($headerParameters)) {
$components[] = [
'type' => 'header',
'parameters' => $headerParameters
// Si se pasan components completos (por ejemplo flow/button/image), úsalos tal cual
if (is_array($rawComponents) && !empty($rawComponents)) {
$template = [
'name' => $templateName,
'language' => [ 'code' => $language ],
'components' => $rawComponents
];
$data = [
'messaging_product' => 'whatsapp',
'to' => $this->formatPhoneNumber($to),
'type' => 'template',
'template' => $template
];
return $this->sendMessage($data);
}
$components = [];
// Normalizar parámetros de header (si vienen como strings -> convertir a objeto de texto)
if (!empty($headerParameters)) {
$normalizedHeader = [];
foreach ($headerParameters as $hp) {
if (is_array($hp) && isset($hp['type'])) {
$normalizedHeader[] = $hp;
} elseif (is_string($hp)) {
$normalizedHeader[] = [
'type' => 'text',
'text' => $hp
];
}
}
if (!empty($normalizedHeader)) {
$components[] = [
'type' => 'header',
'parameters' => $normalizedHeader
];
}
}
// Normalizar parámetros de body: convertir strings a objetos {type: 'text', text: '...'}
if (!empty($bodyParameters)) {
$components[] = [
'type' => 'body',
'parameters' => $bodyParameters
];
$normalizedBody = [];
foreach ($bodyParameters as $bp) {
if (is_array($bp) && isset($bp['type'])) {
$normalizedBody[] = $bp; // ya en formato detallado
} elseif (is_string($bp)) {
$normalizedBody[] = [
'type' => 'text',
'text' => $bp
];
}
}
if (!empty($normalizedBody)) {
$components[] = [
'type' => 'body',
'parameters' => $normalizedBody
];
}
}
$template = [