A intelligent WhatsApp support agent powered by OpenAI, integrated with MySQL, Odoo, and Twilio for comprehensive customer support automation.
AI Support is a Flask-based application that enables businesses to handle customer inquiries through WhatsApp using an AI-powered assistant. The system integrates with multiple services including OpenAI for intelligent responses, MySQL for chat history, Twilio for WhatsApp messaging, and Odoo for CRM operations.
- AI-Powered Responses: Uses OpenAI assistants to generate intelligent, context-aware customer support responses
- WhatsApp Integration: Sends and receives messages via Twilio WhatsApp Business API
- Chat History: Stores all conversations in MySQL for future reference and improved responses
- Odoo CRM Integration: Seamlessly integrates with Odoo for customer data and operations management
- Concurrent Processing: Handles multiple conversations simultaneously using thread pool execution
- Lazy Loading: Efficiently initializes services only when needed
- Production Ready: Dockerized for easy deployment with health checks and non-root user security
- app.py - Flask application with REST API endpoints and message processing logic
- openai_knowledge.py - OpenAI API client for generating intelligent responses
- mysql_chat.py - MySQL database handler for storing and retrieving chat history
- twilio_sender.py - Twilio client for sending WhatsApp messages
- odoo_integration.py - Odoo API integration for CRM operations
WhatsApp Message
↓
Twilio
↓
app.py (Flask)
↓
Process Async
├─ Save to MySQL
├─ Get Chat History
├─ Generate OpenAI Response
└─ Send via Twilio
↓
WhatsApp Response
- Python 3.11+
- MySQL Database
- OpenAI API Key with an Assistant configured
- Twilio Account with WhatsApp Business API access
- Odoo Instance (optional, for CRM features)
git clone https://github.com/pouria-shahmiri/AI_Support.git
cd AI_Supportpython -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activatepip install -r requirements.txtCreate a .env file in the project root:
# MySQL Configuration
MYSQL_USERNAME=your_mysql_user
MYSQL_PASSWORD=your_mysql_password
MYSQL_HOST=localhost
MYSQL_PORT=3306
MYSQL_DATABASE=whatsapp_chat
# OpenAI Configuration
OPENAI_API_KEY=your_openai_api_key
OPENAI_ASSISTANT_ID=your_assistant_id
# Twilio Configuration
TWILIO_ACCOUNT_SID=your_account_sid
TWILIO_AUTH_TOKEN=your_auth_token
TWILIO_WHATSAPP_NUMBER=whatsapp:+1234567890
# Odoo Configuration (Optional)
ODOO_URL=https://your-odoo-instance.com
ODOO_DB=your_database
ODOO_USERNAME=your_username
ODOO_API_KEY=your_api_keypython app.pyThe application will start on http://localhost:5000
Build and run with Docker:
docker build -t ai-support .
docker run -p 5000:5000 --env-file .env ai-supportGET /health
Returns the health status of all services.
POST /process
Content-Type: application/json
{
"from_number": "whatsapp:+1234567890",
"message_body": "Hello, I need help with...",
"profile_name": "customer_name"
}
| Variable | Description | Required |
|---|---|---|
MYSQL_USERNAME |
MySQL user | Yes |
MYSQL_PASSWORD |
MySQL password | Yes |
MYSQL_HOST |
MySQL host address | Yes |
MYSQL_PORT |
MySQL port (default: 3306) | Yes |
MYSQL_DATABASE |
MySQL database name | Yes |
OPENAI_API_KEY |
OpenAI API key | Yes |
OPENAI_ASSISTANT_ID |
OpenAI Assistant ID | Yes |
TWILIO_ACCOUNT_SID |
Twilio Account SID | Yes |
TWILIO_AUTH_TOKEN |
Twilio Auth Token | Yes |
TWILIO_WHATSAPP_NUMBER |
Twilio WhatsApp number | Yes |
ODOO_URL |
Odoo instance URL | No |
ODOO_DB |
Odoo database | No |
ODOO_USERNAME |
Odoo username | No |
ODOO_API_KEY |
Odoo API key | No |
The application automatically creates a chat_history table with the following structure:
CREATE TABLE chat_history (
id INT AUTO_INCREMENT PRIMARY KEY,
whatsapp_number VARCHAR(50) NOT NULL,
direction ENUM('inbound', 'outbound') NOT NULL,
message TEXT NOT NULL,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
INDEX idx_whatsapp_number (whatsapp_number),
INDEX idx_timestamp (timestamp)
)This project includes a dockploy.yml configuration file for easy deployment:
name: whatsapp-agent
type: application
runtime:
port: 5000
healthcheck:
path: /health
interval: 30sDeploy using Dockploy:
dockploy deploy- Flask (2.3.3) - Web framework
- Gunicorn (21.2.0) - Production WSGI server
- mysql-connector-python (8.2.0) - MySQL database driver
- python-dotenv (1.0.0) - Environment variable management
- Twilio (8.5.0) - WhatsApp messaging
- OpenAI (≥1.3.0) - AI assistant API
- Requests (2.31.0) - HTTP library
Services (MySQL, OpenAI, Twilio, Odoo) are initialized only when first accessed, improving startup time and reducing unnecessary connections.
The application uses a ThreadPoolExecutor with 15 workers to handle multiple WhatsApp messages simultaneously without blocking.
All messages are stored with timestamps and direction (inbound/outbound) for:
- Context-aware AI responses
- Conversation auditing
- Customer support analytics
Comprehensive error handling ensures the service remains operational even if individual integrations fail gracefully.
The application uses Python's logging module configured at INFO level. Logs include:
- Service initialization status
- Message processing events
- AI response generation
- Error details for troubleshooting
- Non-root user execution in Docker
- Environment variable management for sensitive data
- SSL-disabled option for MySQL connections (configurable)
- Input validation and error handling
- Verify MySQL credentials in
.env - Check MySQL is running and accessible
- Ensure database exists or creation permissions granted
- Verify API key is valid
- Confirm Assistant ID exists in your OpenAI account
- Check rate limits haven't been exceeded
- Verify Twilio credentials
- Ensure WhatsApp Business API is enabled
- Check webhook URL is correctly configured in Twilio
This project is part of the AI_Support suite.
Contributions are welcome! Please feel free to submit pull requests or report issues.
For support and inquiries, please contact the development team.