A Telegram bot to generate direct link for your Telegram files.
Note
Checkout python branch if you are interested in that.
Table of Contents
Important
You'll have to expand the "Environment variables and files" section and update the env variables before hitting the deploy button.
Note
This deploys the latest docker release and NOT the latest commit. Since it uses prebuilt docker container, the deploy speed will be significantly faster.
Note
You'll have to fork this repository to deploy to Heroku.
Press the below button to fast deploy to Heroku
Click Here to know how to add / edit environment variables in Heroku.
- Head over to releases tab, from the pre release section, download the one for your platform and architecture.
- Extract the zip file to a folder.
- Create an a file named
fsb.envand add all the variables there (seefsb.sample.envfile for reference). - Give the executable file permission to execute using the command
chmod +x fsb(Not required for windows). - Run the bot using
./fsb runcommand. (./fsb.exe runfor windows)
- Clone the repository
git clone https://github.com/EverythingSuckz/TG-FileStreamBot
cd TG-FileStreamBot- Create an a file named
fsb.envand add all the variables there (seefsb.sample.envfile for reference).
nano fsb.env- Build and run the docker-compose file
docker-compose up -dOR
docker compose up -ddocker run --env-file fsb.env ghcr.io/everythingsuckz/fsb:latestWhere fsb.env is the environment file containing all the variables.
Note
Make sure to install go 1.21 or above. Refer https://stackoverflow.com/a/17566846/15807350
git clone https://github.com/EverythingSuckz/TG-FileStreamBot
cd TG-FileStreamBot
go build ./cmd/fsb/
chmod +x fsb
mv fsb.sample.env fsb.env
nano fsb.env
# (add your environment variables, see the next section for more info)
./fsb runand to stop the program, do CTRL+C
Note
Make sure to install go 1.21 or above.
git clone https://github.com/EverythingSuckz/TG-FileStreamBot
cd TG-FileStreamBot
go build ./cmd/fsb/
Rename-Item -LiteralPath ".\fsb.sample.env" -NewName ".\fsb.env"
notepad fsb.env
# (add your environment variables, see the next section for more info)
.\fsb runand to stop the program, do CTRL+C
If you're locally hosting, create a file named fsb.env in the root directory and add all the variables there.
You may check the fsb.sample.env.
An example of fsb.env file:
API_ID=452525
API_HASH=esx576f8738x883f3sfzx83
BOT_TOKEN=55838383:yourbottokenhere
LOG_CHANNEL=-10045145224562
PORT=8080
HOST=http://yourserverip
# (if you want to set up multiple bots)
MULTI_TOKEN1=55838373:yourworkerbottokenhere
MULTI_TOKEN2=55838355:yourworkerbottokenhereBefore running the bot, set these mandatory variables:
API_ID: Telegram API ID from https://my.telegram.org.API_HASH: Telegram API hash from https://my.telegram.org.BOT_TOKEN: Bot token from @BotFather.LOG_CHANNEL: Telegram channel ID where bot messages/files are stored for streaming links.- How to get it: Create a channel, send a message, forward it to @missrose_bot, then reply with
/id.
- How to get it: Create a channel, send a message, forward it to @missrose_bot, then reply with
In addition to required variables, these optional ones are available:
-
DEV(default:false)- Enables development logging behavior.
-
PORT(default:8080)- HTTP port used by the stream server.
-
HOST(default: auto-generated)- Base URL used in generated links (for example
https://example.comorhttp://192.168.1.10:8080). - If omitted, bot auto-builds it from detected IP +
PORT.
- Base URL used in generated links (for example
-
HASH_LENGTH(default:6, min5, max32)- Length of URL hash used in stream links.
-
USE_SESSION_FILE(default:true)- Reuse saved worker sessions to speed startup and reduce login overhead.
-
USER_SESSION(default: empty)- Optional user session string for userbot features (for example auto-adding bots to
LOG_CHANNEL).
- Optional user session string for userbot features (for example auto-adding bots to
-
USE_PUBLIC_IP(default:false)- If enabled, bot tries to discover public IP for host generation.
-
ALLOWED_USERS(default: empty)- Comma-separated Telegram user IDs that are allowed to use the bot (access allowlist).
These optional variables allow you to tune the streaming performance. Most users won't need to change these defaults.
-
STREAM_CONCURRENCY(default:4)- How many block downloads a single stream request runs in parallel.
- Effective first-batch request fanout per stream request =
STREAM_CONCURRENCY. - Higher values can improve throughput and startup latency, but increase Telegram API pressure.
-
STREAM_BUFFER_COUNT(default:8)- Capacity of the in-memory block queue between downloader and HTTP writer.
- It controls how far ahead prefetch can run before the reader catches up.
- It does not increase parallel download count by itself.
- Approximate extra memory per request:
STREAM_BUFFER_COUNT * blockSize(blockSize is dynamic: 64KB/256KB/512KB/1MB).
-
STREAM_TIMEOUT_SEC(default:30)- Per-block timeout for Telegram
UploadGetFilerequest.
- Per-block timeout for Telegram
-
STREAM_MAX_RETRIES(default:3)- Retry attempts per block before failing the stream.
-
STREAM_CONCURRENCY= how many Telegram downloads happen at the same time for one stream.- Bigger value = video may start faster and download faster.
- But bigger value also means more chance of Telegram floodwait/rate-limit.
-
STREAM_BUFFER_COUNT= how many downloaded chunks are kept ready in memory.- Bigger value = smoother playback on unstable networks.
- It does not create more Telegram requests by itself.
-
STREAM_TIMEOUT_SEC= how long to wait before saying a chunk request is too slow. -
STREAM_MAX_RETRIES= how many times to retry a failed chunk.
Quick rule:
- Telegram request pressure is mainly controlled by
STREAM_CONCURRENCY. - Approximate in-flight Telegram calls at peak =
active_streams × STREAM_CONCURRENCY.
Safe starting presets:
-
Small server / home host:
STREAM_CONCURRENCY=4STREAM_BUFFER_COUNT=8STREAM_TIMEOUT_SEC=30STREAM_MAX_RETRIES=3
-
Medium VPS:
STREAM_CONCURRENCY=6STREAM_BUFFER_COUNT=12STREAM_TIMEOUT_SEC=45STREAM_MAX_RETRIES=3
-
High bandwidth + multiple worker bots:
STREAM_CONCURRENCY=8STREAM_BUFFER_COUNT=16STREAM_TIMEOUT_SEC=60STREAM_MAX_RETRIES=5
Note
Increasing STREAM_CONCURRENCY increases concurrent Telegram requests from one client connection. If you push this too high, floodwait/rate-limit risk increases.
Start with 4, test 6 or 8, and only go higher if logs show stable behavior.
Example configuration for high-performance servers:
STREAM_CONCURRENCY=8
STREAM_BUFFER_COUNT=16
STREAM_TIMEOUT_SEC=60
STREAM_MAX_RETRIES=5This configuration would allow up to 8 blocks to be downloaded in parallel, with a buffer of 16 blocks, and a longer timeout for slow connections.
You can add worker bots to distribute stream requests across different bot tokens:
MULTI_TOKEN1MULTI_TOKEN2MULTI_TOKEN3- ...
Each active HTTP stream request uses one worker client in round-robin mode. Using multiple workers reduces floodwait risk versus sending all traffic through a single token.
Note
What it multi-client feature and what it does?
This feature shares the Telegram API requests between worker bots to speed up download speed when many users are using the server and to avoid the flood limits that are set by Telegram.
Note
You can add up to 50 bots since 50 is the max amount of bot admins you can set in a Telegram Channel.
To enable multi-client, generate new bot tokens and add it as your fsb.env with the following key names.
MULTI_TOKEN1: Add your first bot token here.
MULTI_TOKEN2: Add your second bot token here.
you may also add as many as bots you want. (max limit is 50)
MULTI_TOKEN3, MULTI_TOKEN4, etc.
Warning
Don't forget to add all these worker bots to the LOG_CHANNEL for the proper functioning
Warning
This might sometimes result in your account getting resticted or banned. Only newly created accounts are prone to this.
To use this feature, you need to generate a pyrogram session string for the user account and add it to the USER_SESSION variable in the fsb.env file.
This feature is used to auto add the worker bots to the LOG_CHANNEL when they are started. This is useful when you have a lot of worker bots and you don't want to add them manually to the LOG_CHANNEL.
The easiest way to generate a session string is by running
./fsb session --api-id <your api id> --api-hash <your api hash>
This will generate a session string for your user account using QR code authentication. Authentication via phone number is not supported yet and will be added in the future.
Feel free to contribute to this project if you have any further ideas
You can contact either via my Telegram Group or you can message me on @EverythingSuckz
- @celestix for gotgproto
- @divyam234 for his Teldrive Project
- @karu for adding image support
Copyright (C) 2023 EverythingSuckz under GNU Affero General Public License.
TG-FileStreamBot is Free Software: 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 Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Also keep in mind that all the forks of this repository MUST BE OPEN-SOURCE and MUST BE UNDER THE SAME LICENSE.
