# 🚀 WhatsApp Self-Hosted Gateway untuk MikhMon

## ✅ Instalasi Selesai!

Sistem WhatsApp Gateway self-hosted telah berhasil dibuat dan siap digunakan!

---

## 🎯 Apa yang Sudah Dibuat?

### 1. **WhatsApp Gateway Service** (Node.js + Baileys)
Layanan WhatsApp Gateway yang berjalan di Node.js menggunakan library Baileys.

**Lokasi:** `whatsapp-gateway/`

**Fitur:**
- ✅ REST API untuk kirim pesan
- ✅ QR Code authentication
- ✅ Session management (auto-reconnect)
- ✅ Message queue dengan anti-spam delay
- ✅ Webhook untuk incoming messages
- ✅ Database logging
- ✅ PM2 support untuk production

### 2. **Admin Panel** (PHP)
Panel admin untuk monitoring dan kontrol WhatsApp Gateway.

**Lokasi:** `settings/whatsapp_gateway_admin.php`

**Fitur:**
- ✅ Connection status real-time
- ✅ QR Code display untuk scan
- ✅ Test message
- ✅ Logout & Restart buttons
- ✅ Auto-refresh jika disconnected

### 3. **Integration dengan MikhMon**
Integrasi lengkap dengan sistem MikhMon yang sudah ada.

**File yang diupdate:**
- ✅ `include/whatsapp_config.php` - Support selfhosted gateway
- ✅ `include/menu.php` - Menu baru "WhatsApp Gateway"
- ✅ `index.php` - Route baru untuk admin panel

### 4. **Dokumentasi Lengkap**
Dokumentasi super lengkap dalam bahasa Indonesia.

**File dokumentasi:**
- ✅ `WHATSAPP_GATEWAY_INSTALLED.txt` - Panduan instalasi
- ✅ `WHATSAPP_SELFHOSTED_GUIDE.md` - Panduan lengkap
- ✅ `WHATSAPP_SELFHOSTED_QUICKSTART.md` - Quick start 5 menit
- ✅ `WHATSAPP_SELFHOSTED_SUMMARY.txt` - Summary
- ✅ `WHATSAPP_GATEWAY_ARCHITECTURE.txt` - Arsitektur sistem
- ✅ `WHATSAPP_GATEWAY_INDEX.md` - Index dokumentasi
- ✅ `README_WHATSAPP_GATEWAY.md` - File ini

### 5. **Auto Installer**
Script installer otomatis untuk Windows dan Linux.

**File:**
- ✅ `whatsapp-gateway/install.bat` - Windows installer
- ✅ `whatsapp-gateway/install.sh` - Linux/macOS installer

---

## 🚀 Cara Mulai (Quick Start)

### 1. Install Node.js
**Windows:**
- Download: https://nodejs.org/
- Install versi LTS
- Restart command prompt

**Linux (Ubuntu/Debian):**
```bash
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs
```

### 2. Install Dependencies
**Windows:**
```cmd
cd whatsapp-gateway
install.bat
```

**Linux:**
```bash
cd whatsapp-gateway
chmod +x install.sh
./install.sh
```

### 3. Konfigurasi API Key
1. Buka file: `whatsapp-gateway/.env`
2. Copy API_KEY yang ada
3. Buka file: `include/whatsapp_config.php`
4. Paste API_KEY di `SELFHOSTED_API_KEY`

### 4. Start Gateway
```bash
cd whatsapp-gateway
npm start
```

### 5. Scan QR Code
1. Buka browser: http://localhost/mikhmon/
2. Login ke MikhMon
3. Klik menu: **Settings → WhatsApp Gateway**
4. Tunggu QR code muncul (10-30 detik)
5. Scan dengan WhatsApp di HP Anda

### 6. Selesai! ✅
Status akan berubah menjadi "Connected" dan siap digunakan!

---

## 📖 Dokumentasi

### Untuk Pemula
**Mulai dari sini:**
1. [WHATSAPP_GATEWAY_INSTALLED.txt](WHATSAPP_GATEWAY_INSTALLED.txt) - Panduan instalasi step by step
2. [WHATSAPP_SELFHOSTED_QUICKSTART.md](WHATSAPP_SELFHOSTED_QUICKSTART.md) - Quick start 5 menit

### Dokumentasi Lengkap
**Untuk detail lebih lanjut:**
- [WHATSAPP_SELFHOSTED_GUIDE.md](WHATSAPP_SELFHOSTED_GUIDE.md) - Panduan lengkap (instalasi, konfigurasi, troubleshooting)
- [WHATSAPP_SELFHOSTED_SUMMARY.txt](WHATSAPP_SELFHOSTED_SUMMARY.txt) - Ringkasan fitur & cara pakai

### Technical Documentation
**Untuk developer:**
- [WHATSAPP_GATEWAY_ARCHITECTURE.txt](WHATSAPP_GATEWAY_ARCHITECTURE.txt) - Arsitektur sistem
- [whatsapp-gateway/README.md](whatsapp-gateway/README.md) - Gateway service docs
- [WHATSAPP_GATEWAY_INDEX.md](WHATSAPP_GATEWAY_INDEX.md) - Index semua dokumentasi

---

## 🎯 Cara Penggunaan

### 1. Kirim Voucher via WhatsApp
1. Login ke MikhMon
2. Buka: **Hotspot → Generate Users**
3. Pilih profile & jumlah
4. Centang **"Kirim ke WhatsApp"**
5. Input nomor customer
6. Klik **Generate**
7. Voucher otomatis terkirim! ✅

### 2. Bot WhatsApp untuk Customer
Customer bisa chat ke nomor WhatsApp yang terhubung:

**Cek Harga:**
```
HARGA
```

**Beli Voucher:**
```
BELI 1JAM
```

**Bantuan:**
```
HELP
```

### 3. Test Kirim Pesan
1. Buka admin panel: **Settings → WhatsApp Gateway**
2. Scroll ke **"Test Message"**
3. Input nomor & pesan
4. Klik **"Send Test Message"**
5. Cek WhatsApp! ✅

---

## 🔧 Production Setup

Untuk production, gunakan PM2 agar gateway jalan terus:

```bash
# Install PM2
npm install -g pm2

# Start dengan PM2
cd whatsapp-gateway
npm run pm2

# Check status
pm2 status

# View logs
pm2 logs mikhmon-wa-gateway

# Auto-start on boot
pm2 startup
pm2 save
```

---

## 📊 Admin Panel

**URL:** http://localhost/mikhmon/settings/whatsapp_gateway_admin.php

**Akses:**
1. Login ke MikhMon
2. Klik menu: **Settings → WhatsApp Gateway**

**Fitur:**
- ✅ Connection status (Connected/Disconnected)
- ✅ Phone number yang terhubung
- ✅ QR Code untuk scan ulang
- ✅ Test message
- ✅ Logout & Restart buttons
- ✅ Auto-refresh jika disconnected

---

## 🐛 Troubleshooting

### QR Code tidak muncul?
```bash
pm2 restart mikhmon-wa-gateway
# Tunggu 10-30 detik
```

### Pesan tidak terkirim?
```bash
# Cek status
curl http://localhost:3000/api/status

# Pastikan connected: true
# Pastikan API key sama di .env dan whatsapp_config.php
```

### Gateway crash?
```bash
# Lihat error
pm2 logs mikhmon-wa-gateway

# Restart
pm2 restart mikhmon-wa-gateway
```

**Detail troubleshooting:** Lihat [WHATSAPP_SELFHOSTED_GUIDE.md](WHATSAPP_SELFHOSTED_GUIDE.md#troubleshooting)

---

## 💰 Keuntungan Self-Hosted

✅ **GRATIS** - Tidak perlu bayar bulanan ke pihak ketiga  
✅ **UNLIMITED** - Tidak ada batasan jumlah pesan  
✅ **PRIVACY** - Data tidak lewat pihak ketiga  
✅ **CEPAT** - Langsung dari server Anda  
✅ **FULL CONTROL** - Customize sesuka hati  
✅ **MONITORING** - Admin panel lengkap  

**Hemat ratusan ribu per bulan!** 💸

---

## 📁 Struktur File

```
mikhmon/
├── whatsapp-gateway/              # Gateway Service (NEW!)
│   ├── package.json
│   ├── .env.example
│   ├── server.js
│   ├── install.sh
│   ├── install.bat
│   └── src/
│       ├── whatsapp.js
│       ├── messageHandler.js
│       ├── routes.js
│       ├── database.js
│       └── logger.js
│
├── settings/
│   └── whatsapp_gateway_admin.php # Admin Panel (NEW!)
│
├── include/
│   ├── whatsapp_config.php        # Updated (selfhosted support)
│   └── menu.php                   # Updated (menu baru)
│
├── index.php                      # Updated (route baru)
│
└── Documentation/
    ├── WHATSAPP_GATEWAY_INSTALLED.txt
    ├── WHATSAPP_SELFHOSTED_GUIDE.md
    ├── WHATSAPP_SELFHOSTED_QUICKSTART.md
    ├── WHATSAPP_SELFHOSTED_SUMMARY.txt
    ├── WHATSAPP_GATEWAY_ARCHITECTURE.txt
    ├── WHATSAPP_GATEWAY_INDEX.md
    └── README_WHATSAPP_GATEWAY.md (this file)
```

---

## 🔌 API Endpoints

**Base URL:** http://localhost:3000

| Method | Endpoint | Description |
|--------|----------|-------------|
| GET | /health | Health check |
| GET | /api/status | Connection status |
| GET | /api/qr | Get QR code |
| POST | /api/send | Send single message |
| POST | /api/send-bulk | Send bulk messages |
| POST | /api/logout | Logout WhatsApp |
| POST | /api/restart | Restart connection |
| GET | /api/logs | Get message logs |

**Authentication:** Header `X-API-Key: your-secret-api-key`

---

## 📞 Support

### Dokumentasi
- Quick Start: [WHATSAPP_SELFHOSTED_QUICKSTART.md](WHATSAPP_SELFHOSTED_QUICKSTART.md)
- Full Guide: [WHATSAPP_SELFHOSTED_GUIDE.md](WHATSAPP_SELFHOSTED_GUIDE.md)
- Index: [WHATSAPP_GATEWAY_INDEX.md](WHATSAPP_GATEWAY_INDEX.md)

### Logs
- Gateway: `pm2 logs mikhmon-wa-gateway`
- Messages: `mikhmon/logs/whatsapp_log.txt`
- Database: `wa_message_logs` table

### Admin Panel
- URL: http://localhost/mikhmon/settings/whatsapp_gateway_admin.php
- Features: Status, QR Code, Test Message, Control

---

## 🎉 Next Steps

1. ✅ Install Node.js
2. ✅ Run installer (`install.bat` / `install.sh`)
3. ✅ Edit konfigurasi API key
4. ✅ Start gateway (`npm start`)
5. ✅ Scan QR code (admin panel)
6. ✅ Test kirim pesan
7. ✅ Setup PM2 untuk production
8. ✅ Monitor via admin panel
9. ✅ Enjoy unlimited WhatsApp! 🚀

---

## 📄 License

GPL-2.0 - Same as MikhMon

---

## 👨‍💻 Author

MikhMon Team

---

## 🌟 Features

- ✅ Self-hosted (no third-party)
- ✅ Unlimited messages
- ✅ Multi-device support
- ✅ QR Code authentication
- ✅ Auto-reconnect
- ✅ Message queue
- ✅ Anti-spam delay
- ✅ Webhook support
- ✅ Bot commands
- ✅ Database logging
- ✅ Admin panel
- ✅ PM2 support
- ✅ Docker ready
- ✅ REST API
- ✅ Full documentation

---

**Version:** 1.0.0  
**Release:** 2024

**Happy Messaging! 🚀**
