Post

DirectlinkRobot: Generate Direct Download Links from Telegram

DirectlinkRobot: Generate Direct Download Links from Telegram

๐Ÿ”— Directlink Robot

Directlink Robot is a Telegram bot designed to generate direct download links for files, videos, and audios sent to it. The bot uses Cloudflare Workers for serverless deployment, ensuring fast and reliable performance.

๐Ÿš€ Demo in Telegram: @directlink_robot

โœจ Features

  • ๐Ÿ’ฌ Responds to the /start command with a welcome message.
  • ๐Ÿ‘ฅ Supported chat types: private, groups, supergroups.
  • ๐Ÿ“ Handles file, video, and audio messages to generate direct download links.
  • ๐Ÿ•’ Auto rename unnamed videos with a timestamp.
  • ๐Ÿ“ Limits file sizes to a maximum of 20 MiB due to Telegram Bot API constraints.
  • ๐Ÿ” Utilizes encryption for secure message handling.
  • ๐ŸŒ Supports CORS for cross-origin requests.

โš™๏ธ Setup

  1. ๐Ÿงฉ Clone the Repository

    1
    2
    
    git clone https://gitlab.com/fr0stb1rd/directlink-robot.git
    cd directlink-robot
    
  2. ๐Ÿ”‘ Configure Bot Tokens

    Add your bot tokens in the BOT_TOKENS array in the script. You can add multiple tokens separated by a comma.

    1
    
    const BOT_TOKENS = ["your_bot_token"];
    
  3. ๐Ÿ†” Set the Channel ID

    Set the CHANNEL_ID to the ID of your Telegram channel where the bot will forward messages.

    1
    
    const CHANNEL_ID = -1000000000;
    
  4. โ˜๏ธ Deploy to Cloudflare Workers

    • ๐Ÿ› ๏ธ Install the Wrangler CLI.

      1
      
      npm install -g @cloudflare/wrangler
      
    • ๐Ÿ” Login to your Cloudflare account.

      1
      
      wrangler login
      
    • ๐Ÿš€ Publish your worker.

      1
      
      wrangler publish
      
  5. ๐ŸŒ Set Webhook URL

    Set the webhook URL for your bot using the following format:

    1
    
    const webhook_url = `https://api.telegram.org/bot<your_bot_token>/setWebhook?url=https://<your_worker_url>/webhook`;
    

    ๐Ÿ”„ Replace <your_bot_token> with your bot token and <your_worker_url> with your Cloudflare Worker URL.

โšก๏ธ How It Works

  1. โ–ถ๏ธ Start Command

    When a user sends the /start command, the bot responds with a welcome message and instructions.

  2. ๐Ÿ“ฆ File Handling

    When a user sends a file, video, or audio, the bot:

    • Forwards the message to the specified channel.
    • Generates a direct download link.
    • Sends the download link to the user.
  3. ๐Ÿ”— Download Links

    The download links are generated using a cipher function to ensure security. ๐Ÿ”’

๐Ÿ› ๏ธ Functions

  • ๐Ÿ” cipher(textV)
    • Encrypts the given text using a predefined salt.
  • ๐Ÿ”“ decipher(textV)
    • Decrypts the given text using the same salt.
  • ๐Ÿ“จ handleOptions(request)
    • Handles CORS preflight requests.
  • ๐Ÿค– getBotToken()
    • Retrieves a random bot token from the list of tokens.
  • ๐ŸŽฒ generateRandomString(length)
    • Generates a random string of the specified length.
  • ๐ŸŒ fetchJson(url)
    • Fetches JSON data from the specified URL.
  • ๐Ÿ“ฅ downloadFile(message_id)
    • Downloads the file from Telegram using the message ID.
  • ๐Ÿ”„ handleUpdate(update)
    • Handles incoming updates from Telegram.

๐ŸŒ CORS Headers

The bot includes CORS headers to support cross-origin requests:

1
2
3
4
5
const corsHeaders = {
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Methods": "GET, HEAD, POST, OPTIONS",
    "Access-Control-Allow-Headers": "Content-Type",
};

๐Ÿ“ธ Screenshots

Image 1 Image 2
1 2

๐Ÿ’ป Source Code

Soonโ€ฆ

๐Ÿ“„ License

You can use, study, share and improve it at your will. Specifically you can redistribute and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This post is licensed under CC BY 4.0 by the author.