indonusa / wablas
Package for integration wablas
Requires
- php: ^8.1
This package is not auto-updated.
Last update: 2026-01-01 09:04:32 UTC
README
Laravel package untuk integrasi Wablas WhatsApp API: kirim pesan teks (single send) & schedule (buat, ubah, batal). Dibangun di atas Laravel HTTP Client.
Mendukung Laravel 11 / 12, PHP >= 8.1.
Instalasi
1) Pasang via Composer
composer require indonusa/wablas
2) Publish config
php artisan vendor:publish --tag="Indonusa\Wablas\WablasServiceProvider"
Akan menghasilkan file config/wablas.php.
3) Isi kredensial di .env
WABLAS_BASE_URL=https://sby.wablas.com
WABLAS_TOKEN=your_device_token
WABLAS_SECRET_KEY=your_secret_key
WABLAS_TIMEZONE=Asia/Jakarta
# Opsional
WABLAS_TIMEOUT=15
WABLAS_RETRIES=2
WABLAS_RETRY_DELAY_MS=200
WABLAS_DEFAULT_COUNTRY_CODE=62
WABLAS_DEVICE=
Catatan:
- Header
Authorizationakan otomatis di-set sebagaitoken.secret_key. phonegunakan format internasional tanpa tanda “+” (contoh Indonesia:62812xxxxxxx). Paket ini juga menyertakan normalisasi dasar.
Konfigurasi (config/wablas.php)
return [
'base_url' => env('WABLAS_BASE_URL', 'https://sby.wablas.com'),
'token' => env('WABLAS_TOKEN', ''),
'secret_key' => env('WABLAS_SECRET_KEY', ''),
'timezone' => env('WABLAS_TIMEZONE', 'Asia/Jakarta'),
// HTTP & Retry
'timeout' => env('WABLAS_TIMEOUT', 15),
'retries' => env('WABLAS_RETRIES', 2),
'retry_delay_ms' => env('WABLAS_RETRY_DELAY_MS', 200),
// Normalisasi phone
'default_country_code' => env('WABLAS_DEFAULT_COUNTRY_CODE', '62'),
// Device opsional (jika dibutuhkan oleh akun Wablas Anda)
'device' => env('WABLAS_DEVICE'),
];
Quick Start
Kirim pesan teks (single send)
use Indonusa\Wablas\Facades\Wablas;
Wablas::sendText('6281234567890', 'Halo! Pesanan Anda sedang diproses.', [
'ref_id' => 'ORDER-INV-12345',
'isGroup' => false, // juga menerima bool; akan diubah ke "true"/"false"
// 'device' => 'MyDevice01', // opsional
]);
Kirim pesan image
use Indonusa\Wablas\Facades\Wablas;
Wablas::sendImage('6281234567890', 'url_image', [
'caption' => 'contoh caption', // optional
]);
Buat schedule kirim pesan
Wablas::scheduleCreate([
'phone' => '6281234567890',
'message' => 'Reminder janji temu besok, jam 10:00.',
'scheduled_at' => now()->addDay()->setTime(9, 0), // otomatis jadi date+time+timezone
// Atau:
// 'date' => '2025-10-01', 'time' => '09:00:00', 'timezone' => 'Asia/Jakarta',
'isGroup' => false,
]);
Ubah schedule
Wablas::scheduleUpdate('SCHEDULE_ID_DARI_RESPONSE', [
'phone' => '6281234567890',
'message' => 'Reminder diundur pukul 10:00.',
'scheduled_at' => '2025-10-01 10:00:00',
'isGroup' => false,
]);
Batalkan schedule
Wablas::scheduleCancel('SCHEDULE_ID_DARI_RESPONSE');
API Reference
Semua method tersedia melalui Facade Wablas dan juga melalui dependency injection Indonusa\Wablas\Contracts\WablasContract.
1) sendText(string $phone, string $message, array $opts = []): array
Kirim pesan teks sekali jalan.
- \$phone: Nomor tujuan (internasional tanpa “+”). Paket akan melakukan normalisasi ringan.
- \$message: Isi pesan.
\$opts (opsional):
ref_id(string): Referensi internal Anda.isGroup(bool|string): Jika kirim ke group ID; boolean akan dikonversi ke"true"/"false".device(string): Override device per-kiriman.- Field opsional lain yang didukung endpoint Wablas dapat diteruskan melalui
$opts. Nilainullotomatis dibuang.
Return: array hasil json() dari Wablas (atau ['raw' => '...'] bila response non-JSON).
2) scheduleCreate(array $payload): array
Buat jadwal pengiriman pesan.
Payload minimal:
phone(string),message(string),Salah satu:
scheduled_at(Carbon|string|\DateTime), ataudate(Y-m-d),time(H:i:s),timezone(IANA TZ, default config).
Opsional:
isGroup(bool|string),device(string),ref_id(string), dst.
3) scheduleUpdate(string $scheduleId, array $payload): array
Ubah jadwal yang sudah dibuat.
Payload: sama seperti scheduleCreate (boleh scheduled_at atau trio date/time/timezone).
4) scheduleCancel(string $scheduleId): array
Batalkan jadwal kirim.
Dependency Injection
use Indonusa\Wablas\Contracts\WablasContract;
class NotifyOrderShipped
{
public function __construct(private WablasContract $wablas) {}
public function __invoke()
{
$this->wablas->sendText('62812...', 'Order Anda dikirim.');
}
}
Service Provider mengikat WablasContract ke manager internal.
Queue Support (Contoh)
Gunakan Job biasa agar non-blocking:
// app/Jobs/SendWablasText.php
namespace App\Jobs;
use Indonusa\Wablas\Facades\Wablas;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
class SendWablasText implements ShouldQueue
{
use Queueable;
public function __construct(
public string $phone,
public string $message,
public array $opts = []
) {}
public function handle(): void
{
Wablas::sendText($this->phone, $this->message, $this->opts);
}
}
Pemakaian:
dispatch(new \App\Jobs\SendWablasText('62812...', 'Halo dari queue!', ['ref_id' => 'Q-1']));
Laravel Notification (Opsional)
Buat channel custom agar bisa notify():
// app/Notifications/Channels/WablasChannel.php
namespace App\Notifications\Channels;
use Indonusa\Wablas\Facades\Wablas;
class WablasChannel
{
public function send($notifiable, $notification)
{
if (! method_exists($notification, 'toWablas')) {
return;
}
$msg = $notification->toWablas($notifiable); // ['phone'=>'', 'message'=>'', 'opts'=>[]]
Wablas::sendText($msg['phone'], $msg['message'], $msg['opts'] ?? []);
}
}
Di Notification:
public function via($notifiable)
{
return [\App\Notifications\Channels\WablasChannel::class];
}
public function toWablas($notifiable)
{
return [
'phone' => $notifiable->phone,
'message' => "Halo {$notifiable->name}, pesananmu siap diambil.",
];
}
Normalisasi & Keamanan
Normalisasi phone: Paket akan:
- membuang non-digit & “+”,
- jika diawali
0dandefault_country_codediset (mis.62), maka08xxxx→628xxxx.
- Null stripping: Field bernilai
nulldihapus otomatis. - Retry: 5xx/timeout melempar exception; internal retry sederhana tersedia sesuai config.
- Whitelisting (opsional): Anda dapat membatasi key yang dikirim (lihat implementasi
whitelist()di client jika ingin diaktifkan).