Skip to content

A intelligent WhatsApp support agent powered by OpenAI, integrated with MySQL, Odoo, and Twilio for comprehensive customer support automation.

Notifications You must be signed in to change notification settings

freacatt/AI_Support

Repository files navigation

AI Support - WhatsApp AI Assistant

A intelligent WhatsApp support agent powered by OpenAI, integrated with MySQL, Odoo, and Twilio for comprehensive customer support automation.

Overview

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.

Features

  • 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

Architecture

Core Components

  1. app.py - Flask application with REST API endpoints and message processing logic
  2. openai_knowledge.py - OpenAI API client for generating intelligent responses
  3. mysql_chat.py - MySQL database handler for storing and retrieving chat history
  4. twilio_sender.py - Twilio client for sending WhatsApp messages
  5. odoo_integration.py - Odoo API integration for CRM operations

Data Flow

WhatsApp Message
       ↓
    Twilio
       ↓
   app.py (Flask)
       ↓
   Process Async
   ├─ Save to MySQL
   ├─ Get Chat History
   ├─ Generate OpenAI Response
   └─ Send via Twilio
       ↓
   WhatsApp Response

Prerequisites

  • 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)

Installation

1. Clone the Repository

git clone https://github.com/pouria-shahmiri/AI_Support.git
cd AI_Support

2. Create Virtual Environment

python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

3. Install Dependencies

pip install -r requirements.txt

4. Configure Environment Variables

Create 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_key

Running the Application

Local Development

python app.py

The application will start on http://localhost:5000

Docker Deployment

Build and run with Docker:

docker build -t ai-support .
docker run -p 5000:5000 --env-file .env ai-support

API Endpoints

Health Check

GET /health

Returns the health status of all services.

Process WhatsApp Message

POST /process
Content-Type: application/json

{
    "from_number": "whatsapp:+1234567890",
    "message_body": "Hello, I need help with...",
    "profile_name": "customer_name"
}

Environment Variables Reference

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

Database Schema

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)
)

Deployment with Dockploy

This project includes a dockploy.yml configuration file for easy deployment:

name: whatsapp-agent
type: application
runtime:
  port: 5000
  healthcheck:
    path: /health
    interval: 30s

Deploy using Dockploy:

dockploy deploy

Dependencies

  • 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

Features in Detail

Lazy Loading

Services (MySQL, OpenAI, Twilio, Odoo) are initialized only when first accessed, improving startup time and reducing unnecessary connections.

Concurrent Processing

The application uses a ThreadPoolExecutor with 15 workers to handle multiple WhatsApp messages simultaneously without blocking.

Chat History

All messages are stored with timestamps and direction (inbound/outbound) for:

  • Context-aware AI responses
  • Conversation auditing
  • Customer support analytics

Error Handling

Comprehensive error handling ensures the service remains operational even if individual integrations fail gracefully.

Logging

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

Security

  • Non-root user execution in Docker
  • Environment variable management for sensitive data
  • SSL-disabled option for MySQL connections (configurable)
  • Input validation and error handling

Troubleshooting

MySQL Connection Issues

  • Verify MySQL credentials in .env
  • Check MySQL is running and accessible
  • Ensure database exists or creation permissions granted

OpenAI Integration Issues

  • Verify API key is valid
  • Confirm Assistant ID exists in your OpenAI account
  • Check rate limits haven't been exceeded

Twilio Issues

  • Verify Twilio credentials
  • Ensure WhatsApp Business API is enabled
  • Check webhook URL is correctly configured in Twilio

License

This project is part of the AI_Support suite.

Contributing

Contributions are welcome! Please feel free to submit pull requests or report issues.

Contact

For support and inquiries, please contact the development team.

About

A intelligent WhatsApp support agent powered by OpenAI, integrated with MySQL, Odoo, and Twilio for comprehensive customer support automation.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published