WhatsApp API untuk Kirim Notifikasi Otomatis
Cara kirim notifikasi otomatis via WhatsApp API. Order update, reminder, alert sistem. Contoh kode dan best practices!
Notifikasi WhatsApp = Open rate 98%!
Jauh lebih tinggi dari email (20%) atau SMS (90%).
Dengan WhatsApp API, kamu bisa kirim notifikasi otomatis untuk berbagai keperluan.
Jenis Notifikasi
| Jenis | Contoh |
|---|---|
| Transaksional | Order confirmation, payment received |
| Reminder | Pembayaran, appointment, renewal |
| Alert | Server down, stok habis, security |
| Update | Shipping, status order |
| Marketing | Promo, product launch |
Contoh Implementasi
Order Confirmation
javascript
async function sendOrderNotification(order) {
const message = `✅ ORDER DITERIMA!
Hai ${order.name}!
Order #${order.id}
━━━━━━━━━━━━━━━━
${order.items.map(i => `• ${i.name} x${i.qty}`).join('\n')}
━━━━━━━━━━━━━━━━
Total: Rp ${order.total.toLocaleString()}
Bayar sebelum: ${order.deadline}
Terima kasih! 🙏`;
await sendWhatsApp(order.phone, message);
}Payment Reminder
javascript
async function sendPaymentReminder(order) {
const message = `⏰ REMINDER PEMBAYARAN
Hai ${order.name}!
Order #${order.id} menunggu pembayaran.
Total: Rp ${order.total.toLocaleString()}
Deadline: ${order.deadline}
Transfer ke:
BCA 1234567890
a.n. Toko ABC
Jangan sampai terlewat ya! 😊`;
await sendWhatsApp(order.phone, message);
}
// Trigger: Cron job setiap jam
// Cek order unpaid > 12 jamShipping Update
javascript
async function sendShippingUpdate(order, resi) {
const message = `🚚 PESANAN DIKIRIM!
Hai ${order.name}!
Paketmu sudah jalan! 🎉
No. Resi: ${resi}
Kurir: ${order.courier}
Track: https://cekresi.com/?r=${resi}
Estimasi: 2-3 hari
Ada pertanyaan? Balas chat ini.`;
await sendWhatsApp(order.phone, message);
}System Alert (ke Admin)
javascript
async function sendSystemAlert(alert) {
const adminPhone = '6281234567890';
const message = `🚨 SYSTEM ALERT
Type: ${alert.type}
Severity: ${alert.severity}
Time: ${new Date().toISOString()}
${alert.message}
Action required!`;
await sendWhatsApp(adminPhone, message);
}
// Usage
sendSystemAlert({
type: 'SERVER_DOWN',
severity: 'CRITICAL',
message: 'Web server not responding'
});Best Practices
1. Template untuk Business-Initiated
Cloud API mensyaratkan template message untuk memulai conversation. Buat template di Meta Business Suite terlebih dahulu.
2. Rate Limiting
javascript
const queue = [];
const RATE_LIMIT = 1000; // 1 msg/second
async function sendWithRateLimit(phone, message) {
queue.push({ phone, message });
processQueue();
}
async function processQueue() {
if (queue.length === 0) return;
const { phone, message } = queue.shift();
await sendWhatsApp(phone, message);
setTimeout(processQueue, RATE_LIMIT);
}3. Error Handling & Retry
javascript
async function sendWithRetry(phone, message, maxRetries = 3) {
for (let i = 0; i < maxRetries; i++) {
try {
await sendWhatsApp(phone, message);
return true;
} catch (error) {
console.log(`Retry ${i + 1}/${maxRetries}`);
await sleep(1000 * (i + 1)); // Exponential backoff
}
}
// Log failed message
logFailedMessage(phone, message);
return false;
}4. Logging
javascript
function logNotification(phone, type, status) {
const log = {
timestamp: new Date(),
phone,
type,
status
};
// Save to database/file
saveLog(log);
}Template Notifikasi
E-commerce:
- Order received
- Payment confirmed
- Order shipped
- Order delivered
- Review request
Appointment:
- Booking confirmed
- Reminder (H-1, H-3)
- Reschedule
- Cancellation
Subscription:
- Welcome
- Renewal reminder
- Payment failed
- Expired
Coba No-Code Notification — Gratis! →