A Python script to download files from Telegram entities (channels, groups, chats, or saved messages). It supports filtering by file type (e.g., images, PDFs).
- Telegram-File-Downloader
- Download files from channels, groups, private chats, or your saved messages.
- Filter downloads by file type (e.g., images, PDFs, videos).
- Save files to a specific directory.
- List all channels, groups, and chats you have access to.
- Easy setup with environment variables for secure credential management.
-
Python 3.12 or higher installed on your system.
-
telethonlibrary installed. You can install all the required libraries using:pip3 install -r requirements.txt
-
A Telegram account or bot token.
To use this script, you need an API ID and API Hash from Telegram. Follow these steps:
- Open your browser and go to Telegram's My Apps page.
- Log in with your Telegram account credentials.
- Click on Create New Application.
- Fill in the required details.
- After creating the application, you will see your API ID and API Hash. Copy these values.
To securely store your credentials:
-
In the project directory, create a file named
.env. -
Add the following lines to the file:
TELEGRAM_API_ID=your_api_id TELEGRAM_API_HASH=your_api_hash
Replace
your_api_idandyour_api_hashwith the actual values obtained from Telegram.
To see all channels, groups, and chats you have access to:
python main.py --listThis will display a table with:
- ID: The numeric identifier of the entity
- Type: The type of entity (Channel (public/private), User, Chat, etc.)
- Name: The display name
- Username: The public username (if available)
Basic syntax:
python main.py <entity> --format <file_type> --output <directory> --limit <number>Examples:
# Download from a public channel
python main.py @channel_name --format images --output ./downloads --limit 100
# Download from your Saved Messages
python main.py me --format videos --output ./downloads --limit 100
# Download from a private channel, private group or user chat using its ID
python main.py -1001234567891 --format images --output ./downloads --limit 100
# Download a specific file type (pdf)
python main.py @channel_name --format pdf --output ./downloads --limit 0
# Download all media types (no filter)
python main.py @channel_name --output ./downloads --limit 0Arguments:
<entity>: The entity to download from. Can be:- Public channel username:
@channel_nameorchannel_name - Entity ID:
-1001234567891(use--listto find IDs) - Saved Messages:
meorself
- Public channel username:
--formator-f: Filter by file type category or specific extension.- Categories:
images,documents,videos,audios,archives - Specific extensions:
pdf,jpg,png,mp4,zip, etc. - If omitted, all media types are downloaded.
- Categories:
--outputor-o: The directory to save downloaded files. Defaults to the current directory.--limitor-l: The maximum number of messages to fetch. Use0to fetch all messages. Defaults to 100.
Note: You can customize supported extensions for each category in the code's FILE_CATEGORIES dictionary.
Note: The --limit parameter specifies how many messages to check, not how many files to download. For example, --format pdf --limit 10 will check the 10 most recent messages and download any PDFs found among them.
- Avoid sharing the .env file.
- Avoid sharing the
session_name.sessionfile, as it contains encrypted data that could potentially be misused to access your account.
The Telethon library widely uses the concept of “entities”. An entity will refer to any User, Chat or Channel object that the API may return in response to certain methods, such as GetUsersRequest.
Learn more: Telethon Entities Documentation
Use the --list command to see all your accessible dialogs with their IDs:
python main.py --listNote: You must be a member of the channel/group or have an existing chat with the user to download files.
No. Telegram bots have restricted API access and cannot retrieve message history from channels using the methods this script requires (iter_messages). You must use a user account (phone number authentication) for this script to work properly.
You can filter by category or specific extension:
Categories: images, documents, videos, audios, archives
Specific extensions: pdf, jpg, png, mp4, mp3, zip, etc.
TODO: there are some bugs for file formats. should be fixed later.
If you have suggestions or issues, please open an issue or pull request. All contributions are welcomed.