A Node.js application that integrates OpenAI's ChatGPT with WhatsApp through Twilio, allowing users to interact with AI directly through WhatsApp messages with subscription-based token management.
- π€ AI-Powered Conversations: Interact with OpenAI's GPT model through WhatsApp
- π± WhatsApp Integration: Seamless messaging through Twilio's WhatsApp API
- π³ Subscription Management: Token-based usage with subscription tiers
- π Secure Configuration: Environment-based configuration for API keys
- π Session Tracking: Track message counts per user session
- User sends a WhatsApp message to your Twilio number
- Twilio forwards the message to your webhook endpoint (
/sms) - The bot checks the user's subscription status
- Based on subscription status, the bot either:
- Processes the message with ChatGPT
- Prompts for subscription/token setup
- Requests subscription upgrade
- User receives: "Please give me your required tokens"
- User sends a number to set their token limit
- System creates subscription with specified tokens
- User receives: "You don't have an active subscription. Please visit our website to view our plans"
- Message is sent to OpenAI's GPT model
- Response is generated using user's available tokens
- Reply is sent back through WhatsApp
WhatsApp User β Twilio β Your Server β Subscription API
β
OpenAI GPT API β Response β WhatsApp User
- Node.js (v14 or higher)
- Twilio account with WhatsApp sandbox or approved number
- OpenAI API account
- Subscription management API (or modify code for direct database)
-
Clone the repository
git clone <repository-url> cd chatgpt-whatsapp-bot
-
Install dependencies
npm install
-
Environment Configuration
Create a
.envfile in the root directory:# OpenAI Configuration OPENAI_API_KEY=your_openai_api_key_here OPENAI_ORGANIZATION=your_openai_org_id_here # Subscription API SUBSCRIPTION_API_URL=https://chatgpt.talhasultan.dev/api/subscriptions # Database Configuration (for future MySQL integration) MYSQL_HOST=localhost MYSQL_USER=your_mysql_user MYSQL_PASS=your_mysql_password MYSQL_DB=chatgpt # Server Configuration PORT=3000
-
Twilio Webhook Setup
- Set your Twilio webhook URL to:
https://yourdomain.com/sms - Ensure your server is accessible via HTTPS (use ngrok for local development)
- Set your Twilio webhook URL to:
Development
npm startProduction
node server.jsThe server will start on port 3000 (or your specified PORT).
Health check endpoint that returns "Running............"
Main webhook endpoint for processing WhatsApp messages from Twilio.
Expected Request Body:
From: User's phone number (Twilio format)Body: Message content from user
For future MySQL integration, create a database named chatgpt with a subscription table:
CREATE DATABASE chatgpt;
USE chatgpt;
CREATE TABLE subscription (
id INT AUTO_INCREMENT PRIMARY KEY,
contact_no VARCHAR(20) NOT NULL,
sub INT NOT NULL,
tokens INT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);- Model:
text-davinci-003 - Temperature:
0(for consistent responses) - Max Tokens: Based on user's subscription limit
-1: New user (token selection required)0: Inactive subscription7: Active subscription (configurable)
- All sensitive data is stored in environment variables
.envfile is gitignored to prevent accidental commits- Session management for tracking user interactions
- Basic error handling for API failures
- Graceful degradation for subscription API issues
- Console logging for debugging
- Create a new Heroku app
- Set environment variables in Heroku dashboard
- Deploy using Git or GitHub integration
- Connect your repository
- Configure environment variables
- Deploy with automatic builds
- Clone repository to your server
- Install Node.js and dependencies
- Configure environment variables
- Use PM2 or similar for process management
- Set up reverse proxy (nginx) for HTTPS
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
MIT License - see LICENSE file for details.
For issues and questions:
- Check the console logs for error messages
- Verify all environment variables are set correctly
- Ensure Twilio webhook is properly configured
- Test OpenAI API connectivity independently
- Direct MySQL integration
- Enhanced error handling
- Rate limiting implementation
- Multiple AI model support
- Admin dashboard for subscription management
- Usage analytics and reporting
