# Test WhatsApp Webhooks Locally

Two things to test: **verification (GET)** and **incoming message (POST)**.

---

## Prerequisites

1. **Database reachable**  
   The backend needs a working DB. If you see:
   ```text
   Can't reach database server at `srv537.hstgr.io:3306`
   ```
   - Check the host/port in `.env` (`DATABASE_URL` or `DB_HOST`/`DB_PORT`).
   - Ensure the DB server is running and your network/firewall allows the connection (e.g. allow your IP for remote DB).

2. **Backend running**  
   After the DB connects:
   ```bash
   cd backend/unified_server
   npm run dev
   ```
   You should see the server listening (e.g. port 5000). Only then run the tests below.

---

## 1. Test verification (GET)

Meta calls this when you save the webhook URL in the dashboard. Your backend must return the `hub.challenge` value.

**Request (browser or curl):**
```text
GET http://localhost:5000/api/v1/webhooks/whatsapp?hub.mode=subscribe&hub.verify_token=YOUR_VERIFY_TOKEN&hub.challenge=hello123
```

- Replace `YOUR_VERIFY_TOKEN` with the value in your `.env`: `WEBHOOK_VERIFY_TOKEN` or `META_WEBHOOK_VERIFY_TOKEN` (e.g. `AfsaraliKhan`).

**Expected:** Response body is `hello123` and status 200. If the token doesn’t match, you get 403.

**Example (PowerShell):**
```powershell
Invoke-WebRequest -Uri "http://localhost:5000/api/v1/webhooks/whatsapp?hub.mode=subscribe&hub.verify_token=AfsaraliKhan&hub.challenge=hello123"
```

---

## 2. Test incoming message (POST)

Simulates Meta sending an incoming text message. Use the script (recommended) or curl/Postman.

### Option A: Script (recommended)

```bash
cd backend/unified_server
node scripts/test_webhook_incoming.js "Your test message"
```

Or with npm:
```bash
npm run test:webhook -- "Your test message"
```

**Expected:** HTTP 200 and `{ STATUS: "success", ... }`. In backend logs look for “Processing text message” and “Text message processed”. Check DB: `messages` and `webhook_logs` tables. In the Inbox UI, open the conversation for the sender number (default `923001234567`) or refresh.

**If you see “Request failed” / ECONNREFUSED:** Backend is not running or not reachable; fix DB and start the server first (see Prerequisites).

### Option B: curl (PowerShell / Git Bash)

```bash
curl -X POST http://localhost:5000/api/v1/webhooks/whatsapp \
  -H "Content-Type: application/json" \
  -d "{\"object\":\"whatsapp_business_account\",\"entry\":[{\"id\":\"1\",\"changes\":[{\"value\":{\"metadata\":{\"phone_number_id\":\"test\"},\"messages\":[{\"from\":\"923001234567\",\"id\":\"wamid.test1\",\"timestamp\":\"1234567890\",\"type\":\"text\",\"text\":{\"body\":\"Hello\"}}]},\"field\":\"messages\"}]}]}"
```

---

## 3. Optional: Test with Meta (real webhook)

1. Expose your local server (e.g. **ngrok**): `ngrok http 5000`.
2. In Meta App Dashboard → WhatsApp → Configuration → Webhook:
   - Callback URL: `https://YOUR_NGROK_URL/api/v1/webhooks/whatsapp`
   - Verify token: same as in `.env`
   - Verify and save; subscribe to **messages**.
3. Send a WhatsApp message to your business number; your backend should receive the POST and process it (same logic as the script).

---

## Quick checklist

- [ ] DB in `.env` is reachable; backend starts without “Can’t reach database server”.
- [ ] GET verification returns the challenge (200) when `hub.verify_token` matches `.env`.
- [ ] POST test script returns 200 and backend logs show message processed; DB has new rows.
- [ ] (Optional) ngrok + Meta webhook URL verified and subscribed; real WhatsApp message triggers the same flow.
