Update WhatsAppService.php
This commit is contained in:
@@ -107,19 +107,48 @@ class WhatsAppService
|
||||
}
|
||||
}
|
||||
|
||||
// Normalizar parámetros de body: convertir strings a objetos {type: 'text', text: '...'}
|
||||
// Normalizar parámetros de body: soportar tres formatos:
|
||||
// 1) Indexed array of strings: ['Alice','+123'] => converted to text params in order
|
||||
// 2) Indexed array of param objects: [['type'=>'text','text'=>'Alice'], ...] => used as-is
|
||||
// 3) Associative array for NAMED params: ['name' => 'Alice'] => converted to {type:text, name: 'name', text: 'Alice'}
|
||||
if (!empty($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
|
||||
];
|
||||
|
||||
// Detect associative (named) arrays: if array has string keys
|
||||
$isNamed = false;
|
||||
foreach ($bodyParameters as $k => $v) {
|
||||
if (!is_int($k)) { $isNamed = true; break; }
|
||||
}
|
||||
|
||||
if ($isNamed) {
|
||||
// bodyParameters is like ['name' => 'Alice', ...]
|
||||
foreach ($bodyParameters as $paramName => $paramValue) {
|
||||
if (is_array($paramValue) && isset($paramValue['type'])) {
|
||||
// already detailed param object: ensure name is set
|
||||
if (!isset($paramValue['name'])) $paramValue['name'] = $paramName;
|
||||
$normalizedBody[] = $paramValue;
|
||||
} else {
|
||||
$normalizedBody[] = [
|
||||
'type' => 'text',
|
||||
'name' => $paramName,
|
||||
'text' => (string)$paramValue
|
||||
];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Indexed array: preserve objects or convert strings
|
||||
foreach ($bodyParameters as $bp) {
|
||||
if (is_array($bp) && isset($bp['type'])) {
|
||||
$normalizedBody[] = $bp; // already detailed
|
||||
} elseif (is_string($bp) || is_numeric($bp)) {
|
||||
$normalizedBody[] = [
|
||||
'type' => 'text',
|
||||
'text' => (string)$bp
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($normalizedBody)) {
|
||||
$components[] = [
|
||||
'type' => 'body',
|
||||
|
||||
Reference in New Issue
Block a user