# wa-voice-bridge

Microservice Node.js (TypeScript) pour orchestrer les appels WhatsApp:

- recoit un `POST /calls/start` depuis `webhook.php`,
- (optionnel) tente d'accepter l'appel via l'API Meta Calling,
- (optionnel) declenche un originate via ARI vers FreePBX.

> Ce service n'implemente pas encore le media bridge RTP/WebRTC complet.
> Il prepare l'orchestration serveur et la securisation des evenements.

## Installation

```bash
cd wa-voice-bridge
cp .env.example .env
npm install
npm run dev
```

## Endpoints

- `GET /health` : status du service
- `POST /calls/start` : webhook interne appele par `webhook.php`

Payload attendu:

```json
{
  "call_id": "abc123",
  "from": "50931030303",
  "to": "50948900202",
  "event": "connect",
  "direction": "USER_INITIATED",
  "timestamp": "1781291056",
  "contact_name": "M T M"
}
```

## Variables `.env`

- `BRIDGE_TOKEN` : token partage avec `webhook.php` (`X-Bridge-Token`)
- `META_CALL_ACCEPT_URL_TEMPLATE` : endpoint Meta Calling avec `{CALL_ID}`
- `ENABLE_ARI_ORIGINATE=1` pour activer l'originate ARI
- `FREEPBX_TARGET_EXT` : extension cible ARI

## Integration avec webhook.php

Dans `config.php` du projet PHP:

```php
'voice_bridge_endpoint' => 'http://127.0.0.1:8787/calls/start',
'voice_bridge_timeout_seconds' => 2,
'voice_bridge_token' => 'change-me',
```

Le `webhook.php` enverra automatiquement les appels `connect` vers ce service.
