$bodyParams * @return \Illuminate\Http\Client\Response */ public function sendTemplate($to, $templateName, $lang, array $bodyParams = []) { $payload = [ 'messaging_product' => 'whatsapp', 'to' => $to, 'type' => 'template', 'template' => [ 'name' => $templateName, 'language' => ['code' => $lang], ], ]; if (count($bodyParams) > 0) { $payload['template']['components'] = [[ 'type' => 'body', 'parameters' => array_map(function ($text) { return [ 'type' => 'text', 'text' => $this->sanitizeParam($text), ]; }, array_values($bodyParams)), ]]; } $version = config('whatsapp.api_version', 'v25.0'); $phoneId = config('whatsapp.phone_number_id'); $url = 'https://graph.facebook.com/' . $version . '/' . $phoneId . '/messages'; $response = Http::withToken(config('whatsapp.token')) ->acceptJson() ->asJson() ->post($url, $payload); if (!$response->successful()) { Log::error('WhatsApp Cloud send failed', [ 'to' => $to, 'template' => $templateName, 'status' => $response->status(), 'body' => $response->json() ?: $response->body(), ]); } return $response; } protected function sanitizeParam($text) { $text = trim(preg_replace('/\s+/', ' ', (string) $text)); if ($text === '') { return '-'; } return mb_substr($text, 0, 200); } }