WhatsApp AI chatbot powered by Claude Sonnet 4 (Anthropic API) and Twilio WhatsApp API. Built with Node.js, TypeScript, Express.js, PostgreSQL, and Redis.
- π€ AI-powered conversations using Claude Sonnet 4
- π¬ WhatsApp integration via Twilio
- ποΈ PostgreSQL for data persistence
- β‘ Redis for conversation context caching
- ποΈ Clean Architecture & Domain-Driven Design (DDD)
- π Security-first approach (Helmet, CORS, rate limiting)
- π Structured logging with Winston
- π§ͺ Comprehensive testing (Jest, Supertest)
- π³ Docker support for development and production
This project follows Clean Code and Domain-Driven Design (DDD) principles:
βββββββββββββββββββββββββββββββ
β Presentation Layer β β Controllers (HTTP handling)
βββββββββββββββ¬ββββββββββββββββ
βΌ
βββββββββββββββββββββββββββββββ
β Application Layer β β Services (use cases, orchestration)
βββββββββββββββ¬ββββββββββββββββ
βΌ
βββββββββββββββββββββββββββββββ
β Domain Layer β β Entities, Value Objects (business logic)
βββββββββββββββ¬ββββββββββββββββ
βΌ
βββββββββββββββββββββββββββββββ
β Infrastructure Layer β β Repositories, External APIs
βββββββββββββββββββββββββββββββ
src/
βββ config/ # Configuration (env validation, DB, Redis)
βββ controllers/ # HTTP request handlers (thin)
βββ services/ # Business logic orchestration
βββ repositories/ # Data access layer
βββ middleware/ # Express middleware (error, validation, rate limit)
βββ utils/ # Utilities (logger, errors, helpers)
βββ types/ # TypeScript type definitions
βββ routes/ # Route definitions
βββ app.ts # Express app setup
βββ server.ts # Server entry point
- Node.js: 20+ (LTS)
- pnpm: 8+
- PostgreSQL: 15+
- Redis: 7+
- Docker (optional, for development)
-
Clone the repository
git clone <repository-url> cd chatbot-whatsapp
-
Install dependencies
pnpm install
-
Setup environment variables
cp .env.example .env
Edit
.envand fill in your credentials:- Database URL (PostgreSQL)
- Redis URL
- Twilio credentials (Account SID, Auth Token, Phone Number)
- Anthropic API key
-
Start Docker services (PostgreSQL + Redis)
docker-compose up -d postgres redis
-
Run database migrations
pnpm prisma:migrate pnpm prisma:generate
-
Start development server
pnpm dev
The server will start on http://localhost:3001
pnpm dev # Start development server with hot reload
pnpm build # Build TypeScript to JavaScript
pnpm start # Start production serverpnpm lint # Run ESLint
pnpm lint:fix # Fix ESLint errors
pnpm format # Format code with Prettier
pnpm format:check # Check code formattingpnpm prisma:generate # Generate Prisma Client
pnpm prisma:migrate # Run migrations (development)
pnpm prisma:studio # Open Prisma Studio GUI
pnpm prisma:deploy # Deploy migrations (production)pnpm test # Run all tests
pnpm test:watch # Run tests in watch mode
pnpm test:coverage # Run tests with coverage report| Variable | Description | Required | Example |
|---|---|---|---|
NODE_ENV |
Environment mode | No | development |
PORT |
Server port | No | 3001 |
DATABASE_URL |
PostgreSQL connection string | Yes | postgresql://user:pass@localhost:5432/db |
REDIS_URL |
Redis connection string | Yes | redis://localhost:6379 |
TWILIO_ACCOUNT_SID |
Twilio Account SID | Yes | ACxxxxxxxxxxxxxxxx |
TWILIO_AUTH_TOKEN |
Twilio Auth Token | Yes | your_auth_token |
TWILIO_PHONE_NUMBER |
Twilio WhatsApp number | Yes | whatsapp:+14155238886 |
ANTHROPIC_API_KEY |
Claude API key | Yes | sk-ant-xxxxx |
LOG_LEVEL |
Logging level | No | info |
# Start all services (app, postgres, redis)
docker-compose up -d
# View logs
docker-compose logs -f app
# Stop services
docker-compose downdocker-compose -f docker-compose.prod.yml up -dGET /health
Returns application health status:
{
"status": "ok",
"timestamp": "2024-01-01T00:00:00.000Z",
"uptime": 3600,
"environment": "development",
"version": "1.0.0",
"checks": {
"server": { "status": "ok" },
"memory": {
"status": "ok",
"usage": {
"rss": "50MB",
"heapTotal": "25MB",
"heapUsed": "20MB"
}
}
}
}POST /webhook/whatsapp
Receives incoming WhatsApp messages from Twilio (to be implemented).
Run tests with:
pnpm testCoverage goals:
- Global: 70%
- Services: 80%
- Controllers: 70%
- β Helmet.js for secure HTTP headers
- β CORS configuration
- β Rate limiting (Redis-based)
- β Input validation (Zod)
- β Twilio webhook signature validation
- β Environment variable validation
- β No secrets in code
- Runtime: Node.js 20+
- Language: TypeScript 5+
- Framework: Express.js 4
- AI: Claude Sonnet 4 (Anthropic API)
- Messaging: Twilio WhatsApp API
- Database: PostgreSQL 15+ (Prisma ORM)
- Cache: Redis 7
- Logging: Winston
- Testing: Jest, Supertest
- Linting: ESLint + Prettier
- Package Manager: pnpm
This project follows strict Clean Code and DDD principles:
- Read
.claude/PRINCIPLES.mdbefore contributing - Functions must be < 30 lines
- No magic numbers - use constants
- TypeScript strict mode (no
any) - Comprehensive error handling
- Structured logging
- Tests required (70%+ coverage)
MIT
- Backend Development Team
- AI Integration Team
- DevOps Team
Built with β€οΈ using Claude Code