├── .gitignore ├── README.md └── worker.js /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | /.wrangler 3 | /node_modules 4 | t.py 5 | demo.pdf 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TG DL Api Workers 2 | 3 | The **TG DL Api Workers** provides download links to various types of files on Telegram, including images, videos, PDFs, APKs, and more. It’s deployable to Cloudflare Workers, and best of all, it’s free! Here are some key features: 4 | 5 | - **Easy to Use:** The API is straightforward to use. 6 | - **Permanent Download Links:** The links generated by this API remain valid over time. 7 | 8 | Total Repo Views 9 | 10 | ## Demo 11 | 12 | Check out the demo PDF download link: https://dl.techzbots.co/file/-1001833807432/3 13 | 14 | **How to Use on Your Own Channel:** 15 | 16 | 1. Add [@TG_DL_Api_Workers_Bot](https://telegram.me/TG_DL_Api_Workers_Bot) to your channel as an admin with edit message rights. 17 | 2. Obtain your Telegram channel ID or username. 18 | 3. Upload a file to your Telegram channel and get its message ID. 19 | 4. Construct the URL using the format: https://dl.techzbots.co/file/CHANNEL_ID/MESSAGE_ID and open it. 20 | 21 | Certainly! Let's improve the "Note" section. Here are the revised instructions: 22 | 23 | ## Important Note 24 | - The bot **edits the file’s caption** on the channel. Ensure that this won’t cause any issues, or consider creating a new **private channel** for use with the bot. 25 | - Use a **Telegram channel**, not a group. 26 | - Your file must **not contain a forward tag**, and it must not be forwarded with a forward tag. 27 | - The bot must have **edit rights inside the channel**. 28 | - To use this API, your file size must be **less than 20 MB**. Wondering why? Read more about it [here](https://core.telegram.org/bots/api#getfile). 29 | 30 | ## Deploying The API 31 | 32 | Follow these steps to deploy the API: 33 | 34 | 1. Create an account on [Cloudflare](https://www.cloudflare.com/) and log in. 35 | 2. Navigate to the `Workers & Pages` tab in the sidebar. 36 | 3. Click on the `Create Application` button. 37 | 4. Create a new worker by clicking on the `Create Worker` button. 38 | 5. Deploy the worker by clicking on the `Deploy` button. 39 | 6. Edit the code by clicking on the `Edit Code` button. 40 | 7. Copy the full code from the [worker.js](./worker.js) file. 41 | 8. Paste the code into Cloudflare. 42 | 9. Create a new bot using [@BotFather](https://telegram.me/BotFather). 43 | 10. Add your bot token to the Cloudflare code at the top, inside the `const BOT_TOKENS = [];` section. 44 | 11. Finally, click on `Deploy` to activate your API. 45 | 46 | Certainly! Let's improve the FAQs section of your project. Here are some suggestions: 47 | 48 | ## Frequently Asked Questions (FAQs) 49 | 50 | 1. **Does this API support groups?** 51 | - No, this API does not support groups. You must use a Telegram channel instead. 52 | 53 | 2. **Do I have to give the bot all admin rights?** 54 | - No, you only need to grant the bot the "edit message" right. Full admin rights are not necessary. 55 | 56 | 3. **How do I get the CHANNEL_ID of my channel?** 57 | - Follow these steps: 58 | 1. Forward any message from your channel to [@MissRose_bot](https://telegram.me/MissRose_bot). 59 | 2. Reply with the command `/id` to the forwarded message. 60 | 3. [@MissRose_bot](https://telegram.me/MissRose_bot) will provide you with the channel ID. 61 | 62 | 4. **How do I get the MESSAGE_ID of my file?** 63 | - Here's how: 64 | 1. Copy the link of the message, which looks like this: `https://t.me/c/1833807432/3`. 65 | 2. The number at the end (in this case, `3`) is the message ID of your file. 66 | 67 | ## 🔔 Join For Latest Updates / Support 68 | 69 | Stay informed about the latest updates by joining our channels / groups : 70 | 71 | [![Telegram Channel](https://img.shields.io/static/v1?label=Join&message=Telegram%20Channel&color=blueviolet&style=for-the-badge&logo=telegram&logoColor=violet)](https://telegram.me/TechZBots) [![Telegram Group](https://img.shields.io/static/v1?label=Join&message=Telegram%20Group&color=blueviolet&style=for-the-badge&logo=telegram&logoColor=violet)](https://telegram.me/TechZBots_Support) 72 | -------------------------------------------------------------------------------- /worker.js: -------------------------------------------------------------------------------- 1 | // Add your bot tokens here, you can add multiple tokens separated by comma 2 | const BOT_TOKENS = []; 3 | // Example : const botTokens = ["bot_token_1", "bot_token_2", "bot_token_3"]; 4 | 5 | 6 | // CORS Fix Start 7 | 8 | const corsHeaders = { 9 | "Access-Control-Allow-Origin": "*", 10 | "Access-Control-Allow-Methods": "GET, HEAD, POST, OPTIONS", 11 | "Access-Control-Allow-Headers": "Content-Type", 12 | }; 13 | 14 | async function handleOptions(request) { 15 | if ( 16 | request.headers.get("Origin") !== null && 17 | request.headers.get("Access-Control-Request-Method") !== null && 18 | request.headers.get("Access-Control-Request-Headers") !== null 19 | ) { 20 | return new Response(null, { 21 | headers: corsHeaders, 22 | }); 23 | } else { 24 | return new Response(null, { 25 | headers: { 26 | Allow: "GET, HEAD, POST, OPTIONS", 27 | }, 28 | }); 29 | } 30 | } 31 | 32 | // CORS Fix End 33 | 34 | 35 | 36 | function getBotToken() { 37 | const pos = Math.floor(Math.random() * BOT_TOKENS.length); 38 | return BOT_TOKENS[pos]; 39 | } 40 | 41 | function generateRandomString(length) { 42 | const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; 43 | let randomString = ''; 44 | for (let i = 0; i < length; i++) { 45 | const randomIndex = Math.floor(Math.random() * chars.length); 46 | randomString += chars.substring(randomIndex, randomIndex + 1); 47 | } 48 | return randomString; 49 | } 50 | 51 | async function getJson(url) { 52 | const response = await fetch(url); 53 | return await response.json(); 54 | } 55 | 56 | async function downloadFile(CHANNEL_ID, message_id) { 57 | const botToken = getBotToken(); 58 | 59 | const x = generateRandomString(10); 60 | let url = `https://api.telegram.org/bot${botToken}/editMessageCaption?chat_id=${CHANNEL_ID}&message_id=${message_id}&caption=${x}`; 61 | console.log(url); 62 | let data = await getJson(url); 63 | console.log(data); 64 | const fileId = data.result.document.file_id; 65 | const file_name = data.result.document.file_name; 66 | const file_size = data.result.document.file_size; 67 | 68 | url = `https://api.telegram.org/bot${botToken}/getFile?file_id=${fileId}`; 69 | console.log(url); 70 | data = await getJson(url); 71 | console.log(data); 72 | const file_path = data.result.file_path; 73 | 74 | url = `https://api.telegram.org/file/bot${botToken}/${file_path}`; 75 | console.log(url); 76 | const file = await fetch(url); 77 | return [await file.arrayBuffer(), file_name, file_size]; 78 | } 79 | 80 | export default { 81 | async fetch(request, env, ctx) { 82 | if (request.method === "OPTIONS") { 83 | // Handle CORS preflight requests 84 | return await handleOptions(request); 85 | } else if ( 86 | request.method === "GET" || 87 | request.method === "HEAD" || 88 | request.method === "POST" 89 | ) { 90 | const url = new URL(request.url); 91 | const path = url.pathname; 92 | 93 | if (path.includes('/file/')) { 94 | const x = path.split('/file/')[1].split('/'); 95 | const CHANNEL_ID = x[0] 96 | const fileID = x[1] 97 | console.log(CHANNEL_ID, fileID) 98 | const y = await downloadFile(CHANNEL_ID, fileID); 99 | console.log(y) 100 | const data = y[0] 101 | const file_name = y[1] 102 | const file_size = y[2] 103 | 104 | return new Response(data, { 105 | status: 200, headers: { 106 | "Content-Disposition": `attachment; filename=${file_name}`, 107 | "Content-Length": file_size, 108 | ...corsHeaders 109 | } 110 | }); 111 | } 112 | return new Response('TG DL Api Working!\n\nUsage : /file/CHANNEL_ID/MESSAGE_ID', { status: 200, headers: corsHeaders }); 113 | } else { 114 | return new Response('Method Not Allowed', { status: 405 }); 115 | } 116 | } 117 | } --------------------------------------------------------------------------------