telegram.File¶
-
class
telegram.File(file_id, bot=None, file_size=None, file_path=None, **kwargs)¶ Bases:
telegram.base.TelegramObjectThis object represents a file ready to be downloaded. The file can be downloaded with
download. It is guaranteed that the link will be valid for at least 1 hour. When the link expires, a new one can be requested by calling getFile.Note
Maximum file size to download is 20 MB
-
file_id¶ Unique identifier for this file.
Type: str
-
file_size¶ Optional. File size.
Type: str
Parameters: - file_id (
str) – Unique identifier for this file. - file_size (
int, optional) – Optional. File size, if known. - file_path (
str, optional) – File path. Usedownloadto get the file. - bot (
telegram.Bot, optional) – Bot to use with shortcut method. - **kwargs (
dict) – Arbitrary keyword arguments.
Note
If you obtain an instance of this class from
telegram.PassportFile.get_file, then it will automatically be decrypted as it downloads when you calldownload().-
download(custom_path=None, out=None, timeout=None)¶ Download this file. By default, the file is saved in the current working directory with its original filename as reported by Telegram. If a
custom_pathis supplied, it will be saved to that path instead. Ifoutis defined, the file contents will be saved to that object using theout.writemethod.Note
custom_pathandoutare mutually exclusive.Parameters: - custom_path (
str, optional) – Custom path. - out (
io.BufferedWriter, optional) – A file-like object. Must be opened for writing in binary mode, if applicable. - timeout (
int|float, optional) – If this value is specified, use it as the read timeout from the server (instead of the one specified during creation of the connection pool).
Returns: The same object as
outif specified. Otherwise, returns the filename downloaded to.Return type: str|io.BufferedWriterRaises: ValueError– If bothcustom_pathandoutare passed.- custom_path (
-
download_as_bytearray(buf=None)¶ Download this file and return it as a bytearray.
Parameters: buf ( bytearray, optional) – Extend the given bytearray with the downloaded data.Returns: The same object as bufif it was specified. Otherwise a newly allocatedbytearray.Return type: bytearray
-