A Telegram bot built on Cloudflare Workers that automatically uploads files and photos sent to it directly to Google Drive.
- 📁 Automatic File Upload: Uploads any document sent to the bot directly to Google Drive
- 📷 Photo Support: Handles photo uploads with automatic naming
- ☁️ Serverless: Runs on Cloudflare Workers for zero-cost hosting
- 🔒 Secure: Uses Google Service Account authentication
- 📱 Real-time Notifications: Sends confirmation messages to admin
- User sends a file or photo to the Telegram bot
- Bot downloads the file from Telegram servers
- Authenticates with Google Drive using service account credentials
- Uploads the file to a specified Google Drive folder
- Sends confirmation message to admin
Use the Deploy to Cloudflare button above for the fastest setup experience.
Before you begin, make sure you have:
- Cloudflare account (free tier available)
- Node.js (version 16 or higher)
- Git installed on your machine
-
Create Google Cloud Project
- Go to Google Cloud Console
- Create a new project or select an existing one
- Note down your project ID
-
Enable Google Drive API
- In the Google Cloud Console, go to "APIs & Services" > "Library"
- Search for "Google Drive API" and click "Enable"
-
Create Service Account
- Go to "APIs & Services" > "Credentials"
- Click "Create Credentials" > "Service Account"
- Fill in the service account details and create
- Click on the created service account
- Go to "Keys" tab > "Add Key" > "Create New Key"
- Choose JSON format and download the file
- Keep this JSON file secure - it contains sensitive credentials
-
Setup Google Drive Folder
- Create a folder in your Google Drive where files will be uploaded
- Right-click the folder > "Share"
- Add the service account email (found in the JSON file) as an editor
- Copy the folder ID from the URL (e.g.,
https://drive.google.com/drive/folders/FOLDER_ID_HERE)
-
Create Telegram Bot
- Message @BotFather on Telegram
- Send
/newbotcommand - Follow the prompts to choose a name and username
- Save the bot token provided
-
Get Your Chat ID
- Message @userinfobot to get your chat ID
- Or send a message to your bot and visit:
https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates - Look for
"chat":{"id":CHAT_ID}in the response
You'll need these environment variables. Keep them secure!
| Variable | Description | Example |
|---|---|---|
TELEGRAM_BOT_TOKEN |
Bot token from BotFather | 123456789:ABCdefGHIjklMNOpqrsTUVwxyz |
GOOGLE_DRIVE_FOLDER_ID |
Google Drive folder ID | 1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms |
ADMIN_CHAT_ID |
Your Telegram chat ID | 123456789 |
GOOGLE_CREDENTIALS |
Service account JSON (as string) | {"type":"service_account",...} |
-
Clone the repository
git clone https://github.com/xixu-me/gdrive-upload-robot.git cd gdrive-upload-robot -
Install dependencies
npm install
-
Login to Cloudflare
npx wrangler login
-
Configure secrets (for production)
# Add bot token npx wrangler secret put TELEGRAM_BOT_TOKEN # Add Google Drive folder ID npx wrangler secret put GOOGLE_DRIVE_FOLDER_ID # Add your chat ID npx wrangler secret put ADMIN_CHAT_ID # Add Google credentials (paste the entire JSON content) npx wrangler secret put GOOGLE_CREDENTIALS
-
Deploy to Cloudflare Workers
npx wrangler deploy
-
Set up Telegram webhook
curl -X POST "https://api.telegram.org/bot<YOUR_BOT_TOKEN>/setWebhook" \ -H "Content-Type: application/json" \ -d '{"url": "https://your-worker.your-subdomain.workers.dev"}'
Replace
<YOUR_BOT_TOKEN>with your actual bot token and update the URL with your worker's URL. -
Test the bot
- Send a file or photo to your Telegram bot
- Check if it appears in your Google Drive folder
- You should receive a confirmation message
- Bot not responding: Check if the webhook is set correctly
- Upload fails: Verify Google Drive folder permissions and service account access
- Authentication errors: Ensure all environment variables are set correctly
- Local development: Use
npx wrangler devfor local testing
The worker expects POST requests from Telegram webhooks with the following payload structure:
{
"message": {
"document": {
"file_id": "telegram_file_id",
"file_name": "document.pdf"
}
}
}{
"message": {
"photo": [
{
"file_id": "telegram_file_id",
"width": 1280,
"height": 720
}
]
}
}- Environment variables containing sensitive data are stored securely in Cloudflare Workers
- Google Service Account credentials should have minimal required permissions
- Bot token should be kept secret and not committed to version control
- Consider implementing rate limiting for production use
This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.
- Built with Cloudflare Workers
- Uses Google Drive API
- Integrates with Telegram Bot API