up
up
This commit is contained in:
@@ -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 = [
|
||||
|
||||
Reference in New Issue
Block a user