Files
verifikator/app/Services/WhatsAppService.php
T
2026-10-01 15:24:39 +07:00

73 lines
2.6 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';
public function sendPengajuanAbsenNotification(
string $nomorTujuan,
string $namaAtasan,
string $namaPemohon,
string $nipPemohon,
int $jumlahHari,
string $tanggalMulai,
string $tanggalSelesai,
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' => $tanggalMulai === $tanggalSelesai ? $tanggalMulai : "$tanggalMulai dan $tanggalSelesai"],
['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;
}
}