Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Telegram: get fileid from telegram client

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.

like image 323
Behrouz.M Avatar asked Dec 26 '16 05:12

Behrouz.M


People also ask

How can I get bot from Telegram file?

From telegram's docs: On success, a File object is returned. The file can then be downloaded via the link https://api.telegram.org/file/bot/, where is taken from the response. It is guaranteed that the link will be valid for at least 1 hour.

Is there an API for Telegram?

We offer two kinds of APIs for developers. The Bot API allows you to easily create programs that use Telegram messages for an interface. The Telegram API and TDLib allow you to build your own customized Telegram clients.


1 Answers

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.

like image 64
Danogentili Avatar answered Oct 02 '22 17:10

Danogentili