An asynchronous Python automation script that extracts PDF documents from a specific Telegram channel and securely backs them up to a Google Drive folder.
This tool is designed to be lightweight and storage-efficient. It downloads files locally, tracks the transfer with visual progress bars, streams the upload to Google Drive in chunks, and automatically deletes the local copy once the upload is verified.
- Asynchronous Operations: Built with
Telethonandasynciofor efficient Telegram message parsing and media downloading. - Smart Filtering: Automatically scans the channel history and strictly targets
application/pdfMIME types. - Visual Progress Tracking: Features custom CLI progress bars (
[██████████----------] 50.0%) for both the Telegram download and the Google Drive upload phases. - Chunked Uploads: Uploads to Google Drive using 2MB chunks (
resumable=True), making it stable for larger PDF files. - Zero Storage Footprint: Automatically cleans up (deletes) the local PDF file immediately after a successful upload to Drive.
- First-Run Auth: Automatically handles Google Drive OAuth2 flow and generates a reusable
token.jsonfor subsequent background runs.
Before you begin, ensure you have the following installed and set up:
- Python 3.7+
- A Telegram account to generate API credentials.
- A Google account to generate Drive API credentials.
- Log in to your Telegram core portal: https://my.telegram.org/
- Go to API development tools.
- Create a new application to get your
api_idandapi_hash.
- Go to the Google Cloud Console.
- Create a new project and enable the Google Drive API.
- Go to Credentials -> Create Credentials -> OAuth client ID (Choose "Desktop App").
- Download the JSON file and rename it to
credentials.json. - Place
credentials.jsonin the root directory of this project.
1. Clone the repository
git clone https://github.com/yourusername/telegram-to-drive-sync.git
cd telegram-to-drive-sync2. Create a virtual environment (Recommended)
python3 -m venv venv
source venv/bin/activate3. Install dependencies Install the required packages using pip.
pip install telethon google-api-python-client google-auth-httplib2 google-auth-oauthlib4. Configuration
Open telegram_to_drive.py (or your script name) and update the configuration variables at the top of the file with your specific details:
# --- YOUR CONFIGURATION ---
API_ID = 12345678 # Replace with your Telegram API ID (integer)
API_HASH = 'your_api_hash_here' # Replace with your Telegram API Hash (string)
CHANNEL_USERNAME = '@your_channel' # Replace with the target Telegram channel username
DRIVE_FOLDER_ID = 'your_folder_id' # Replace with your target Google Drive Folder IDRun the script from your terminal:
python3 telegram_to_drive.py- Google Auth: A browser window will open asking you to authorize the application to access your Google Drive. Once accepted, a
token.jsonfile will be created locally so you don't have to log in again. - Telegram Auth: The terminal will prompt you to enter your Telegram phone number and the login code sent to your Telegram app. This creates a
.sessionfile. - Execution: The script will scan the channel, download PDFs, and stream them to your Drive.
- Security: Never commit your
credentials.json,token.json, or Telegram session files to GitHub. Ensure they are added to your.gitignorefile. - Rate Limits: Be mindful of Telegram's download limits and Google Drive's API quotas if you are parsing a channel with thousands of heavy PDFs.