1
0
Files
services_core/app/Services/Notification/Adapters/WhatsAppUnofficialAdapter.php
T
2026-07-31 13:57:11 +07:00

35 lines
1.4 KiB
PHP

<?php
namespace App\Services\Notification\Adapters;
use App\Models\NotificationDelivery;
use App\Services\Notification\Contracts\NotificationChannelAdapter;
use Illuminate\Support\Facades\Http;
use Illuminate\Validation\ValidationException;
class WhatsAppUnofficialAdapter implements NotificationChannelAdapter
{
public function send(NotificationDelivery $delivery): array
{
$config = $delivery->channelConfig;
$credentials = $config?->credentials ?? [];
$settings = $config?->settings ?? [];
$url = $settings['send_url'] ?? null;
if (! $url) {
throw ValidationException::withMessages(['channel' => 'URL pengiriman gateway unofficial belum diatur.']);
}
$headers = [];
if ($apiKey = $credentials['api_key'] ?? null) {
$headers[$settings['api_key_header'] ?? 'X-API-Key'] = $apiKey;
}
$payload = [
$settings['destination_field'] ?? 'number' => $delivery->destination,
$settings['message_field'] ?? 'message' => $delivery->rendered_body,
'instance_id' => $credentials['instance_id'] ?? null,
];
$response = Http::withHeaders($headers)->timeout(30)->post($url, $payload)->throw()->json();
return ['provider_message_id' => data_get($response, $settings['message_id_path'] ?? 'id'), 'response' => $response];
}
}