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

Every time I want to get my files, I get 404 file not found

Open alexander-lamdan opened this issue 6 months ago • 12 comments

Title:
Problem downloading files via /file/bot... endpoint when using telegram-bot-api with --local mode

Description:
When running telegram-bot-api with the --local option, we are experiencing issues downloading files via the /file/bot<token>/<file_path> endpoint. The API does not reliably serve files, even though the files are supposed to be saved and accessible locally in this mode.

Even if the ctx.message shows this:

Image

Steps to Reproduce:

  1. Start telegram-bot-api with the --local flag enabled.
  2. Use the getFile method to obtain a valid file_path for a file (e.g., after sending or receiving a document or photo).
  3. Attempt to download the file using:
    http://<your_host>:<port>/file/bot<token>/<file_path>
  4. The request fails in one of the following ways:
    • 404 Not Found
    • Unexpected error (e.g. 400, 429, or 5xx)
    • Download hangs or is incomplete

Expected Behavior:

  • The file should be served directly and reliably from the local storage by the API, as long as it is not deleted and the file_path is valid.

Actual Behavior:

  • The file is not served as expected.
  • In some cases, a 404 or other HTTP error is returned, even though getFile reports the file as available.
  • In other cases, the download does not start or does not complete.
  • The problem occurs for different file types and sizes.

Additional Information:

  • This issue occurs with the latest version of telegram-bot-api 9.0
  • The bot has all necessary permissions.
  • We have verified that the file is within the allowed size limits.
  • We have reviewed the relevant parts of the source code (Client.cpp, on_update_file, etc.) but could not identify a workaround.
  • The problem appears unique to --local mode

Environment:

  • OS: Ubuntu 24.04
  • telegram-bot-api version: 9.0
  • How you deploy: Native on Ubuntu server 24.04

Logs & Error Messages:
4][t 6][1748117244.843397140][HttpConnectionBase.cpp:102][!HttpInboundConnection] Can read from the connection [ 4][t 6][1748117244.843448162][BufferedFd.h:206][!HttpInboundConnection] Flush read: +255B[total:255B] [ 4][t 6][1748117244.843462944][HttpReader.cpp:567][!HttpInboundConnection] Process header [host=>lmdntech.co.il] [ 4][t 6][1748117244.843465089][HttpReader.cpp:567][!HttpInboundConnection] Process header [x-real-ip=>77.127.107.166] [ 4][t 6][1748117244.843466043][HttpReader.cpp:567][!HttpInboundConnection] Process header [x-forwarded-for=>77.127.107.166] [ 4][t 6][1748117244.843469142][HttpReader.cpp:567][!HttpInboundConnection] Process header [user-agent=>curl/8.5.0] [ 4][t 6][1748117244.843471288][HttpReader.cpp:567][!HttpInboundConnection] Process header [accept=>/] [ 4][t 6][1748117244.843473911][HttpConnectionBase.cpp:144][!HttpInboundConnection] Send query to handler [ 4][t 6][1748117244.843477010][HttpConnection.cpp:27][!HttpConnection] Handle GET:/file/bot7343651493:AAE3swTi7DZh2GSIvfZWK5b_OAUERbA4spo/7343651493:AAE3swTi7DZh2GSIvfZWK5b_OAUERbA4spo/documents/file_1:keep-alive host=lmdntech.co.il x-real-ip=77.127.107.166 x-forwarded-for=77.127.107.166 user-agent=curl/8.5.0 accept=/ BEGIN CONTENT END CONTENT [ 4][t 6][1748117244.843507051][HttpConnection.cpp:77][!HttpConnection] Response headers: HTTP/1.1 404 Not Found Connection: keep-alive Content-Type: application/json Content-Length: 55

[ 4][t 6][1748117244.843509197][HttpConnection.cpp:83][!HttpConnection] Send result: {"ok":false,"error_code":404,"description":"Not Found"} [ 4][t 6][1748117244.843514919][HttpConnectionBase.cpp:156][!HttpInboundConnection] Can write to the connection [ 4][t 6][1748117244.843516111][BufferedFd.h:140][!HttpInboundConnection] Nothing to write to [fd:39] [ 4][t 6][1748117244.843523263][HttpConnectionBase.cpp:156][!HttpInboundConnection] Can write to the connection [ 4][t 6][1748117244.843615055][BufferedFd.h:215][!HttpInboundConnection] Flush write: +157B[left:0B] [ 4][t 6][1748117244.843916177][HttpConnectionBase.cpp:102][!HttpInboundConnection] Can read from the connection [ 4][t 6][1748117244.843935012][HttpConnectionBase.cpp:186][!HttpInboundConnection] Can close the connection

Sample Code to Reproduce:

import requests

file_url = "http://<your_host>:<port>/file/bot<token>/<file_path>"
r = requests.get(file_url)
print(r.status_code)
print(r.content)

Using: Node.js 22.16 TypeScript 5.8 Grammy 1.36 Express 5.1 Telegram Bot API 9.0

I tried in both servers, and none of them is worked. One server is from Israel physically, and second server is from Europe, germany

alexander-lamdan avatar May 24 '25 20:05 alexander-lamdan

See https://github.com/tdlib/telegram-bot-api/issues/26.

levlam avatar May 24 '25 21:05 levlam

See https://github.com/tdlib/telegram-bot-api/issues/26.

Hey, but I running telegram-bot-api as --local only without the need to connect to api.telegram.org beacuse of the 50 mb limits

ghost avatar May 25 '25 03:05 ghost

There is no difference. Files are saved locally and you can access them directly from the file system.

levlam avatar May 25 '25 07:05 levlam

There is no difference. Files are saved locally and you can access them directly from the file system.

But I showed you, that I can't access them because of 404 error

ghost avatar May 25 '25 13:05 ghost

The files are available locally on the local file system. You don't need to download them again over HTTP. getFile method provides you full path to the file if the Bot API server is used in local mode.

levlam avatar May 25 '25 21:05 levlam

I was in the same situation and with the help of GPT I was able to deal with this situation, using Nginx, because I needed to obtain it in the future via URL and not absolute path. However, I had some other difficulties in the permission to obtain the files. But I managed to solve it, if you want help I can send you how I solved it.

eujacksonsales avatar May 30 '25 17:05 eujacksonsales

I was in the same situation and with the help of GPT I was able to deal with this situation, using Nginx, because I needed to obtain it in the future via URL and not absolute path. However, I had some other difficulties in the permission to obtain the files. But I managed to solve it, if you want help I can send you how I solved it.

Yeah, can you help me out with this please?

ghost avatar Jun 02 '25 10:06 ghost

The files are available locally on the local file system. You don't need to download them again over HTTP. getFile method provides you full path to the file if the Bot API server is used in local mode.

Sorry for late answer.

So you mean if I upload a file, it saves locally on the folder that I set him in ./telegram-bot-api --dir-path-write=/var/lib/tg_bot_api/bot_token/folder-name/file?

And I only need to get that temporary file as I work with files directly in the web server, for example: mv file-name.jpg ../../../www/html/bot-dir/upload

I tried every steps with GPT, I gave even 777 in permissions, I tried to give even root:root, and nothing works, the file is just vanished after 1-2 minutes.

None of them is not worked, not working directly with the file and even with getFile, they both showed 404 error.

I tried even ls -l /var/lib/tg_bot_api/bot-token/folder-name/ and they not even exist.

I was thinking about bad IP reputation, or my VPS too close to Telegram DC

ghost avatar Jun 02 '25 10:06 ghost

The Bot API server has no such option as --dir-path-write. You need to read ./telegram-bot-api --help to see the list of available options.

Files that were downloaded with getFile are available locally for at least 1 hour after download.

levlam avatar Jun 02 '25 11:06 levlam

Yeah, can you help me out with this please?

Okay, I made a document where, together with GPT, it explains how I did it to make it easier for me in new VMs. I'll send it to you. If you have any questions, just talk to me. If you want, you can send me your email and I'll send it there too.

eujacksonsales avatar Jun 02 '25 16:06 eujacksonsales

Yeah, can you help me out with this please?

Okay, I made a document where, together with GPT, it explains how I did it to make it easier for me in new VMs. I'll send it to you. If you have any questions, just talk to me. If you want, you can send me your email and I'll send it there too.

Can you send it to me please [email protected]

ghost avatar Jun 03 '25 13:06 ghost

Yeah, can you help me out with this please?

Okay, I made a document where, together with GPT, it explains how I did it to make it easier for me in new VMs. I'll send it to you. If you have any questions, just talk to me. If you want, you can send me your email and I'll send it there too.

Can you send it to me please [email protected]

Sorry for late reply Alexander, but I already sent you the email. If you had any doubt, talk to me :)

eujacksonsales avatar Jun 18 '25 00:06 eujacksonsales

Hey @levlam, I've encountered the same issue. I spent an entire day debugging my code and eventually found this thread. In my case, I need to run the Bot API server on one machine and the bot itself on another, so accessing files locally from the bot’s code isn’t an option. Also, since my code already works perfectly with the cloud-based API, I expected to be able to use the exact same logic with a self-hosted server. Would it be possible to fix this or extend the Bot API to better support this kind of distributed setup?

danila-mamontov avatar Jul 26 '25 00:07 danila-mamontov

@danila-mamontov If you need to run the bot on a different server, then it is unsecure to get files (as well as send regular requests to the server) over HTTP. You will need a TLS-terminating web server to make requests to the Bot API server and can use the web server to get local files in a secure way.

levlam avatar Jul 26 '25 17:07 levlam