cambios importantes
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
class WhatsAppSender
|
||||
{
|
||||
private const API_VERSION = 'v18.0';
|
||||
private const BASE_URL = 'https://graph.facebook.com';
|
||||
|
||||
public static function sendText(string $to, string $text, string $phoneNumberId): array
|
||||
{
|
||||
return self::callApi($phoneNumberId, [
|
||||
'messaging_product' => 'whatsapp',
|
||||
'recipient_type' => 'individual',
|
||||
'to' => $to,
|
||||
'type' => 'text',
|
||||
'text' => ['body' => $text],
|
||||
]);
|
||||
}
|
||||
|
||||
public static function sendTemplate(string $to, string $templateName, string $phoneNumberId, array $components = []): array
|
||||
{
|
||||
$payload = [
|
||||
'messaging_product' => 'whatsapp',
|
||||
'recipient_type' => 'individual',
|
||||
'to' => $to,
|
||||
'type' => 'template',
|
||||
'template' => [
|
||||
'name' => $templateName,
|
||||
'language' => ['code' => 'es'],
|
||||
],
|
||||
];
|
||||
|
||||
if (!empty($components)) {
|
||||
$payload['template']['components'] = $components;
|
||||
}
|
||||
|
||||
return self::callApi($phoneNumberId, $payload);
|
||||
}
|
||||
|
||||
public static function sendImage(string $to, string $mediaIdOrUrl, string $phoneNumberId, ?string $caption = null): array
|
||||
{
|
||||
$payload = [
|
||||
'messaging_product' => 'whatsapp',
|
||||
'recipient_type' => 'individual',
|
||||
'to' => $to,
|
||||
'type' => 'image',
|
||||
'image' => [
|
||||
(str_starts_with($mediaIdOrUrl, 'http') ? 'link' : 'id') => $mediaIdOrUrl,
|
||||
],
|
||||
];
|
||||
|
||||
if ($caption !== null) {
|
||||
$payload['image']['caption'] = $caption;
|
||||
}
|
||||
|
||||
return self::callApi($phoneNumberId, $payload);
|
||||
}
|
||||
|
||||
public static function sendInteractive(string $to, array $interactive, string $phoneNumberId): array
|
||||
{
|
||||
return self::callApi($phoneNumberId, [
|
||||
'messaging_product' => 'whatsapp',
|
||||
'recipient_type' => 'individual',
|
||||
'to' => $to,
|
||||
'type' => 'interactive',
|
||||
'interactive' => $interactive,
|
||||
]);
|
||||
}
|
||||
|
||||
private static function callApi(string $phoneNumberId, array $payload): array
|
||||
{
|
||||
$url = self::BASE_URL . '/' . self::API_VERSION . '/' . $phoneNumberId . '/messages';
|
||||
$token = env('WHATSAPP_ACCESS_TOKEN', '');
|
||||
|
||||
if ($token === '') {
|
||||
return ['success' => false, 'error' => 'WHATSAPP_ACCESS_TOKEN no configurado'];
|
||||
}
|
||||
|
||||
$ch = curl_init($url);
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_POSTFIELDS => json_encode($payload),
|
||||
CURLOPT_HTTPHEADER => [
|
||||
'Authorization: Bearer ' . $token,
|
||||
'Content-Type: application/json',
|
||||
],
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_TIMEOUT => 15,
|
||||
]);
|
||||
|
||||
$response = curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
$error = curl_error($ch);
|
||||
curl_close($ch);
|
||||
|
||||
$decoded = $response ? json_decode($response, true) : null;
|
||||
|
||||
return [
|
||||
'success' => $httpCode >= 200 && $httpCode < 300,
|
||||
'http_code' => $httpCode,
|
||||
'response' => $decoded ?? $response,
|
||||
'error' => $error ?: ($decoded['error']['message'] ?? null),
|
||||
'wam_id' => $decoded['messages'][0]['id'] ?? null,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user