# 📱 WhatsApp Message Templates - Documentation

## Overview

Fitur untuk mengkustomisasi header, footer, dan informasi bisnis pada pesan WhatsApp yang dikirim ke agent dan customer.

---

## ✨ Features

### 1. **Custom Header**
- Header yang ditampilkan di awal setiap pesan
- Support ASCII art dan emoji
- Bisa dikosongkan jika tidak diperlukan

### 2. **Custom Footer**
- Footer yang ditampilkan di akhir setiap pesan
- Support dynamic variables
- Informasi kontak dan alamat

### 3. **Business Information**
- Nama bisnis
- Nomor WhatsApp bisnis
- Alamat bisnis

### 4. **Admin Numbers Management**
- Manage nomor admin via web interface
- Validation format nomor
- List nomor terdaftar

### 5. **Message Options**
- Toggle emoji on/off
- Toggle formatting (bold/italic) on/off

### 6. **Live Preview**
- Preview real-time saat edit
- Sample voucher message
- Variable replacement preview

---

## 🎨 Default Templates

### Header
```
╔═══════════════════╗
║  🎫 VOUCHER WIFI  ║
╚═══════════════════╝
```

### Footer
```

━━━━━━━━━━━━━━━━━━━━
📞 Customer Service
WA: {business_phone}
📍 {business_address}

Terima kasih! 🙏
```

---

## 🔧 Setup

### 1. Access Settings Page

Login admin → Menu **Agent/Reseller** → **WhatsApp Agent**

Or direct URL:
```
http://your-domain/mikhmon/?hotspot=whatsapp-agent-settings&session=YOUR_SESSION
```

### 2. Configure Admin Numbers

Format: `628xxx` (without + or spaces)

Multiple numbers separated by comma:
```
628123456789,628987654321,628555666777
```

### 3. Set Business Information

- **Nama Bisnis**: WiFi Hotspot
- **Nomor WhatsApp**: 08123456789
- **Alamat**: Jl. Contoh No. 123

### 4. Customize Header

Example ASCII art:
```
╔═══════════════════╗
║  🎫 VOUCHER WIFI  ║
╚═══════════════════╝
```

Or simple:
```
🎫 VOUCHER WIFI
```

Or empty (no header).

### 5. Customize Footer

Use variables:
- `{business_name}` - Replaced with business name
- `{business_phone}` - Replaced with business phone
- `{business_address}` - Replaced with business address

Example:
```

━━━━━━━━━━━━━━━━━━━━
📞 Customer Service
WA: {business_phone}
📍 {business_address}

Powered by {business_name}
Terima kasih! 🙏
```

### 6. Save Settings

Click **Simpan Pengaturan** button.

---

## 📋 Variables

### Available Variables

| Variable | Description | Example |
|----------|-------------|---------|
| `{business_name}` | Business name | WiFi Hotspot |
| `{business_phone}` | Business phone | 08123456789 |
| `{business_address}` | Business address | Jl. Contoh No. 123 |

### Usage

Variables can be used in **Footer** only.

Click variable tag to insert at cursor position.

---

## 💬 Message Examples

### Before (Without Template)

```
✅ VOUCHER BERHASIL DI-GENERATE

Profile: 3JAM
Jumlah: 5 voucher
Total: Rp 15,000

━━━━━━━━━━━━━━━━━━━━

Voucher #1
Username: AG12AB34CD
Password: XY56ZW

...

💰 Saldo Anda: Rp 85,000
```

### After (With Template)

```
╔═══════════════════╗
║  🎫 VOUCHER WIFI  ║
╚═══════════════════╝

✅ VOUCHER BERHASIL DI-GENERATE

Profile: 3JAM
Jumlah: 5 voucher
Total: Rp 15,000

━━━━━━━━━━━━━━━━━━━━

Voucher #1
Username: AG12AB34CD
Password: XY56ZW

...

💰 Saldo Anda: Rp 85,000

━━━━━━━━━━━━━━━━━━━━
📞 Customer Service
WA: 08123456789
📍 Jl. Contoh No. 123

Terima kasih! 🙏
```

---

## 🎨 Template Ideas

### Minimalist

**Header:**
```
🎫 VOUCHER WIFI
```

**Footer:**
```
━━━━━━━━━━━━━━━━━━━━
CS: {business_phone}
```

---

### Professional

**Header:**
```
╔════════════════════════╗
║   {business_name}      ║
║   VOUCHER INTERNET     ║
╚════════════════════════╝
```

**Footer:**
```

━━━━━━━━━━━━━━━━━━━━━━━━
📞 Customer Service
   {business_phone}

📍 Alamat
   {business_address}

🌐 Website
   www.example.com

Terima kasih atas kepercayaan Anda! 🙏
```

---

### Fun & Friendly

**Header:**
```
🎉 YEAY! VOUCHER KAMU SIAP! 🎉
━━━━━━━━━━━━━━━━━━━━━━━━
```

**Footer:**
```

━━━━━━━━━━━━━━━━━━━━━━━━
💬 Ada kendala?
   Chat kami: {business_phone}

📍 Kunjungi kami:
   {business_address}

Selamat browsing! 🚀✨
```

---

### Corporate

**Header:**
```
┌─────────────────────────┐
│  {business_name}        │
│  INTERNET VOUCHER       │
└─────────────────────────┘
```

**Footer:**
```

┌─────────────────────────┐
│  CUSTOMER SUPPORT       │
├─────────────────────────┤
│  Phone: {business_phone}│
│  Address:               │
│  {business_address}     │
└─────────────────────────┘

© 2024 {business_name}
All rights reserved.
```

---

## 🔧 Technical Implementation

### Database Storage

Settings stored in `agent_settings` table:

| Setting Key | Description |
|-------------|-------------|
| `admin_whatsapp_numbers` | Comma-separated admin numbers |
| `wa_message_header` | Header template |
| `wa_message_footer` | Footer template |
| `wa_business_name` | Business name |
| `wa_business_phone` | Business phone |
| `wa_business_address` | Business address |
| `wa_enable_emoji` | Enable emoji (1/0) |
| `wa_enable_formatting` | Enable formatting (1/0) |

### Message Formatting

Function `formatMessage()` in webhook:

```php
function formatMessage($content) {
    global $messageSettings;
    
    $header = $messageSettings['header'];
    $footer = $messageSettings['footer'];
    
    // Replace variables
    $footer = str_replace('{business_name}', $messageSettings['business_name'], $footer);
    $footer = str_replace('{business_phone}', $messageSettings['business_phone'], $footer);
    $footer = str_replace('{business_address}', $messageSettings['business_address'], $footer);
    
    // Build message
    $message = '';
    if (!empty($header)) {
        $message .= $header . "\n\n";
    }
    $message .= $content;
    if (!empty($footer)) {
        $message .= "\n" . $footer;
    }
    
    return $message;
}
```

### Usage in Webhook

```php
// Before
$reply = "✅ VOUCHER BERHASIL\n...";
sendWhatsAppMessage($phone, $reply);

// After
$content = "✅ VOUCHER BERHASIL\n...";
$reply = formatMessage($content);
sendWhatsAppMessage($phone, $reply);
```

---

## 🧪 Testing

### Test Header/Footer

1. Set custom header and footer
2. Save settings
3. Send WhatsApp command:
   ```
   GEN 3JAM 1
   ```
4. Check received message
5. Verify header and footer appear correctly

### Test Variables

1. Set footer with variables:
   ```
   CS: {business_phone}
   Alamat: {business_address}
   ```
2. Save settings
3. Generate voucher
4. Verify variables replaced with actual values

### Test Admin Numbers

1. Add admin number
2. Save settings
3. Send command from admin number
4. Should work without balance check

---

## 🎯 Best Practices

### Header

✅ **Do:**
- Keep it short (max 3 lines)
- Use clear branding
- Include emoji for visual appeal

❌ **Don't:**
- Make it too long
- Use too many emojis
- Include dynamic content

### Footer

✅ **Do:**
- Include contact information
- Use variables for flexibility
- Add call-to-action
- Keep it professional

❌ **Don't:**
- Make it too long (max 10 lines)
- Include sensitive information
- Use too many links

### Business Info

✅ **Do:**
- Use real contact information
- Keep phone number active
- Update address if moved

❌ **Don't:**
- Use fake information
- Include personal numbers
- Forget to update

---

## 🐛 Troubleshooting

### Header/Footer not showing

**Cause:** Settings not saved or empty

**Solution:**
- Check settings saved successfully
- Verify not empty in database
- Clear cache and retry

---

### Variables not replaced

**Cause:** Wrong variable format or typo

**Solution:**
- Use exact variable names
- Check spelling: `{business_phone}` not `{phone}`
- Variables only work in footer

---

### Preview not updating

**Cause:** JavaScript not running

**Solution:**
- Check browser console for errors
- Refresh page
- Try different browser

---

### Admin numbers not working

**Cause:** Wrong format or not saved

**Solution:**
- Use format 628xxx (no + or spaces)
- Check saved in database
- Verify comma separation

---

## 📱 Mobile Optimization

### WhatsApp Formatting

- Use `*bold*` for emphasis
- Use `_italic_` for subtle text
- Use ``` for code/username
- Use emoji sparingly

### Line Length

- Keep lines under 40 characters
- Use line breaks for readability
- Avoid horizontal scrolling

### Visual Hierarchy

- Use separators (━━━)
- Group related information
- Add spacing between sections

---

## 🔮 Future Enhancements

### Planned
- [ ] Multiple templates (per profile)
- [ ] Template variables for voucher data
- [ ] Rich media support (images)
- [ ] Template preview with actual data
- [ ] Template library/marketplace

### Nice to Have
- [ ] A/B testing templates
- [ ] Template analytics
- [ ] Multi-language templates
- [ ] Seasonal templates
- [ ] Template scheduler

---

## 📚 Related Files

- `agent-admin/whatsapp_agent_settings.php` - Settings page
- `api/whatsapp_agent_webhook.php` - Webhook handler
- `include/menu.php` - Menu integration
- `index.php` - Route handler

---

## 📞 Support

### Common Questions

**Q: Can I use HTML in templates?**  
A: No, WhatsApp doesn't support HTML. Use WhatsApp formatting instead.

**Q: How many admin numbers can I add?**  
A: Unlimited, separated by comma.

**Q: Can I disable header/footer?**  
A: Yes, just leave them empty.

**Q: Do templates apply to all messages?**  
A: Yes, all agent command responses use templates.

**Q: Can I use images in header?**  
A: No, text only. But you can use ASCII art and emoji.

---

**Version:** 1.0  
**Last Updated:** November 2024  
**Status:** ✅ Production Ready
