A Cloudflare Worker-based Telegram bot that dynamically determines the type of files from user-provided URLs and uploads them to Telegram. This bot supports photos, videos, audio files, and documents, ensuring each file is uploaded in its correct format.
- 📷 Photo Uploads: Automatically identifies image formats (e.g., JPG, PNG, GIF) and uploads them as photos.
- 🎥 Video Uploads: Handles video formats (e.g., MP4, AVI, WEBM) and uploads them as native videos.
- 🎵 Audio Uploads: Recognizes audio formats (e.g., MP3, WAV) and uploads them as music/audio.
- 📄 Document Uploads: All other files are uploaded as generic documents.
- 🚦 File Size Limit: Ensures files are within Telegram's 50 MB limit.
- 🔒 Secure Webhook: Includes a secret token for validating incoming webhook requests.
- Visit @BotFather on Telegram.
- Create a new bot using the
/newbot
command. - Copy the provided bot token (e.g.,
123456789:ABCDefghIJKLmnopQRSTuvWXyZ1234567
).
git clone https://github.com/your-username/telegram-file-bot.git
cd telegram-file-bot
You need to configure the following environment variables:
Variable | Description |
---|---|
ENV_BOT_TOKEN |
Your Telegram bot token. |
ENV_BOT_SECRET |
A unique secret token for security. |
Set these variables via the Cloudflare dashboard or CLI:
Using Dashboard:
- Go to the Workers page in the Cloudflare dashboard.
- Click on your worker and go to Settings > Variables.
- Add
ENV_BOT_TOKEN
andENV_BOT_SECRET
.
Using Wrangler CLI:
wrangler secret put ENV_BOT_TOKEN
wrangler secret put ENV_BOT_SECRET
- Install the Wrangler CLI if not already installed:
npm install -g wrangler
- Publish the worker:
wrangler publish
After deploying the worker, set the webhook for your Telegram bot:
- Replace
<YOUR_WORKER_URL>
with your Cloudflare Worker URL (e.g.,https://your-bot-name.workers.dev
). - Run this command:
curl -F "url=<YOUR_WORKER_URL>/endpoint" \ -F "secret_token=your-secret-token" \ https://api.telegram.org/bot<YOUR_BOT_TOKEN>/setWebhook
- JPG, JPEG, PNG, GIF, BMP, WEBP, SVG
- MP4, AVI, MOV, MKV, WMV, FLV, WEBM
- MP3, WAV, AAC, FLAC, OGG, M4A
- All other files will be uploaded as generic documents.
- Users send a URL to the bot on Telegram.
- The bot:
- Downloads the file from the provided URL.
- Determines its type using the MIME type and file extension.
- Uploads it in the correct format using the Telegram Bot API.
- If the file size exceeds 50 MB, the bot responds with:
The file is too large to send (maximum: 50 MB).
- If the URL is invalid or the download fails, the bot responds with:
Please send a valid URL to download the file.
- If Telegram API encounters an issue, the bot provides an error message.
- ✅ Support for ZIP or RAR archives with automatic extraction.
- ✅ Support for text and markdown messages for additional context.
- ✅ Progress indicators for large file downloads.
We welcome contributions to improve the bot. Here's how you can contribute:
- Fork the repository.
- Create a new branch:
git checkout -b feature-name
- Commit your changes:
git commit -m "Add feature-name"
- Push to your branch:
git push origin feature-name
- Open a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.