diff --git a/services/WhatsAppService.php b/services/WhatsAppService.php index ee5d864..6f8c792 100644 --- a/services/WhatsAppService.php +++ b/services/WhatsAppService.php @@ -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',