zapzap-hashino
v1.0.0
Published
Production-ready WhatsApp Business API backend with AI image analysis using OpenAI and Anthropic
Maintainers
Readme
WhatsApp AI Backend
A comprehensive WhatsApp Business API backend with AI-powered image analysis capabilities using OpenAI and Anthropic.
🚀 Features
- 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
📋 Prerequisites
- Node.js 18+
- TypeScript
- WhatsApp Business Account with API access
- OpenAI API Key
- Anthropic API Key
🛠️ Installation
- Clone and navigate to the project:
cd whatsapp-ai-backend- Install dependencies:
npm install- Copy environment configuration:
cp .env.example .env- Configure your environment variables in
.env:
# Server Configuration
PORT=3000
NODE_ENV=development
# AI Services
OPENAI_API_KEY=your_openai_api_key_here
ANTHROPIC_API_KEY=your_anthropic_api_key_here
# WhatsApp Business API
WHATSAPP_API_URL=https://graph.facebook.com/v18.0
WHATSAPP_ACCESS_TOKEN=your_whatsapp_access_token_here
WHATSAPP_PHONE_NUMBER_ID=your_phone_number_id_here
WHATSAPP_VERIFY_TOKEN=your_verify_token_here
# File Upload
MAX_FILE_SIZE=10485760
UPLOAD_PATH=./uploads
# Logging
LOG_LEVEL=info
LOG_FILE=app.log
# Rate Limiting
RATE_LIMIT_WINDOW_MS=900000
RATE_LIMIT_MAX=100🚀 Usage
Development
npm run devProduction
npm run build
npm startTesting
npm testLinting & Type Checking
npm run lint
npm run typecheck📡 API Endpoints
WhatsApp Endpoints
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:
{
"to": "+5511999999999",
"message": "Hello from AI!",
"type": "text"
}AI Endpoints
POST /api/ai/analyze
Analyze images or text using AI.
Body:
{
"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 fileprompt: Analysis promptprovider: AI provider (openai/anthropic)
POST /api/ai/generate
Generate text responses.
Body:
{
"prompt": "Write a haiku about technology",
"provider": "anthropic"
}GET /api/ai/providers
Get available AI providers and their capabilities.
Health Endpoints
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
Supported Commands
- 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
AI Providers
- 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
File Upload
- Maximum file size: 10MB (configurable)
- Supported formats: JPEG, PNG, GIF, WebP
- Automatic image resizing to 1024x1024 max
- Files stored in
./uploadsdirectory
Rate Limiting
- Default: 100 requests per 15 minutes per IP
- Configurable via environment variables
📊 Monitoring
Logging
- Winston-based logging with multiple transports
- Separate error log file
- Structured JSON logging for production
- Console logging for development
Health Checks
/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:
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
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
📞 Support
For support, create an issue in the GitHub repository.
