A production-ready WhatsApp bot that provides AI-powered plant disease detection for farmers and gardeners.
- WhatsApp Integration: Seamless messaging via Twilio
- AI Disease Detection: Powered by Plant Saathi AI API
- Rate Limiting: 5 free scans per day per user
- User Management: Automatic user creation and tracking
- Image Processing: Validates image format and size
- Professional Messaging: Clean, formatted responses
- Scalable Architecture: Ready for thousands of users
- Admin Dashboard: User statistics and management
# Copy environment file
cp .env.example .env
# Install dependencies
pip install -r requirements.txtEdit .env file with your credentials:
# Twilio WhatsApp API
TWILIO_ACCOUNT_SID=your_twilio_account_sid
TWILIO_AUTH_TOKEN=your_twilio_auth_token
TWILIO_PHONE_NUMBER=+1234567890
# Plant Saathi API
PLANT_SAATHI_API_URL=https://plant-saathi-api.onrender.com/detect
PLANT_SAATHI_API_KEY=49732e55cc53907929bb896e25df0111de11f6b01a038c7217f8805f6a29f04d
# Your app URLs
WEB_APP_URL=https://plantSaathi.com
MOBILE_APP_URL=https://plantSaathi.com/app# Development
python -m uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
# Production
python app/main.py# 1. Start the bot
python start_bot.py
# 2. In another terminal, test the endpoints
python test_bot.py# Install ngrok (if not installed)
npm install -g ngrok
# Expose your local bot
ngrok http 8000- Copy the ngrok HTTPS URL (e.g.,
https://abc123.ngrok.io) - Go to Twilio Console
- Update webhook URL to:
https://YOUR-NGROK-URL.ngrok.io/webhook/whatsapp - Keep method as POST
- Click Save
1. Join sandbox: Send "join <sandbox-keyword>" to +1 415 523 8886
2. Send "hi" to +17622528098
3. Send a plant image
4. Test the 5-scan daily limit
# 1. Install Railway CLI
npm install -g @railway/cli
# 2. Login to Railway
railway login
# 3. Deploy your bot
railway up
# 4. Set environment variables in Railway dashboard
# 5. Get your Railway URL: https://your-app.railway.appIn your Twilio Console:
-
When a message comes in:
- URL:
https://your-app.railway.app/webhook/whatsapp - Method:
POST
- URL:
-
Status callback URL: (optional)
- URL:
https://your-app.railway.app/webhook/status - Method:
POST
- URL:
- User sends "Hi" β Gets welcome message
- User sends plant image β AI analyzes and responds with diagnosis
- After 5 scans β Directed to web/mobile app for unlimited access
- Premium users β Unlimited WhatsApp scans
- Welcome: Introduction and instructions
- Disease Detection: Detailed diagnosis with remedies
- Limit Exceeded: Upgrade options and alternatives
- Error Handling: Clear error messages and solutions
app/
βββ main.py # FastAPI application & webhooks
βββ config.py # Environment configuration
βββ database.py # SQLAlchemy models & DB setup
βββ models.py # Pydantic models
βββ services/
βββ whatsapp_service.py # WhatsApp messaging
βββ user_service.py # User management & tracking
βββ detection_service.py # AI API integration
POST /webhook/whatsapp- Twilio WhatsApp webhook
GET /stats/user/{phone_number}- User statisticsGET /admin/users- List all usersGET /health- Health check
phone_number- Unique user identifierdaily_scan_count- Scans used todaytotal_scans- Lifetime scan countis_premium- Premium subscription statuslast_scan_date- Last activity tracking
user_phone- User referenceimage_url- Processed image URLdetection_result- AI analysis resultconfidence_score- Detection confidencescan_timestamp- Processing time
- Default: 5 scans per day
- Resets: Every 24 hours
- Premium: Unlimited scans
- Max size: 10MB
- Formats: JPEG, PNG, WEBP
- Min resolution: 100x100px
- Professional formatting
- Emoji usage for engagement
- Clear call-to-actions
- Multilingual ready
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY app/ ./app/
EXPOSE 8000
CMD ["python", "app/main.py"]- Connect your GitHub repo
- Set environment variables
- Deploy automatically
# Install dependencies
pip install -r requirements.txt
# Run with gunicorn
gunicorn app.main:app -w 4 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000- User registration and activity
- Scan success/failure rates
- API response times
- Daily/monthly usage trends
- Structured logging with timestamps
- Error tracking and debugging
- User action audit trail
- Performance metrics
- Image format verification
- File size limits
- Phone number sanitization
- SQL injection prevention
- Per-user daily limits
- API timeout handling
- Abuse detection ready
- PDF Reports: Detailed diagnosis documents
- Payment Integration: Razorpay/Stripe for premium
- Voice Messages: Audio diagnosis results
- Multi-language: Hindi, regional languages
- Advanced Analytics: Dashboard and insights
For issues or questions:
- Create GitHub issues
- Email: support@plantSaathi.com
- Documentation: Plant Saathi Docs
MIT License - see LICENSE file for details.
Plant Saathi AI - Empowering farmers with AI-driven plant health solutions π±π