Skip to content
项目

erfanghorbanee/Telegram-File-Downloader

在 GitHub 查看

关于

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).

设计与媒体

这些数字如何统计

可以做什么

  • 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.

实测星标增长

24小时
0
7天
0
15天
0
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

Telegram-File-Downloader

made-with-python Ruff

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).

Table of Contents

Features

  • 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.

Prerequisites

  1. Python 3.12 or higher installed on your system (3.14 recommended).

  2. The required libraries (Telethon and python-dotenv). Install them in a virtual environment:

    python3.14 -m venv .venv
    source .venv/bin/activate
    pip install -r requirements.txt
    
  3. A Telegram account or bot token.

Steps to Get Telegram API Credentials

To use this script, you need an API ID and API Hash from Telegram. Follow these steps:

  1. Open your browser and go to Telegram's My Apps page.
  2. Log in with your Telegram account credentials.
  3. Click on Create New Application.
  4. Fill in the required details.
  5. After creating the application, you will see your API ID and API Hash. Copy these values.

Setting Up the .env File

To securely store your credentials:

  1. In the project directory, create a file named .env.

  2. Add the following lines to the file:

    TELEGRAM_API_ID=your_api_id
    TELEGRAM_API_HASH=your_api_hash
    

    Replace your_api_id and your_api_hash with the actual values obtained from Telegram.

Development

Linting and formatting use Ruff. The version is pinned in requirements-dev.txt:

pip install -r requirements-dev.txt
ruff check .
ruff format .

CI runs these checks, plus a smoke test, on every push and pull request.

Usage

List All Accessible Dialogs

To see all channels, groups, and chats you have access to:

python main.py --list

This 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)

Download Files

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 0

Arguments:

  • <entity>: The entity to download from. Can be:
    • Public channel username: @channel_name or channel_name
    • Entity ID: -1001234567891 (use --list to find IDs)
    • Saved Messages: me or self
  • --format or -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.
  • --output or -o: The directory to save downloaded files. Defaults to the current directory.
  • --limit or -l: The maximum number of messages to fetch. Use 0 to 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.

Security Notes

  • Downloaded filenames are sanitized to replace OS-invalid characters and strip path traversal sequences so files always stay inside the --output directory (falling back to the Telegram message ID if the cleaned name is empty).
  • Avoid sharing the .env file.
  • Avoid sharing the session_name.session file, as it contains encrypted data that could potentially be misused to access your account.

FAQ

What is a Telegram Entity?

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

How do I find the ID of a private channel or group?

Use the --list command to see all your accessible dialogs with their IDs:

python main.py --list

Note: You must be a member of the channel/group or have an existing chat with the user to download files.

Can I use a bot token instead of my phone number?

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.

What file formats are supported?

You can filter by category or specific extension:

Categories: images, documents, videos, audios, archives

Specific extensions: pdf, jpg, png, mp4, mp3, zip, etc.

Contributing

If you have suggestions or issues, please open an issue or pull request. All contributions are welcomed.

探索相关项目

加载中…