-1

I've got a function to download an image from telegram message, it accepts message.media.photo:

const downloadPhoto = async (photo, client, name) => {
 await client.connect();
 const file = new Api.InputPhotoFileLocation({
 id: photo.id,
 accessHash: photo.accessHash,
 fileReference: photo.fileReference,
 thumbSize: "y",
 });
 try {
 const buffer = await client.downloadFile(file, {
 dcId: photo.dcId,
 });
 fs.writeFileSync(name ? name : "output.jpg", buffer);
 } catch (error) {
 console.error("Error downloading photo:", error, file);
 }
};

I get messages using this method:

await client.invoke(new Api.messages.GetHistory(req))

which works fine. The issue is with the download method: it fails with an error:

Error downloading photo: RPCError: 400: CONNECTION_NOT_INITED (caused by upload.GetFile)
 at RPCMessageToError (/app/img-bot/node_modules/telegram/errors/index.js:28:12)
 at MTProtoSender._handleRPCResult (/app/img-bot/node_modules/telegram/network/MTProtoSender.js:561:58)
 at MTProtoSender._processMessage (/app/img-bot/node_modules/telegram/network/MTProtoSender.js:492:15)
 at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
 at async MTProtoSender._recvLoop (/app/img-bot/node_modules/telegram/network/MTProtoSender.js:439:17)

What am I doing wrong?

asked Nov 21, 2025 at 7:37

1 Answer 1

2

Telegram stores media on different DCs.
By forcing dcId and also calling client.connect() inside the download function, you end up with a DC connection that is not properly initialized for file download CONNECTION_NOT_INITED.

  1. What to change
Connect only once at startup, not inside downloadPhoto:
// app startup
const client = new TelegramClient(session, apiId, apiHash, { connectionRetries: 5 });
await client.start({ /* auth options */ });
// no client.connect() inside downloadPhoto
  1. Do not pass dcId to downloadFile let GramJS handle it
const downloadPhoto = async (photo, name = "output.jpg") => {
 const file = new Api.InputPhotoFileLocation({
 id: photo.id,
 accessHash: photo.accessHash,
 fileReference: photo.fileReference,
 thumbSize: "y",
 });
 try {
 const buffer = await client.downloadFile(file, {});
 fs.writeFileSync(name, buffer);
 } catch (error) {
 console.error("Error downloading photo:", error);
 }
};
answered Nov 21, 2025 at 9:12
Sign up to request clarification or add additional context in comments.

3 Comments

sadly got the same issue, will check further
yeah, the issue is somehow connected to the DC: got this now "The file to be accessed is currently stored in DC 2 (caused by upload.GetFile)". The issue is the same though
oh! got it! I have explicitly set storeSession.setDC(2, '149.154.167.41', 443); and it worked! thank you

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.