Telegram: get fileid from telegram client

1.8k Views Asked by At

Telegram bots use fileid to download a file. How can I get this fileid from Telegram client (My Unofficial version) ? Files in mtproto have this location class that indicates their address.

File Location Parameters

  • dc_id int Number of the data center holding the file
  • volume_id long Server volume
  • local_id int File ID
  • secret long Checksum to access the file

The problem is I don't know how to generate the fileid that is appropriate for bots from File Location class.

UPDATE 1:

I've tried to do some reverse engineering. So, I've found out that there is some relation between File Location class and FileId.

It is my File Location sample address.

  • id:0 // This field is only available for secret chats
  • access_hash:0 // This field is only available for secret chats
  • volume_id: 429640340 (199BCA94 HEX)
  • secret: -3528741004939935589 (CF0764C08833409B HEX) // according to TG documentations it should be file CheckSum
  • local_id: 6005 (1775 HEX)

And this is my according FileID to the former FileLocation class that I retrieved it from my Bot:

  • FileId: AgADBAADL6gxG06L8w0nhNO87UW3iZTKmxkABJtAM4jAZAfPdRcAAgI

It is obvious that FileID value is in Base64 format. So I've decoded it:

  • HEX Value: 0200030400032FA8311B4E8BF30D2784D3BCED45B78994CA9B1900049B403388C06407CF7517000202

Now we can see the FileLocation values in this hex value:

  • Unknown: 0200030400032FA8311B4E8BF30D2784D3BCED45B789
  • VolumeId:94CA9B19 HEX
  • Unknown:0004
  • Secret: 9B403388C06407CF HEX
  • local_id:7517 Hex
  • Unknown:000202

NOTE: the values are stored in Big-Endian format.

Now we need to decode Unknown values. I appreciate any effort that helps to decode these values.

1

There are 1 best solutions below

1
On

Here's the TL scheme for bot API file ids: https://github.com/danog/MadelineProto/blob/master/src/danog/MadelineProto/TL_botAPI.tl

I have implemented bot API file id conversion in MadelineProto: you basically have to base64decode the file ids, pass them through an RLE decoder based on null byte, and then decode them using the given TL scheme. See TL/Conversion/BotAPI* to see how MessageMedia objects are converted to a File objects before serialization.

I managed to reverse engineer file ids (I forgot to search here xd) using this special MTProto bot I built using the same lib.