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)) {
|
if (!empty($bodyParameters)) {
|
||||||
$normalizedBody = [];
|
$normalizedBody = [];
|
||||||
foreach ($bodyParameters as $bp) {
|
|
||||||
if (is_array($bp) && isset($bp['type'])) {
|
// Detect associative (named) arrays: if array has string keys
|
||||||
$normalizedBody[] = $bp; // ya en formato detallado
|
$isNamed = false;
|
||||||
} elseif (is_string($bp)) {
|
foreach ($bodyParameters as $k => $v) {
|
||||||
$normalizedBody[] = [
|
if (!is_int($k)) { $isNamed = true; break; }
|
||||||
'type' => 'text',
|
}
|
||||||
'text' => $bp
|
|
||||||
];
|
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)) {
|
if (!empty($normalizedBody)) {
|
||||||
$components[] = [
|
$components[] = [
|
||||||
'type' => 'body',
|
'type' => 'body',
|
||||||
|
|||||||
Reference in New Issue
Block a user