telegram-bot-api icon indicating copy to clipboard operation
telegram-bot-api copied to clipboard

Couldnt download file from server

Open EvgeniyKr1980 opened this issue 8 months ago • 2 comments

Hi! I have a problem. Deploying docker with the command: docker run -d -p 8081:8081 --name=telegram-bot-api --restart=always -v telegram-bot-api-data:/var/lib/telegram-bot-api -e TELEGRAM_API_ID=XXXXXXX -e TELEGRAM_API_HASH=XXXXXXXXXXX -e TELEGRAM_LOCAL=True aiogram/telegram-bot-api:latest

I start the bot, I start transferring a large file to the bot. When the bot.get_file(file_id) command is called, the file data is returned. file_info: file_id='BAACAgIAAxkBAAPKaCMKpB0eK6F5lMBDo0k9UuLaStEAAiNfAAKVBelI-s7ISjg6lVQ2BA' fille_unique_id='AgADI18AApUF6Ug' file_size=97548419 file_path='/var/lib/telegram-bot-api/7843XXXXXXXXXXXXXXXcc/videos/file_0.mp4'

And here is the bot.download_file call(.....) error is returned: ERROR:main:Common Error: [Errno 2] No such file or directory: '/var/lib/telegram-bot-api/7843xxxxxxxxxxxxxxcc/videos/file_0.mp4' the file itself is in docker.

I tried to change the paths - nothing helps. 
 What could be the problem? Running on Mac OS

EvgeniyKr1980 avatar May 13 '25 09:05 EvgeniyKr1980

I have the same issue.

ah-rahimi avatar May 28 '25 20:05 ah-rahimi

Hi! The issue you're experiencing is related to how the local mode of the Telegram Bot API server works. Let me explain what's happening and how to resolve it.

The Problem

When you enable local mode with -e TELEGRAM_LOCAL=True, the Bot API server returns absolute file paths on the server instead of HTTP URLs. In your case, the file_path you're getting is: /var/lib/telegram-bot-api/7843XXXXXXXXXXXXXXXcc/videos/file_0.mp4 This path exists inside the Docker container, not on your host machine (Mac OS). When your bot tries to access this path, it's looking for it on the host system where it doesn't exist.

Solution Options

Option 1: If your bot is running in another Docker container - Mount volume

Share the same volume between both containers:

Telegram Bot API container:

docker run -d -p 8081:8081 \
  --name=telegram-bot-api \
  --restart=always \
  -v telegram-bot-api-data:/var/lib/telegram-bot-api \
  -e TELEGRAM_API_ID=XXXXXXX \
  -e TELEGRAM_API_HASH=XXXXXXXXXXX \
  -e TELEGRAM_LOCAL=True \
  aiogram/telegram-bot-api:latest

Your bot container:

docker run -d \
  --name=my-bot \
  -v telegram-bot-api-data:/var/lib/telegram-bot-api \
  my-bot-image:latest

Note: Volume should be created fore containers start, or use docker-compose to manage it easilly.

Option 2: If your bot is running without Docker - Mount the data directory

Mount the Telegram Bot API data directory to your host system:

docker run -d -p 8081:8081 \
  --name=telegram-bot-api \
  --restart=always \
  -v ./bot-files:/var/lib/telegram-bot-api \
  -e TELEGRAM_API_ID=XXXXXXX \
  -e TELEGRAM_API_HASH=XXXXXXXXXXX \
  -e TELEGRAM_LOCAL=True \
  aiogram/telegram-bot-api:latest

Then access files using the local path: ./bot-files/7843XXXXXXXXXXXXXXXcc/videos/file_0.mp4

Note: Permissions to the directory shoul be adjusted to avoit read/write issues

Option 3: Configure nginx or other reverse proxy

Set up a reverse proxy that can serve files from the directory and use HTTP URLs. You can find a complete example of this solution in the repository: https://github.com/aiogram/telegram-bot-api/tree/master/example

Important Notes

  • Path mapping: If you mount the directory with a different path in your bot environment, you'll need to change the file path accordingly. For example, if the API server returns /var/lib/telegram-bot-api/... but you mounted it to /app/files/..., you'll need to replace the path prefix.
  • aiogram framework: If you're using the aiogram framework, it has a built-in solution for path overwriting that can handle this automatically.

Key Points about Local Mode

As mentioned in the official documentation, when using --local mode:

You receive absolute local paths as the file_path value No download is needed after a getFile request - the file is already stored locally Files can be up to 2000 MB (instead of the usual limits) Your bot needs to handle absolute file paths correctly

JrooTJunior avatar Jun 01 '25 13:06 JrooTJunior