Skip to content

Commit edecdcd

Browse files
committed
init
0 parents  commit edecdcd

File tree

8 files changed

+730
-0
lines changed

8 files changed

+730
-0
lines changed

.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
GEMINI_API_KEY="YOUR_GEMINI_API_KEY"
2+
WASENDER_API_TOKEN="YOUR_WASENDER_API_TOKEN"
3+
# Optional: If you change the port in script.py, update it here too for ngrok or other services
4+
# FLASK_RUN_PORT=5000

README.md

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
[
2+
{
3+
"role": "user",
4+
"parts": [
5+
"Hii"
6+
]
7+
},
8+
{
9+
"role": "model",
10+
"parts": [
11+
"Hello! It's nice to meet you. How can I help you today? Is there anything you'd like to discuss about your health or well-being? Remember, I'm here to listen and offer guidance, but I cannot provide diagnoses or prescribe medication."
12+
]
13+
},
14+
{
15+
"role": "user",
16+
"parts": [
17+
"My name is amine"
18+
]
19+
},
20+
{
21+
"role": "model",
22+
"parts": [
23+
"Thank you for sharing your name, Amine. It's a pleasure to speak with you. So, Amine, what's on your mind today? What can I do to assist you?"
24+
]
25+
},
26+
{
27+
"role": "user",
28+
"parts": [
29+
"How are you doing?"
30+
]
31+
},
32+
{
33+
"role": "model",
34+
"parts": [
35+
"Thank you for asking, Amine. I'm doing well and ready to assist you with any general health questions or concerns you might have. Now, tell me, what's been happening with you? What brings you here today?"
36+
]
37+
},
38+
{
39+
"role": "user",
40+
"parts": [
41+
"Tell me what are doing ?"
42+
]
43+
},
44+
{
45+
"role": "model",
46+
"parts": [
47+
"As a large language model, I don't \"do\" things in the way a human does. I don't have a physical body, so I'm not eating, sleeping, or moving around.\n\nInstead, I exist as a computer program. Right now, I'm using my training data to understand your questions and provide you with helpful and informative answers. I'm essentially processing information and generating text. My purpose is to assist users with their queries and provide information to the best of my ability.\n\nThink of it like this: I'm here to learn from you and help you learn in return."
48+
]
49+
},
50+
{
51+
"role": "user",
52+
"parts": [
53+
"What is my name?"
54+
]
55+
},
56+
{
57+
"role": "model",
58+
"parts": [
59+
"Your name is Amine, you told me earlier. \ud83d\ude0a"
60+
]
61+
}
62+
]

0 commit comments

Comments
 (0)