A professional-grade Telegram bot that automatically deletes messages in groups and channels based on time thresholds. Built with modern Python and running on GitHub Actions.
TeleClean is a Telegram bot that automatically cleans up messages in groups and channels based on configurable time thresholds. It runs independently on GitHub Actions every 15 minutes, providing an effortless way to maintain clean conversation spaces.
- Fully automated - Runs entirely via GitHub Actions
- Customizable thresholds - Set different deletion times for:
- Regular user messages
- All messages (including bot messages)
- Exempt users - Configure specific users whose messages won't be deleted
- Detailed alerts - Receive notifications about:
- Bot startup
- Live deletion progress
- Completion statistics
- Detailed error reports
- Dry run mode - Test deletion rules without actually removing messages
- MongoDB integration - Reliable storage of chat data and deletion history
- Python 3.10 or higher
- MongoDB database (cloud or self-hosted)
- Telegram Bot Token (from @BotFather)
- GitHub account (for running GitHub Actions)
-
Bot Creation
- Create a new bot with @BotFather
- Turn OFF privacy mode (important for group message access)
- Copy your bot token
-
MongoDB Setup
- Create a MongoDB database (free tier on MongoDB Atlas works fine)
- Get your MongoDB URI connection string
-
GitHub Repository
-
Fork or clone this repository
-
Set up the following GitHub Secrets:
Secret Description Default BOT_TOKENTelegram Bot Token (required) MONGO_URIMongoDB Connection URI (required) OWNER_IDYour Telegram User ID (required) USER_MESSAGESMinutes to keep user messages 60 (1 hour) ALL_MESSAGESMinutes to keep all messages 1440 (24 hours) EXCEPTIONSComma-separated list of exempt user and channel IDs (empty) DRY_RUN"True" to simulate deletions without deleting False LOG_LEVELLogging level (INFO, DEBUG, etc.) INFO
-
- Add your bot to a group or channel
- Make the bot an admin with at minimum the "Delete Messages" permission
- The bot will automatically detect it's been added and start tracking the chat
- Messages will be deleted based on your configured thresholds during each run
You can exempt specific users and channels from message deletion:
- User Exceptions: Add user IDs as positive integers
- Bot Exceptions: Add bot IDs as positive integers (they're treated like users)
- Channel Exceptions: Add channel IDs as negative integers (usually starting with -100)
Example format for the EXCEPTIONS environment variable:
12345,-1001234567890,98765,5273782385
This would exempt:
- User with ID 12345
- Channel with ID -1001234567890
- User with ID 98765
- Bot with ID 5273782385
To get various IDs:
- For User IDs: Send "/start" to @userinfobot
- For Bot IDs: Forward a bot's message to @userinfobot (same format as user IDs)
- For Channel IDs: Forward a message from the channel to @userinfobot
- For Channel IDs by username: Use @username_to_id_bot
When setting the environment variable, don't include spaces between IDs.
For GitHub Actions:
env:
EXCEPTIONS: "12345,-1001234567890,98765,5273782385"In PowerShell (local testing):
$env:EXCEPTIONS = "12345,-1001234567890,98765,5273782385"In Bash (Linux or macOS):
export EXCEPTIONS="12345,-1001234567890,98765,5273782385"/start- Initialize the bot (in private chat) or verify it's active (in groups)
To run the bot locally:
- Clone the repository
- Install dependencies:
pip install -r requirements.txt
- Set up environment variables:
export BOT_TOKEN="your_bot_token" export MONGO_URI="your_mongodb_uri" export OWNER_ID="your_telegram_id" export DRY_RUN="True" # For testing without deleting
- Run the bot:
python main.py
TeleClean/
βββ main.py # Entry script
βββ start.py # GitHub Actions entrypoint script
βββ config.py # Static & env-based config variables
βββ requirements.txt
βββ README.md
βββ bot/
β βββ __init__.py
β βββ __main__.py
β βββ handlers/
β β βββ config_handler.py # Load configs from ENV if missing
β β βββ chat_handler.py # Track which chats/channels the bot is in
β β βββ executor.py # Core deletion logic
β β βββ error.py # Custom error handler
β β βββ handlers.py # Import & run all handler modules
β βββ alerts/
β β βββ start.py # Alert on startup
β β βββ update.py # Live update alerts every 5s
β β βββ stop.py # Alert on successful stop
β β βββ error.py # Alert with full traceback on failure
β βββ database/
β β βββ database.py # MongoDB interface, create db/collections
β β βββ analyzer.py # Scan & clean invalid chat records
βββ .github/
β βββ workflows/
β βββ teleclean.yml # Run every 15 minutes
The bot uses MongoDB with the following collections:
- records.botpm: Private chat users
- records.chats: Groups the bot is in
- records.channels: Channels the bot is in
- history.interaction: Log of bot additions/removals
- history.deletion: Log of message deletions
The bot runs automatically every 15 minutes via GitHub Actions. The workflow:
- Sets up Python 3.10
- Installs dependencies
- Loads secrets as environment variables
- Runs the bot via
start.py - Uploads logs as artifacts
The modular design makes it easy to extend:
- Add new handlers in
bot/handlers/ - Add new alert types in
bot/alerts/ - Extend database functionality in
bot/database/
The bot includes comprehensive error handling:
- Catches and reports all exceptions with full tracebacks
- Handles Telegram API errors gracefully
- Automatically notifies the owner of critical issues
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
This bot deletes messages. Always test with DRY_RUN=True before using in important groups.
Made with β€οΈ by [Your Name]