feat: WhatsApp promociones — fechas en español, status webhook y analytics inspector
- Corrige formato de fechas a español con Carbon isoFormat('D [de] MMMM')
- Webhook procesa statuses de Meta (delivered/read/failed) y actualiza pivot alumno_promocion
- Analytics: nuevas métricas de entregados/leídos, embudo de entrega, filtro por tipo
- Inspector de payload raw por log con modal y botón copiar
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -31,36 +31,48 @@ class WhatsAppService
|
||||
$this->apiUrl = "https://graph.facebook.com/v19.0/{$this->phoneId}/messages";
|
||||
}
|
||||
|
||||
public function enviarTemplate(string $telefono, string $templateName, array $parametros = []): ?string
|
||||
{
|
||||
$components = [];
|
||||
public function enviarTemplate(string $telefono, string $templateName, array $parametros = [], ?string $imagenUrl = null): ?string
|
||||
{
|
||||
$components = [];
|
||||
|
||||
if (!empty($parametros)) {
|
||||
$components[] = [
|
||||
'type' => 'body',
|
||||
'parameters' => array_map(fn($p) => ['type' => 'text', 'text' => $p], $parametros),
|
||||
];
|
||||
}
|
||||
|
||||
$response = Http::withToken($this->token)->post($this->apiUrl, [
|
||||
'messaging_product' => 'whatsapp',
|
||||
'to' => $this->normalizarTelefono($telefono),
|
||||
'type' => 'template',
|
||||
'template' => [
|
||||
'name' => $templateName,
|
||||
'language' => ['code' => 'es_MX'],
|
||||
'components' => $components,
|
||||
if ($imagenUrl) {
|
||||
$components[] = [
|
||||
'type' => 'header',
|
||||
'parameters' => [
|
||||
[
|
||||
'type' => 'image',
|
||||
'image' => ['link' => $imagenUrl],
|
||||
]
|
||||
],
|
||||
]);
|
||||
|
||||
if ($response->failed()) {
|
||||
Log::error('WhatsApp template error', ['telefono' => $telefono, 'response' => $response->json()]);
|
||||
return null;
|
||||
}
|
||||
|
||||
return $response->json('messages.0.id');
|
||||
];
|
||||
}
|
||||
|
||||
if (!empty($parametros)) {
|
||||
$components[] = [
|
||||
'type' => 'body',
|
||||
'parameters' => array_map(fn($p) => ['type' => 'text', 'text' => $p], $parametros),
|
||||
];
|
||||
}
|
||||
|
||||
$response = Http::withToken($this->token)->post($this->apiUrl, [
|
||||
'messaging_product' => 'whatsapp',
|
||||
'to' => $this->normalizarTelefono($telefono),
|
||||
'type' => 'template',
|
||||
'template' => [
|
||||
'name' => $templateName,
|
||||
'language' => ['code' => 'es_MX'],
|
||||
'components' => $components,
|
||||
],
|
||||
]);
|
||||
|
||||
if ($response->failed()) {
|
||||
Log::error('WhatsApp template error', ['telefono' => $telefono, 'response' => $response->json()]);
|
||||
return null;
|
||||
}
|
||||
|
||||
return $response->json('messages.0.id');
|
||||
}
|
||||
|
||||
/*public function enviarPromocion(string $telefono, Promocion $promocion): string
|
||||
{
|
||||
$payload = [
|
||||
@@ -94,23 +106,27 @@ class WhatsAppService
|
||||
return $response->json('messages.0.id');
|
||||
}*/
|
||||
|
||||
public function enviarPromocion(string $telefono, Promocion $promocion): string
|
||||
public function enviarPromocion(string $telefono, Promocion $promocion, string $nombre = ''): string
|
||||
{
|
||||
$response = Http::withToken($this->token)->post($this->apiUrl, [
|
||||
'messaging_product' => 'whatsapp',
|
||||
'to' => $this->normalizarTelefono($telefono),
|
||||
'type' => 'template',
|
||||
'template' => [
|
||||
'name' => 'hello_world',
|
||||
'language' => ['code' => 'en_US'],
|
||||
],
|
||||
]);
|
||||
$codigo = 'PROMO-' . strtoupper(substr(md5($promocion->id . $telefono), 0, 6));
|
||||
|
||||
if ($response->failed()) {
|
||||
throw new \RuntimeException($response->body());
|
||||
$mensajeId = $this->enviarTemplate(
|
||||
$telefono,
|
||||
'promocion_laravel',
|
||||
[
|
||||
$nombre,
|
||||
$promocion->fecha_inicio->locale('es')->isoFormat('D [de] MMMM'),
|
||||
$promocion->fecha_fin->locale('es')->isoFormat('D [de] MMMM'),
|
||||
$codigo,
|
||||
],
|
||||
$promocion->imagenUrl()
|
||||
);
|
||||
|
||||
if (!$mensajeId) {
|
||||
throw new \RuntimeException('No se pudo enviar la plantilla');
|
||||
}
|
||||
|
||||
return $response->json('messages.0.id');
|
||||
return $mensajeId;
|
||||
}
|
||||
|
||||
public function procesarOptOut(string $telefono): void
|
||||
|
||||
Reference in New Issue
Block a user