✔ Clear separation of concerns
✔ No circular imports
✔ Easy to reason about
telegram-bot/
├── bot.py
├── requirements.txt
├── README.md
├── .gitignore
├── .*.session # ❌ not committed (server only)
├── .env # ❌ not committed (server only)
│
├── config/
│ ├── __init__.py
│ ├── env.py
│ ├── forwarding.py
│ └── moderation.py
│
├── core/
│ ├── __init__.py
│ ├── client.py
│ └── startup.py
│
├── handlers/
│ ├── __init__.py
│ ├── forward.py
│ └── moderation.py
│
├── utils/
│ ├── __init__.py
│ ├── logger.py
│ └── messages.py
│
├── systemd/
│ ├── telegram-bot.service
│ ├── telegram-bot-update.service
│ ├── telegram-bot-update.timer
│ └── update.sh
│
└── venv/ # ❌ not committed (server only)
✔ .env ignored in Git
✔ Telethon session files ignored
✔ Dedicated Telegram account (best practice)
✔ No hardcoded secrets
✔ Non-root systemd execution
config/env.py
✔ Validates API_ID / API_HASH
✔ Defaults handled safely
✔ Clean env loading config/forwarding.py
✔ Explicit channel → topic mapping
✔ Media-only channel rules
✔ Extension whitelist config/moderation.py
✔ Topic-specific rules
✔ Clear permission model
core/client.py
✔ Single shared client
✔ Correct session usage core/startup.py
✔ Graceful shutdown (SIGTERM / SIGINT)
✔ systemd-friendly
✔ Clean disconnect
✔ Proper exit codes
handlers/forward.py
✔ Only listens to configured source channels
✔ Album forwarding supported
✔ Media filtering enforced
✔ Edit → delete & repost implemented
✔ FloodWait-safe
✔ Exception-isolated
✔ Forward tracking prevents duplicates
handlers/moderation.py
✔ Topic-based rules
✔ Bot exempt
✔ Anonymous admins exempt
✔ Correct forum topic detection
✔ Safe deletes
✔ Temporary reason messages
✔ Clean logging
utils/messages.py
✔ Markdown-safe
✔ User mention safe
✔ Auto-delete TTL
✔ Exception-proof
✔ No UX regressions
utils/logger.py
✔ Single named logger
✔ No duplicate handlers
✔ Journal-friendly output
✔ Readable format
systemd/update.sh
✔ Pulls only when changes exist
✔ No unnecessary restarts
✔ Virtualenv safe
✔ Clear logs
✔ Fail-fast scripting telegram-bot-update.timer
✔ Hourly checks (safe)
✔ Persistent
✔ Low wake-ups telegram-bot-update.service
✔ Sandboxed
✔ No system file access
✔ Network-aware
telegram-bot.service
✔ Non-root user
✔ Auto-restart
✔ Crash protection
✔ Clean shutdown integration
✔ Journal logging
requirements.txt
✔ Minimal
✔ Correct versions implied
✔ No unused libraries
Actual usage:
~17 source channels
~5 messages/day
Bot can safely handle:
✔ 10× load (as per 17 source channels & 5 messages/day)
✔ 24×7 uptime
✔ Long-running sessions
Scenario Outcome
Bot crash systemd restart
Telegram disconnect auto reconnect
FloodWait waits & resumes
Bad message isolated
Bad album skipped
Edit storm safe repost
Update failure no restart
ssh <PRIVATE_KEY> ubuntu@<SERVER_IP>
Update server & install dependencies:
sudo apt update && sudo apt upgrade -y
sudo apt install git python3 python3-pip python3-venv -y
If repo is not cloned
cd /home/ubuntu
git clone https://github.com/DineshValor/telegram-bot.git
If already cloned
cd /home/ubuntu/telegram-bot
git pull origin master
cd /home/ubuntu/telegram-bot
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
deactivate
cp /home/ubuntu/telegram-bot/.env.example /home/ubuntu/telegram-bot/.env
nano /home/ubuntu/telegram-bot/.env
sudo chmod +x /home/ubuntu/telegram-bot/systemd/update.sh
sudo chmod +x /home/ubuntu/telegram-bot/systemd/journal-watcher.sh
Verify:
ls -l /home/ubuntu/telegram-bot/systemd/update.sh
ls -l /home/ubuntu/telegram-bot/systemd/journal-watcher.sh
You should see -rwx.
cd /home/ubuntu/telegram-bot
source venv/bin/activate
python3 bot.py
CTRL+C (Key Press)
Systemd cannot read files from your repo directly. We must copy them.
sudo cp /home/ubuntu/telegram-bot/systemd/telegram-bot.service /etc/systemd/system/
sudo cp /home/ubuntu/telegram-bot/systemd/telegram-bot-update.service /etc/systemd/system/
sudo cp /home/ubuntu/telegram-bot/systemd/telegram-bot-update.timer /etc/systemd/system/
sudo cp /home/ubuntu/telegram-bot/systemd/telegram-bot-journal-watcher.service /etc/systemd/system/
sudo cp /home/ubuntu/telegram-bot/systemd/telegram-bot-failure.service /etc/systemd/system/
sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl enable telegram-bot
sudo systemctl enable telegram-bot-update.timer
sudo systemctl enable telegram-bot-journal-watcher
sudo systemctl start telegram-bot
sudo systemctl start telegram-bot-journal-watcher
sudo systemctl start telegram-bot-update.timer
systemctl status telegram-bot --no-pager
systemctl status telegram-bot-journal-watcher --no-pager
systemctl status telegram-bot-update.timer --no-pager
Run update service manually:
sudo systemctl start telegram-bot-update.service
check logs
systemctl status telegram-bot-update.service --no-pager -l
Expected behavior:
• If no new commit → “No updates found”
• If new commit → pull → restart bot
journalctl -u telegram-bot -f
journalctl -u telegram-bot-update
systemctl list-timers
chmod +x /home/ubuntu/telegram-bot/systemd/update.sh
sudo systemctl daemon-reload
journalctl -u telegram-bot --no-pager
git stash
git pull
git stash pop
Start bot
sudo systemctl start telegram-bot
Restart bot
sudo systemctl restart telegram-bot
Stop bot
Stop bot
sudo systemctl stop telegram-bot
View logs
journalctl -u telegram-bot -f
sudo git config --global --add safe.directory /home/ubuntu/telegram-bot