How asmsafone/musicplayer works
Purpose and runtime
MusicPlayer is a Python Telegram music bot. It receives Telegram commands through Pyrogram and streams audio/video into Telegram voice chats through Py-TgCalls. It supports independent playback queues per chat, including YouTube content, playlists, uploaded media, and live stream URLs.
Primary process entry point: main.py. Runtime configuration is loaded from config.py, with environment-variable examples in sample.env.
Application bootstrap and deployment
main.py starts the bot/user session and registers the application behavior implemented under core/. Deployment targets include direct Python execution, Docker, and Heroku-style process startup.
• main.py
• Dockerfile
• Procfile
• startup.sh
• heroku.yml
The host must provide FFmpeg for media processing/streaming. Python dependencies are declared in requirements.txt; the Python runtime version is pinned by runtime.txt.
Telegram command and authorization boundary
Telegram messages are the external control plane. Pyrogram handlers dispatch commands such as play, skip, pause, queue management, language changes, and restart. Admin/sudo restrictions protect sensitive operations and optionally restrict playback requests.
• core/admins.py
• core/decorators.py
• core/groups.py
• main.py
This boundary separates untrusted group/user commands from operations that alter voice-chat state, queue state, or process lifecycle.
Playback orchestration and per-chat queues
The queue subsystem holds playback state separately for each Telegram chat. Commands append tracks, inspect/reorder queues, loop tracks, advance/skip, clear queues, and import/export queue contents. Playback control coordinates this state with the voice-chat streaming layer.
• core/queue.py
• core/song.py
• core/stream.py
The central control flow is: command request → resolve a playable source → enqueue or replace the chat’s current item → start/alter Py-TgCalls stream → advance according to completion, skip, or loop behavior.
Media source resolution and streaming pipeline
Track handling accepts search terms, YouTube links/live streams/playlists, Telegram media, and custom URL streams. It is designed to begin streaming while download/conversion progresses rather than materializing persistent media files. FFmpeg is the key media runtime boundary; upstream providers include YouTube and optionally Spotify credentials for source metadata/resolution.
• core/song.py
• core/stream.py
• core/funcs.py
core/song.py represents or resolves requested media, while core/stream.py supplies the stream to Telegram voice chat and applies configured audio/video mode and quality.
Localization and user-facing responses
Command responses are language-driven rather than hardcoded to one locale. The selected language can be changed per group, and translation catalogs provide the user-visible command/status strings.
• lang/__init__.py
• lang/en.json
• lang/ar.json
• lang/tr.json
Configuration and external services
Required external identities are Telegram API credentials plus a Pyrogram string session; an optional bot token supports bot operation. Telegram is both the command transport and voice-chat platform. Spotify is optional. Environment settings also govern sudo users, command prefixes, default language, media mode, quality, and admin-only playback.
• config.py
• sample.env
• genStr.py
genStr.py is the session-generation entry point for creating the Pyrogram session value consumed by the main runtime.
Main entry points: main.py Command Handlers · song.py Source Resolution · stream.py Voice Chat Stream · queue.py Per-Chat Queues
Components 17
Runtime & Deployment
- Application Bootstrap · Starts sessions and application behavior.
main.py - Runtime Configuration · Loads credentials and playback settings.
config.py - Deployment Launchers · Supports Docker, Heroku, and direct process startup.
Dockerfile - Session Generator · Creates the Pyrogram session value for runtime use.
genStr.py
Telegram Control Plane
- Command Handlers · Dispatches playback, queue, language, and restart commands.
main.py - Admin & Sudo Guard · Restricts sensitive and optionally playback operations.
core/admins.py - Group Policy
core/groups.py
Playback Pipeline
- Per-Chat Queues · Maintains independent playback state for each chat.
core/queue.py - Source Resolution · Resolves search, links, playlists, uploads, and stream URLs.
core/song.py - Playback Control · Coordinates queue changes, completion, skip, and looping.
core/stream.py - FFmpeg · Processes media for audio or video streaming.
- Voice Chat Stream · Streams resolved media into Telegram voice chats.
core/stream.py - Media Utilities
core/funcs.py
Localization
- Translation Catalogs · Provides language-driven command and status text.
lang/en.json - Group Language · Stores or applies the selected group language.
lang/__init__.py
External Platforms
- Telegram · Provides message transport and voice chats.
- YouTube & Spotify · Supply playable sources and optional metadata resolution.
Connections 20
- Runtime Configuration → Application Bootstrap: configures
- Session Generator → Runtime Configuration: creates session for
- Deployment Launchers → Application Bootstrap: launches
- Application Bootstrap → Command Handlers: registers
- Command Handlers → Admin & Sudo Guard: checks
- Command Handlers → Group Policy: applies settings
- Command Handlers → Source Resolution: resolves request
- Command Handlers → Per-Chat Queues: updates
- Admin & Sudo Guard → Playback Control: permits control
- Per-Chat Queues → Playback Control: supplies current item
- Source Resolution → Per-Chat Queues: enqueues playable item
- Source Resolution → YouTube & Spotify: resolves from
- Source Resolution → Media Utilities: uses
- Playback Control → Voice Chat Stream: starts or alters
- Voice Chat Stream → FFmpeg: processes through
- Voice Chat Stream → Telegram: streams into
- Command Handlers → Group Language: changes
- Group Language → Translation Catalogs: selects
- Command Handlers → Telegram: receives messages from
- Application Bootstrap → Telegram: opens Pyrogram session