https://github.com/blinksta1ker/chatbot
Chatbot using Flask
https://github.com/blinksta1ker/chatbot
backend chatbot flask openai-api postman python
Last synced: about 2 months ago
JSON representation
Chatbot using Flask
- Host: GitHub
- URL: https://github.com/blinksta1ker/chatbot
- Owner: BLinKSta1Ker
- Created: 2025-03-06T07:37:35.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-06T08:18:33.000Z (over 1 year ago)
- Last Synced: 2025-03-06T09:35:44.483Z (over 1 year ago)
- Topics: backend, chatbot, flask, openai-api, postman, python
- Language: Python
- Homepage:
- Size: 1000 Bytes
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Chatbot









## π§ Chatbot API (Flask + OpenAI + MongoDB + Redis)
A secure, rate-limited, JWT-authenticated chatbot API built with Flask. Supports OpenAI integration for conversational AI, user authentication, admin analytics, and chat history management.
## π Features
- **User Authentication**
- Register/Login with JWT
- Secure password hashing
- Token blacklisting on logout
- Rate-limited endpoints
- **Admin Capabilities**
- View total users and total chat messages
- List all users
- Delete specific users
- **AI Chatbot**
- Integrates with OpenAIβs GPT-4o-mini
- Maintains short-term context using Redis
- Persists 30-day chat history in MongoDB
- **Security**
- JWT-based access control
- Rate limiting to prevent abuse
- Input sanitization with `bleach`
- Token revocation via Redis
## π Project Structure
```
Chatbot/
βββ app/
β βββ __init__.py # Flask app factory
β βββ admin.py # Admin routes
β βββ auth.py # Auth routes
β βββ chatbot.py # AI chat logic
β βββ config.py # Environment/config manager
β βββ database.py # Redis and MongoDB connections
β βββ routes.py # Chat routes
β βββ utils.py # Input sanitization
βββ models/
β βββ conversation.py # Chat history handlers
βββ .env # Environment variables
βββ .gitignore # Ignored files
βββ error.log # Error logging
βββ readme.md # You are here
βββ requirements.txt # Python dependencies
βββ run.py # Entry point
```
## π¦ Setup Instructions
### 1. Clone the Repository
```bash
git clone https://github.com/BLinKSta1Ker/Chatbot.git
cd chatbot
```
### 2. Install Dependencies
Create a virtual environment (optional but recommended):
```bash
python3 -m venv venv
source venv/bin/activate
```
Then install:
```bash
pip install -r requirements.txt
```
### 3. Set Environment Variables
Create a `.env` file in the root directory:
```
OPENAI_API_KEY=your_openai_key
MONGO_URI=mongodb://localhost:27017
REDIS_HOST=localhost
REDIS_PORT=6379
JWT_SECRET_KEY=supersecret
```
### 4. Run the App
```bash
python run.py
```
The app will be accessible at `http://127.0.0.1:5000/`.
## π¬ API Endpoints
### π Auth
| Method | Endpoint | Description |
|--------|--------------------|---------------------------|
| POST | `/register` | Register user |
| POST | `/login` | Login & get JWT |
| POST | `/logout` | Revoke token |
| POST | `/register_admin` | Register an admin user |
### π€ Chat
| Method | Endpoint | Description |
|--------|--------------------|---------------------------|
| POST | `/chat` | Send a message to chatbot |
| GET | `/history` | Get last 30 days of history |
### π Admin
| Method | Endpoint | Description |
|--------|---------------|---------------------------|
| GET | `/admin/stats`| View user/chat stats |
| GET | `/admin/users`| List all users |
| DELETE | `/admin/delete_user` | Delete user by ID |
> **Note:** All chat and admin routes require a valid JWT token in the `Authorization` header.
## π‘ Security Measures
- Passwords hashed with Werkzeug
- JWT token expiration and blacklist
- Brute-force protection via `Flask-Limiter`
- Input sanitization using `bleach`
## π§Ή Maintenance
- Chat messages older than 30 days are auto-deleted on startup via `delete_old_messages()`.
## π License
This project is licensed under the MIT License.