This project is a Telegram bot that allows users to download files from Google Drive by sending a Google Drive folder link. The bot is built using the python-telegram-bot
library and the Google Drive API.
- Start the bot with a
/start
command. - Send a Google Drive folder link to the bot to download files from the folder.
- The bot will process the link, authenticate with Google Drive, and download the files.
- Stop the bot with a
/stop
command.
- Python 3.9+
- A Telegram bot token from BotFather
- Google Drive API credentials
-
Clone the repository:
git clone https://github.com/yourusername/telegram-drive-bot.git cd telegram-drive-bot
-
Create a virtual environment and activate it:
python -m venv venv source venv/bin/activate # On Windows, use `venv\Scripts\activate`
-
Install the required packages:
pip install -r requirements.txt
-
Create a .env file in the root directory and add your Telegram bot token:
TELEGRAM_BOT_TOKEN="your-telegram-bot-token"
-
Set up Google Drive API credentials:
- Follow the instructions here to create credentials.json.
- Place the credentials.json file in the root directory of the project.
-
Run the bot:
python telegram_drive_bot.py
-
Start a chat with your bot on Telegram and send the
/start
command. -
Send a Google Drive folder link to the bot. The bot will process the link and download the files from the folder.
-
Stop the bot with the
/stop
command.
This is the main script that contains the bot logic.
-
Imports:
- os: For file operations.
- dotenv: To load environment variables from the .env file.
- telegram: For interacting with the Telegram API.
- telegram.ext: For handling commands and messages.
googleapiclient.discovery
: For interacting with the Google Drive API.google_auth_oauthlib.flow
: For handling OAuth 2.0 authorization.google.auth.transport.requests
: For handling HTTP requests.
-
Functions:
- start(update, context): Sends a welcome message when the
/start
command is issued. - stop(update, context): Sends a message when the
/stop
command is issued and stops the bot. - handle_drive_link(update, context): Handles messages containing Google Drive folder links.
download_files_from_drive(folder_id)
: Downloads files from a Google Drive folder.- main(): Initializes and runs the bot.
- start(update, context): Sends a welcome message when the
-
Main Logic:
- Loads environment variables from the .env file.
- Initializes the bot with the Telegram bot token.
- Adds command handlers for
/start
and/stop
. - Adds a message handler for processing Google Drive folder links.
- Runs the bot using polling.
This file contains environment variables, including the Telegram bot token.
This file contains Google Drive API credentials.
This directory is where downloaded files are stored.
This file stores the Google Drive API token.
This project is licensed under the MIT License. See the LICENSE file for details.