|
| 1 | +# Affordable WhatsApp AI Chatbot Built in Python: Just $6/month |
| 2 | + |
| 3 | +Create a powerful WhatsApp chatbot powered by Google's Gemini AI for just $6/month (WaSenderAPI subscription) plus Google's free Gemini API tier (1500 requests/month). This Python-based solution uses Flask to handle incoming messages via WaSenderAPI webhooks and leverages Gemini's advanced AI capabilities to generate intelligent, conversational responses. |
| 4 | + |
| 5 | +## 💰 Cost-Effective Solution |
| 6 | + |
| 7 | +- **WaSenderAPI**: Only $6/month for WhatsApp integration |
| 8 | +- **Gemini AI**: Free tier with 1500 requests/month |
| 9 | +- **Hosting**: Run locally or on low-cost cloud options |
| 10 | +- **No WhatsApp Business API fees**: Uses WaSenderAPI as an affordable alternative |
| 11 | + |
| 12 | +## 🔥 Key Features |
| 13 | + |
| 14 | +- **WhatsApp Integration**: Receives and sends messages through WaSenderAPI |
| 15 | +- **AI-Powered Responses**: Generates intelligent replies using Google's Gemini AI |
| 16 | +- **Media Support**: Handles text, images, audio, video, and document messages |
| 17 | +- **Smart Message Splitting**: Automatically breaks long responses into multiple messages for better readability |
| 18 | +- **Customizable AI Persona**: Tailor the bot's personality and behavior via simple JSON configuration |
| 19 | +- **Conversation History**: Maintains context between messages for natural conversations |
| 20 | +- **Error Handling**: Robust logging and error management for reliable operation |
| 21 | +- **Easy Configuration**: Simple setup with environment variables |
| 22 | + |
| 23 | +## 📁 Project Structure |
| 24 | + |
| 25 | +``` |
| 26 | +/whatsapp-python-chatbot/ |
| 27 | +├── script.py # Main Flask application and bot logic |
| 28 | +├── requirements.txt # Python dependencies |
| 29 | +├── .env # Environment variables (API keys, etc.) |
| 30 | +├── persona.json # Customizable AI personality settings |
| 31 | +└── README.md # This file |
| 32 | +``` |
| 33 | + |
| 34 | +## 🚀 Setup and Installation |
| 35 | + |
| 36 | +1. **Clone the repository (if applicable) or create the files as described.** |
| 37 | + |
| 38 | +2. **Create a virtual environment (recommended):** |
| 39 | + |
| 40 | + ```bash |
| 41 | + python3 -m venv venv |
| 42 | + source venv/bin/activate # On Windows use `venv\Scripts\activate` |
| 43 | + ``` |
| 44 | + |
| 45 | +3. **Install dependencies:** |
| 46 | + |
| 47 | + ```bash |
| 48 | + pip3 install -r requirements.txt |
| 49 | + ``` |
| 50 | + |
| 51 | +4. **Configure Environment Variables:** |
| 52 | + Create a `.env` file in the project root directory by copying the example below. **Do not commit your `.env` file to version control if it contains sensitive keys.** |
| 53 | + |
| 54 | + ```env |
| 55 | + GEMINI_API_KEY="YOUR_GEMINI_API_KEY_HERE" # Free tier: 1500 requests/month |
| 56 | + WASENDER_API_TOKEN="YOUR_WASENDER_API_TOKEN_HERE" # $6/month subscription |
| 57 | + # Optional: If you change the port in script.py, update it here too for ngrok or other services |
| 58 | + # FLASK_RUN_PORT=5000 |
| 59 | + ``` |
| 60 | + |
| 61 | + Replace the placeholder values with your actual API keys: |
| 62 | + |
| 63 | + - `GEMINI_API_KEY`: Your API key for the Gemini API (free tier available) |
| 64 | + - `WASENDER_API_TOKEN`: Your API token from WaSenderAPI ($6/month subscription) |
| 65 | + |
| 66 | +## 🏃♂️ Running the Application |
| 67 | + |
| 68 | +### 1. Development Mode (using Flask's built-in server) |
| 69 | + |
| 70 | +This is suitable for local development and testing. |
| 71 | + |
| 72 | +```bash |
| 73 | +python3 script.py |
| 74 | +``` |
| 75 | + |
| 76 | +The application will typically run on `http://0.0.0.0:5001/` by default. |
| 77 | + |
| 78 | +### 2. Using ngrok for Webhook Testing |
| 79 | + |
| 80 | +WaSenderAPI needs to send webhook events (incoming messages) to a publicly accessible URL. If you're running the Flask app locally, `ngrok` can expose your local server to the internet. |
| 81 | +
|
| 82 | +a. **Install ngrok** (if you haven't already) from [https://ngrok.com/](https://ngrok.com/). |
| 83 | + |
| 84 | +b. **Start ngrok** to forward to your Flask app's port (e.g., 5001): |
| 85 | +
|
| 86 | +```bash |
| 87 | +ngrok http 5001 |
| 88 | +``` |
| 89 | +
|
| 90 | +c. **ngrok will provide you with a public URL** (e.g., `https://xxxx-xx-xxx-xxx-xx.ngrok-free.app`). |
| 91 | +
|
| 92 | +d. **Configure this ngrok URL as your webhook URL** in the WaSenderAPI dashboard for your connected device/session. Make sure to append the `/webhook` path (e.g., `https://xxxx-xx-xxx-xxx-xx.ngrok-free.app/webhook`). |
| 93 | +
|
| 94 | +### 3. Production Deployment (using Gunicorn) |
| 95 | +
|
| 96 | +For production, it's recommended to use a proper WSGI server like Gunicorn instead of Flask's built-in development server. |
| 97 | +
|
| 98 | +a. **Install Gunicorn:** |
| 99 | +
|
| 100 | +```bash |
| 101 | +pip3 install gunicorn |
| 102 | +``` |
| 103 | +
|
| 104 | +b. **Run the application with Gunicorn:** |
| 105 | +Replace `script:app` with `your_filename:your_flask_app_instance_name` if you change them. |
| 106 | +
|
| 107 | +```bash |
| 108 | +gunicorn --workers 4 --bind 0.0.0.0:5001 script:app |
| 109 | +``` |
| 110 | +
|
| 111 | +- `--workers 4`: Adjust the number of worker processes based on your server's CPU cores (a common starting point is `2 * num_cores + 1`). |
| 112 | +- `--bind 0.0.0.0:5001`: Specifies the address and port Gunicorn should listen on. |
| 113 | + |
| 114 | +c. **Reverse Proxy (Recommended):** |
| 115 | +In a typical production setup, you would run Gunicorn behind a reverse proxy like Nginx or Apache. The reverse proxy would handle incoming HTTPS requests, SSL termination, static file serving (if any), and forward requests to Gunicorn. |
| 116 | + |
| 117 | +## 🔄 WaSenderAPI Webhook Configuration |
| 118 | + |
| 119 | +- Log in to your WaSenderAPI dashboard. |
| 120 | +- Navigate to the session/device management section. |
| 121 | +- Find the option to set or update the webhook URL. |
| 122 | +- Enter the publicly accessible URL where your Flask application's `/webhook` endpoint is running (e.g., your ngrok URL during development, or your production server's URL). |
| 123 | +- Ensure the webhook is configured to send `POST` requests for message events. |
| 124 | + |
| 125 | +## 📝 Customizing Your Bot's Personality |
| 126 | + |
| 127 | +The chatbot includes a customizable base prompt that defines the AI's persona and behavior. Edit the `persona.json` file to change how Gemini responds to messages, making the bot more formal, casual, informative, or conversational as needed for your use case. |
| 128 | +
|
| 129 | +```json |
| 130 | +{ |
| 131 | + "name": "WhatsApp Assistant", |
| 132 | + "base_prompt": "You are a helpful and concise AI assistant replying in a WhatsApp chat...", |
| 133 | + "description": "You are a helpful WhatsApp assistant. Keep your responses concise..." |
| 134 | +} |
| 135 | +``` |
| 136 | +
|
| 137 | +## 📊 Logging and Error Handling |
| 138 | +
|
| 139 | +- The application uses Python's built-in `logging` module. |
| 140 | +- Logs are printed to the console by default. |
| 141 | +- Log format: `%(asctime)s - %(levelname)s - %(message)s`. |
| 142 | +- Unhandled exceptions are also logged. |
| 143 | +- **Important for Production:** Consider configuring logging to write to files, use a centralized logging service (e.g., ELK stack, Sentry, Datadog), and implement log rotation. |
| 144 | + |
| 145 | +## 📚 WaSenderAPI Documentation |
| 146 | + |
| 147 | +Refer to the official WaSenderAPI documentation for the most up-to-date information on API endpoints, request/response formats, and webhook details: [https://wasenderapi.com/api-docs](https://wasenderapi.com/api-docs) |
| 148 | + |
| 149 | +## 💡 Why This Solution? |
| 150 | + |
| 151 | +This chatbot offers an incredibly cost-effective way to deploy an AI-powered WhatsApp bot without the high costs typically associated with WhatsApp Business API. By combining WaSenderAPI's affordable $6/month subscription with Google's free Gemini API tier, you get a powerful, customizable chatbot solution at a fraction of the cost of enterprise alternatives. |
0 commit comments