Production-ready WhatsApp Business API backend with AI image analysis using OpenAI and Anthropic
npm install zapzap-hashinoA comprehensive WhatsApp Business API backend with AI-powered image analysis capabilities using OpenAI and Anthropic.
- WhatsApp Business API Integration: Send/receive messages via WhatsApp webhook
- AI-Powered Image Analysis: Analyze images using OpenAI GPT-4 Vision and Anthropic Claude-3
- Multi-Provider AI Support: Switch between OpenAI and Anthropic for different use cases
- File Upload & Processing: Upload and process images with automatic resizing
- Rate Limiting: Built-in rate limiting for API protection
- Comprehensive Logging: Winston-based logging with different levels
- Health Monitoring: Health check endpoints for monitoring
- Type-Safe: Full TypeScript implementation with strict typing
- Node.js 18+
- TypeScript
- WhatsApp Business Account with API access
- OpenAI API Key
- Anthropic API Key
1. Clone and navigate to the project:
``bash`
cd whatsapp-ai-backend
2. Install dependencies:
`bash`
npm install
3. Copy environment configuration:
`bash`
cp .env.example .env
4. Configure your environment variables in .env:`envServer Configuration
PORT=3000
NODE_ENV=development
š Usage
$3
`bash
npm run dev
`$3
`bash
npm run build
npm start
`$3
`bash
npm test
`$3
`bash
npm run lint
npm run typecheck
`š” API Endpoints
$3
####
GET /api/whatsapp/webhook
Webhook verification for WhatsApp Business API.####
POST /api/whatsapp/webhook
Webhook endpoint for receiving WhatsApp messages.####
POST /api/whatsapp/send-message
Send messages via WhatsApp Business API.Body:
`json
{
"to": "+5511999999999",
"message": "Hello from AI!",
"type": "text"
}
`$3
####
POST /api/ai/analyze
Analyze images or text using AI.Body:
`json
{
"imageUrl": "https://example.com/image.jpg",
"text": "Optional text to analyze",
"provider": "openai",
"options": {
"prompt": "Analyze this image",
"temperature": 0.7,
"max_tokens": 500
}
}
`####
POST /api/ai/analyze-upload
Upload and analyze an image.Form Data:
-
image: Image file
- prompt: Analysis prompt
- provider: AI provider (openai/anthropic)####
POST /api/ai/generate
Generate text responses.Body:
`json
{
"prompt": "Write a haiku about technology",
"provider": "anthropic"
}
`####
GET /api/ai/providers
Get available AI providers and their capabilities.$3
####
GET /api/health
Get application health status.####
GET /api/health/ready
Check if application is ready to serve requests.š¤ WhatsApp Bot Features
The bot automatically responds to different types of messages:
- Text Messages: Generates AI-powered responses
- Images: Analyzes images and provides detailed descriptions
- Greetings: Responds with welcome message in Portuguese/English
- Help Requests: Provides usage instructions
$3
- Send "hello", "hi", or "oi" for greetings
- Send "help" or "ajuda" for assistance
- Send images for AI analysis
- Use keywords like "analyze" or "describe" for detailed analysisšļø Project Structure
`
src/
āāā types/ # TypeScript type definitions
āāā services/ # Business logic services
ā āāā aiService.ts # AI integration (OpenAI + Anthropic)
ā āāā whatsappService.ts # WhatsApp Business API
āāā routes/ # API route handlers
ā āāā ai.ts # AI-related endpoints
ā āāā whatsapp.ts # WhatsApp webhook endpoints
ā āāā health.ts # Health check endpoints
āāā middleware/ # Express middleware
ā āāā errorHandler.ts # Error handling
ā āāā rateLimiter.ts # Rate limiting
āāā utils/ # Utility functions
ā āāā config.ts # Environment configuration
ā āāā logger.ts # Winston logging setup
āāā index.ts # Application entry point
`š§ Configuration
$3
- OpenAI: GPT-4 Vision for image analysis, GPT-3.5 Turbo for text
- Anthropic: Claude-3 Sonnet for image analysis, Claude-3 Haiku for text$3
- Maximum file size: 10MB (configurable)
- Supported formats: JPEG, PNG, GIF, WebP
- Automatic image resizing to 1024x1024 max
- Files stored in ./uploads directory$3
- Default: 100 requests per 15 minutes per IP
- Configurable via environment variablesš Monitoring
$3
- Winston-based logging with multiple transports
- Separate error log file
- Structured JSON logging for production
- Console logging for development$3
- /api/health: Application status
- /api/health/ready: Readiness probe
- Memory usage monitoring
- Uptime trackingš Security Features
- Helmet: Security headers
- CORS: Cross-origin resource sharing
- Rate Limiting: Request throttling
- Input Validation: Joi schema validation
- Error Handling: Secure error responses
- File Upload Security: Type and size validation
š³ Docker Support (Optional)
Create
Dockerfile:
`dockerfile
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY dist ./dist
EXPOSE 3000
CMD ["npm", "start"]
`š License
MIT License - See LICENSE file for details.
š¤ Contributing
1. Fork the repository
2. Create your feature branch (
git checkout -b feature/amazing-feature)
3. Commit your changes (git commit -m 'Add amazing feature')
4. Push to the branch (git push origin feature/amazing-feature`)For support, create an issue in the GitHub repository.