Files
2026-10-01 15:32:20 +07:00

87 lines
3.0 KiB
PHP

<?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Throwable;
class WhatsAppService
{
private string $baseUrl = 'https://chat.api.co.id';
private string $token = 'apicoid_live_7J0vmW5Sbk2dBBAEIEDtdc4tG4fOswy3Seblz_nKZqk';
/**
* ["19-02-2026"] => "19-02-2026"; 2 tanggal => "a dan b"; 3+ => "a, b, dan c".
*/
private function formatDaftarTanggal(array $daftarTanggal): string
{
$daftarTanggal = array_values($daftarTanggal);
$terakhir = array_pop($daftarTanggal);
if ($daftarTanggal === []) {
return (string) $terakhir;
}
return implode(', ', $daftarTanggal).(count($daftarTanggal) > 1 ? ',' : '')." dan $terakhir";
}
public function sendPengajuanAbsenNotification(
string $nomorTujuan,
string $namaAtasan,
string $namaPemohon,
string $nipPemohon,
int $jumlahHari,
array $daftarTanggal,
string $keterangan,
string $link
): bool {
try {
$response = Http::withToken($this->token)
->post("{$this->baseUrl}/api/v1/public/messages/send", [
'phone_number' => $nomorTujuan,
'channel' => 'whatsapp',
'message_type' => 'template',
'template' => [
'name' => 'verifikasi_presensi_wa',
'language' => ['code' => 'id'],
'components' => [
[
'type' => 'body',
'parameters' => [
['type' => 'text', 'text' => $namaAtasan],
['type' => 'text', 'text' => $namaPemohon],
['type' => 'text', 'text' => $nipPemohon],
['type' => 'text', 'text' => (string) $jumlahHari],
['type' => 'text', 'text' => $this->formatDaftarTanggal($daftarTanggal)],
['type' => 'text', 'text' => $keterangan],
],
],
[
'type' => 'button',
'sub_type' => 'url',
'index' => '0',
'parameters' => [
['type' => 'text', 'text' => $link],
],
],
],
],
]);
} catch (Throwable $th) {
Log::error('WhatsApp sendPengajuanAbsenNotification : '.$th->getMessage());
return false;
}
if (! $response->successful()) {
Log::error("WhatsApp sendPengajuanAbsenNotification gagal: HTTP {$response->status()} - {$response->body()}");
return false;
}
return true;
}
}