A production-ready WhatsApp bot built using NestJS and CQRS architecture. It features persistent conversation history with MongoDB, high-performance caching via Redis, direct Meta Graph API integration, and an advanced conversational experience powered by various Language Models.
- ✅ CQRS Architecture - Clean command/query separation for maintainable logic.
- ✅ MongoDB Integration - Persistent conversations and user state tracking.
- ✅ Redis Caching - Fast message retrieval with robust DB fallbacks.
- ✅ LLM Integration - Multi-provider support (OpenAI-compatible APIs, Anthropic, Gemini, Vertex AI, Ajarsaka).
- ✅ WhatsApp Business API - Fully integrated with Meta's Graph API, including enhanced webhook signature verification and raw body handling.
- ✅ Production Ready - Comprehensive error handling, logging, health checks, and Dockerized deployments.
- ✅ Interactive Commands - Support for
/clear,/helpdirectly within the WhatsApp interface.
The platform is actively evolving. The following features are considered "Missing" or "Under Active Development":
- 🟡 Real-Time VoIP Voice Calling (Gemini Live)
- Current Status: An advanced WebRTC pipeline using
weriftand@discordjs/opushas been introduced to intercept WhatsApp SDP offers and bridge VoIP calls directly to Gemini Live multimodal websockets. - Missing Elements: Handling call failures, edge-cases in signaling/ice-gathering delays, dynamic network degradation adjustments, packet-loss concealment, and graceful connection teardown. The feature is functional but experimental.
- Current Status: An advanced WebRTC pipeline using
- 🟡 Model Context Protocol (MCP) Tools
- Tool calling within the voice and text pipelines to fetch dynamic data (e.g., executing backend actions dynamically during a live Gemini conversation). The foundational
mcp-tools.service.tsis staged but requires broader integration.
- Tool calling within the voice and text pipelines to fetch dynamic data (e.g., executing backend actions dynamically during a live Gemini conversation). The foundational
- 🟡 Rich Media Broadcasting & Analytics
- Advanced analytics dashboard to monitor token usage, track interaction paths, and measure webhook latency.
- 🟡 Comprehensive Automated Testing
- e2e tests covering the complex asynchronous WebSocket and WebRTC lifecycle events.
Copy and configure environment variables based on your setup:
cp .env.example .env
# Edit .env with your specific credentialsnpm installSpin up MongoDB and Redis using Docker (or point to cloud instances like MongoDB Atlas / Redis Cloud):
# Start MongoDB & Redis via docker-compose or isolated docker run
npm run docker:dev
# Alternatively manually:
# docker run -d --name mongodb -p 27017:27017 mongo:latest
# docker run -d --name redis -p 6379:6379 redis:alpine- Set up the Meta Business App.
- Retrieve the Phone Number ID and Access Token.
- Configure the webhook URL in Meta:
https://yourdomain.com/whatsapp/webhook - Make sure you set the expected verify token in
.env.
The system supports multiple backends. Sample configuration:
# Database
MONGO_URI=mongodb://localhost:27017/whatsapp-bot
REDIS_HOST=localhost
# WhatsApp
META_ACCESS_TOKEN=your-access-token
PHONE_NUMBER_ID=your-phone-id
WEBHOOK_VERIFY_TOKEN=your-verify-token
# LLM Examples
LLM_BASE_URL=https://api.openai.com/v1
LLM_API_KEY=sk-your-api-key
LLM_MODEL=gpt-3.5-turbo
GEMINI_API_KEY=your-gemini-key# Development
npm run start:dev
# Production
npm run build
npm run start:prodGET /whatsapp/webhook- WhatsApp webhook payload verification.POST /whatsapp/webhook- Core endpoint processing incoming messages and calls.GET /whatsapp/health- Standard health check validation.
┌─────────────────┐ ┌──────────────┐ ┌─────────────┐
│ WhatsApp │───▶│ Controller │───▶│ Command │
│ Webhook / Calls │ │ │ │ Handler │
└─────────────────┘ └──────────────┘ └─────────────┘
│
┌─────────────┐ ┌─────────────┐
│ Redis │◀──▶│ LLM / AI │
│ (Cache) │ │ Services │
└─────────────┘ └─────────────┘
│
┌─────────────┐ ┌─────────────┐
│ MongoDB │◀──▶│ WhatsApp │
│(Persistence)│ │ Outbound │
└─────────────┘ └─────────────┘
- Inbound Processing → Webhook verification succeeds, parsing text or SDP (for calls).
- State Management → Identify ongoing sessions or check Redis Cache (fallback to MongoDB).
- Execution → Messages sent to unified LLM pipeline; calls routed to WebRTC logic bridging Opus audio to Gemini Live.
- Outbound Responses → Generated text sent via WhatsApp Graph API; audio streams paced and dispatched as RTP packets via the active transceiver.
- Ensure
NODE_ENV=productionis initialized. - Establish robust external Managed Databases (Atlas / ElastiCache).
- Configure logging verbosity:
LOG_LEVEL=warn. - Run through a process manager or direct container deployment:
# PM2 Setup pm2 start dist/main.js --name wa-bot # Docker (Using included configuration) docker build -t wa-bot . docker run -d --env-file .env -p 3000:3000 wa-bot
- Webhook failures: Double-check your
WEBHOOK_VERIFY_TOKENmatches the Meta dashboard exactly. Verify that signature validations are passing. - Messages aren't working: Check outbound logic/logs for LLM ratelimits or database connection snags.
- Audio / Call Dropouts: Voice calling relies on UDP WebRTC traffic. Make sure your server doesn't block UDP ICE candidates, and that node process latency stays minimal.
- Debug Logging: Run the application with
LOG_LEVEL=debug npm run start:devto expose detailed process tracks.