Telegram bot for generating unique hashtag batches with usage history tracking and authorization system.
- π Authorization System - bot access via access code only
- π Category Management - create, edit, delete categories with pagination
- π· Hashtag Management - add, link and unlink hashtags
- π Flexible Linking - one hashtag can belong to multiple categories
- π² Smart Generation - create batches of 10 hashtags with uniqueness control
- π Batch History - view, copy and delete saved batches
- πΎ Persistent Database - all data saved between restarts
- Pagination - convenient browsing of large lists (categories - 10/page, hashtags - 15/page)
- Interactive Editing - all actions via message editing (doesn't clutter chat)
- Uniqueness Control - hashtags don't repeat in the last 5 batches
- Link Existing Hashtags - link hashtags to categories without recreation
- Unlink Hashtags - remove hashtag from category with one tap
- User selects 1 to 9 categories
- System collects all unique hashtags from selected categories
- Filters hashtags, excluding those used in the last 5 batches
- Randomly selects 9 unique hashtags
- Adds fixed hashtag
#fypat the end - Prompts to save batch to history
Output format: #hashtag1 #hashtag2 #hashtag3 ... #fyp
- Python 3.11+
- Docker and Docker Compose (for deployment)
- Telegram Bot Token from @BotFather
- Clone the repository:
git clone https://github.com/topmonroe9/hashtag-bot.git
cd hashtag-bot- Create virtual environment:
python -m venv venv
# Windows
venv\Scripts\activate
# Linux/macOS
source venv/bin/activate- Install dependencies:
pip install -r requirements.txt- Create
.envfile:
cp .env.example .env- Configure environment variables (edit
.env):
BOT_TOKEN=your_token_from_BotFather
ACCESS_CODE=your_access_code- Run the bot:
python main.py# Create .env file with token and access code
cp .env.example .env
nano .env
# Start the bot
docker-compose up -d --build
# View logs
docker-compose logs -f
# Stop
docker-compose down- Connect to server:
ssh dev2.tech.apree- Create directory for bot:
mkdir -p ~/hashtag-bot
cd ~/hashtag-bot- Copy project files (from local machine):
# Create archive
tar --exclude='venv' --exclude='__pycache__' --exclude='*.pyc' --exclude='*.db' --exclude='.git' -czf hashtag-bot.tar.gz .
# Copy to server
scp hashtag-bot.tar.gz dev2.tech.apree:~/hashtag-bot/
# On server, extract
cd ~/hashtag-bot
tar -xzf hashtag-bot.tar.gz
rm hashtag-bot.tar.gz- Create .env file (on server):
nano .envBOT_TOKEN=your_token
ACCESS_CODE=your_code- Start the bot:
mkdir -p data
docker-compose up -d --build- Check status:
docker-compose ps
docker-compose logs -f# View logs
docker-compose logs -f
# Restart
docker-compose restart
# Stop
docker-compose stop
# Start
docker-compose up -d
# Update code and restart
docker-compose down
docker-compose up -d --build
# Status
docker-compose ps- Find the bot on Telegram: @hashtags_apree_bot
- Send
/start - Authorize:
/auth your_code
| Command | Description |
|---|---|
/start |
Start bot and show main menu |
/auth [code] |
Authorization |
/generate |
Generate hashtag batch |
/history |
View saved batch history |
/categories |
Manage categories |
/hashtags |
Manage hashtags |
/add_category |
Quick add category |
/add_hashtag |
Quick add hashtag |
- π² Generate - directly opens batch generation
- π History - shows last 10 saved batches
- π Categories - opens all categories list
- π· Hashtags - hashtag management menu
-
Create a category:
- Click "π Categories" β "β Add Category"
- Enter name (e.g., "Lifestyle")
-
Add hashtags:
- Click "π· Hashtags" β "β Add Hashtag"
- Enter hashtag (e.g., "travel" or "#travel")
- Select category to link
-
Link existing hashtags:
- Open category
- Click "β Link Hashtag"
- Select hashtags from list
-
Unlink hashtag:
- Open category
- Click π next to hashtag
-
Generate batch:
- Click "π² Generate"
- Select 1 to 9 categories
- Click "π² Generate"
- Decide whether to save batch to history
-
Work with history:
- Click "π History"
- Use π to copy batch
- Use π to delete batch
hashtag-bot/
βββ bot/
β βββ database/
β β βββ __init__.py
β β βββ database.py # SQLite database operations
β βββ handlers/
β β βββ __init__.py
β β βββ common.py # /start, /auth, menu buttons
β β βββ categories.py # Category management + pagination
β β βββ hashtags.py # Hashtag management
β β βββ generate.py # Batch generation
β β βββ history.py # Saved batch history
β βββ middlewares/
β β βββ __init__.py
β β βββ auth.py # Authorization middleware
β βββ utils/
β β βββ __init__.py
β β βββ generator.py # Hashtag generation logic
β βββ __init__.py
βββ data/ # Database (auto-created)
β βββ hashtag_bot.db
βββ main.py # Entry point
βββ config.py # Configuration
βββ requirements.txt # Python dependencies
βββ Dockerfile # Docker image
βββ docker-compose.yml # Docker Compose config
βββ .dockerignore # Docker ignore files
βββ .env.example # Environment variables example
βββ .gitignore
βββ README.md
SQLite database is automatically created at data/hashtag_bot.db.
- users - Bot users with authorization flag
- categories - Hashtag categories
- hashtags - All hashtags
- category_hashtags - Category-hashtag links (many-to-many)
- history - Saved batch history (up to 10 per user)
- aiogram 3.16.0 - Async framework for Telegram Bot API
- aiosqlite 0.20.0 - Async SQLite operations
- python-dotenv 1.0.1 - Environment variable management
- Docker & Docker Compose - Containerization
| Variable | Description | Default |
|---|---|---|
BOT_TOKEN |
Telegram bot token from @BotFather | Required |
ACCESS_CODE |
Access code for authorization | Required |
In bot/handlers/categories.py:
CATEGORIES_PER_PAGE = 10 # Categories per page
HASHTAGS_PER_PAGE = 15 # Hashtags per pageIn bot/utils/generator.py:
FIXED_HASHTAG = "#fyp" # Always added at the end- β
.envfile with token added to.gitignore - β Middleware checks authorization before each action
- β Database protected from SQL injection (parameterized queries)
- β Token and access code stored in environment variables
- Never commit
.envto git - Use strong
ACCESS_CODEfor production - Regularly backup
data/hashtag_bot.db
- Check
.envfile exists - Ensure token is in format
BOT_TOKEN=your_token
- Check
main.pyor Docker container is running - Verify token is correct
- Check logs:
docker-compose logs -f
- Authorize:
/auth your_code
- Add more hashtags to categories
- Use more categories for generation
- Check
./datadirectory exists - Ensure volume is mounted:
docker-compose ps - Check permissions:
ls -la ~/hashtag-bot/data/
Locally:
# Logs output to console
python main.pyDocker:
# View logs
docker-compose logs -f
# Last 100 lines
docker-compose logs --tail=100
# Logs with timestamps
docker-compose logs -f -t# Enter container
docker exec -it hashtag-bot sh
# Access database
sqlite3 /app/data/hashtag_bot.db
# Queries
.tables
SELECT * FROM users;
SELECT * FROM categories;
.exit- On local machine:
git pull
docker-compose down
docker-compose up -d --build- On server:
# Copy new files
scp -r ./bot ./main.py ./requirements.txt dev2.tech.apree:~/hashtag-bot/
# On server
ssh dev2.tech.apree
cd ~/hashtag-bot
docker-compose down
docker-compose up -d --buildDatabase supports automatic migrations when adding new fields.
If you encounter issues:
- Check logs:
docker-compose logs -f - Ensure all environment variables are configured
- Create an Issue in the repository
MIT
Created with β€οΈ using Claude Code
Bot: @hashtags_apree_bot