A production-ready, scalable Telegram bot for downloading music from Spotify with advanced features.
- Multi-format downloads: Tracks, albums, playlists
- Quality selection: 128 kbps / 320 kbps
- Metadata extraction: Artist, title, album
- Progress updates: Real-time status messages
- Smart validation: Strict Spotify URL checks
- Rate limiting: Flood protection (default: 5 requests / 60s)
- Error handling: Robust recovery with helpful messages
- Logging: Structured logging with rotation
- Config management: Environment-driven settings
- Dependencies: Automatic FFmpeg installation
- Multilingual: English & Persian (Farsi)
- Commands:
/start,/help - Inline UI: Quality selector keyboards
- File hygiene: Auto-cleanup & size limits
- Modular architecture
- Type hints throughout
- Well-documented code
- Extensible for new features
Important: Before running the bot, verify Spotify API connectivity with Spotify-API-Test.
- Python 3.8+
- Telegram Bot Token (from @BotFather)
- Git
- Ubuntu/Debian (for automatic FFmpeg installation)
git clone https://github.com/power0matin/Telegram-Music-Downloader-Bot.git
cd Telegram-Music-Downloader-Bot
python3 -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txtexport BOT_TOKEN="your_bot_token_here"
# Optional tweaks
export RATE_LIMIT_REQUESTS="5"
export RATE_LIMIT_WINDOW_SECONDS="60"
export DEFAULT_QUALITY="320"
export MAX_DOWNLOAD_SIZE_MB="50"
export LOG_LEVEL="INFO"python bot.py| Variable | Description | Default |
|---|---|---|
BOT_TOKEN |
Required β Telegram bot token | β |
DOWNLOAD_DIR |
Storage directory for downloads | downloads |
QUEUE_DIR |
Directory for queue files | queue |
MAX_DOWNLOAD_SIZE_MB |
Max file size (MB) | 50 |
RATE_LIMIT_REQUESTS |
Max requests per window | 5 |
RATE_LIMIT_WINDOW_SECONDS |
Window length (seconds) | 60 |
DEFAULT_QUALITY |
Default audio quality (128 or 320) |
320 |
LOG_LEVEL |
Logging level | INFO |
SUPPORTED_LANGUAGES |
Comma-separated locale list | en,fa |
DEFAULT_LANGUAGE |
Default locale | en |
sudo cp systemd/spotify-bot.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable spotify-bot
sudo systemctl start spotify-botCheck status/logs:
sudo systemctl status spotify-bot
sudo journalctl -u spotify-bot -f# Build
docker build -t spotify-bot .
# Run
docker run -d \
--name spotify-bot \
-e BOT_TOKEN="your_token_here" \
-v "$(pwd)"/downloads:/app/downloads \
-v "$(pwd)"/logs:/app/logs \
spotify-botβββ bot.py # Entry point
βββ config.py # Configuration manager
βββ requirements.txt
βββ handlers/
β βββ command_handler.py # /start, /help
β βββ spotify_handler.py # URL processing
β βββ callback_handler.py # Inline callbacks
βββ utils/
β βββ downloader.py # Download engine
β βββ spotify_utils.py # URL validation & metadata
β βββ rate_limiter.py # Throttling
β βββ i18n.py # Internationalization
β βββ logging_config.py # Logging setup
β βββ queue_functions.py # Queue ops
βββ systemd/
βββ scripts/
βββ docs/
Core Classes
SpotifyBotβ lifecycle & orchestrationSpotifyDownloaderβ high-level download engineRateLimiterβ user throttlingMessagesβ multilingual text providerSpotifyURLValidatorβ URL parsing/validation
Key Functions
download_and_send()β Orchestrates end-to-end flowvalidate_spotify_url()β Ensures supported URL typesget_spotify_metadata()β Fetches metadatacheck_rate_limit()β Enforces throttling
# Direct
https://open.spotify.com/track/4iV5W9uYEdYUVa79Axb7Rh
https://open.spotify.com/album/1DFixLWuPkv3KT3TnV35m3
https://open.spotify.com/playlist/37i9dQZF1DX0XUsuxWHRQd
# With params (cleaned automatically)
https://open.spotify.com/track/4iV5W9uYEdYUVa79Axb7Rh?si=abc123
# URI format
spotify:track:4iV5W9uYEdYUVa79Axb7Rh
spotify:album:1DFixLWuPkv3KT3TnV35m3
# Regional
https://open.spotify.com/de/track/4iV5W9uYEdYUVa79Axb7Rh
# utils/i18n.py
TRANSLATIONS = {
"en": {
"welcome": "Your custom welcome message",
"help": "Your custom help text",
}
}from utils.rate_limiter import RateLimiter
rate_limiter = RateLimiter(
max_requests=10,
window_seconds=120
)export LOG_LEVEL="DEBUG" # DEBUG, INFO, WARNING, ERROR
export LOG_FILE="custom.log"FFmpeg not found
sudo apt update && sudo apt install ffmpegPermission denied
chmod +x start_bot.sh
chown -R $USER:$USER downloads/ logs/Too many requests
- Increase
RATE_LIMIT_REQUESTS - Or reduce
RATE_LIMIT_WINDOW_SECONDS
High memory usage
- Lower
MAX_DOWNLOAD_SIZE_MB - Enable log rotation
Debug mode
export LOG_LEVEL="DEBUG"
python bot.pyTail logs
tail -f logs/spotify_bot.log
grep "ERROR" logs/spotify_bot.log
grep "Download" logs/spotify_bot.log- Fork the repo
- Create a branch:
git checkout -b feature/amazing-feature - Commit:
git commit -m 'Add amazing feature' - Push:
git push origin feature/amazing-feature - Open a PR
Dev setup
pip install -r requirements.txt
export LOG_LEVEL="DEBUG"
python bot.py
# Tests (when available)
pytest tests/MIT β see LICENSE.
This bot is for educational use only. You are responsible for complying with Spotifyβs Terms of Service and copyright laws. The authors assume no liability for misuse.
Matin Shahabadi (Ω ΨͺΫΩ Ψ΄Ψ§ΩβΨ’Ψ¨Ψ§Ψ―Ϋ / Ω ΨͺΫΩ Ψ΄Ψ§Ω Ψ’Ψ¨Ψ§Ψ―Ϋ)
- π Issues: https://github.com/power0matin/Telegram-Music-Downloader-Bot/issues
- π¬ Telegram: @power0matin
- π Website: matinshahabadi.ir
- π§ Email: me@matinshahabadi.ir
- π§βπ» GitHub: power0matin
- πΌ LinkedIn: matin-shahabadi
Made with β€οΈ by power0matin β If this project helps you, please star the repo!