A Python script to download files from a given Telegram channel. It supports filtering by file type (e.g., images, PDFs).
- Download all files from a specified Telegram channel.
- Filter downloads by file type (e.g., images, PDFs, videos).
- Save files to a specific directory.
- Easy setup with environment variables for secure credential management.
-
Python 3.12 or higher installed on your system.
-
telethon
library 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_id
andyour_api_hash
with the actual values obtained from Telegram.
The first time you run the script, Telethon will authenticate your account to create a session. You can authenticate using:
-
Phone Number:
- Telethon sends a code to your Telegram app or via SMS.
- Enter this code in the terminal when prompted.
-
Bot Token:
- If you're using a bot, you can authenticate with the bot token instead of a phone number.
- Obtain the bot token from BotFather.
Telethon saves this authentication data in a session_name.session
file in the current directory. This session file allows you to skip authentication on subsequent runs.
python main.py @channel_name --format images --output ./downloads
@channel_name
: The username or ID of the Telegram channel.--format
or-f
: The file type to download (e.g.,images
,pdf
). If omitted, all file types are downloaded.--output
or-o
: The directory to save the files. Defaults to the current directory.
- 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.
If you have suggestions or issues, please open an issue or pull request. All contributions are welcomed.