telegram.File¶
- class telegram.File(*args, **kwargs)[source]¶
Bases:
telegram.TelegramObject
This 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 callingtelegram.Bot.get_file()
.Objects of this class are comparable in terms of equality. Two objects of this class are considered equal, if their
file_unique_id
is equal.Note
- Maximum file size to download is
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()
.
- Parameters
file_id (
str
) – Identifier for this file, which can be used to download or reuse the file.file_unique_id (
str
) – Unique identifier for this file, which is supposed to be the same over time and for different bots. Can’t be used to download or reuse the file.file_size (
int
, optional) – Optional. File size in bytes, if known.file_path (
str
, optional) – File path. Usedownload
to get the file.bot (
telegram.Bot
, optional) – Bot to use with shortcut method.
- file_unique_id[source]¶
Unique identifier for this file, which is supposed to be the same over time and for different bots. Can’t be used to download or reuse the file.
- Type
- file_path[source]¶
Optional. File path. Use
download()
to get the file.- Type
- async download(custom_path=None, out=None, read_timeout=None, write_timeout=None, connect_timeout=None, pool_timeout=None)[source]¶
Download this file. By default, the file is saved in the current working directory with its original filename as reported by Telegram. If the file has no filename, it the file ID will be used as filename. If a
custom_path
is supplied, it will be saved to that path instead. Ifout
is defined, the file contents will be saved to that object using theout.write
method.Note
custom_path
andout
are mutually exclusive.If neither
custom_path
norout
is provided andfile_path
is the path of a local file (which is the case when a Bot API Server is running in local mode), this method will just return the path.
Changed in version 20.0:
custom_path
parameter now also acceptspathlib.Path
as argument.Returns
pathlib.Path
object in cases where previously astr
was returned.
- Parameters
custom_path (
pathlib.Path
|str
, optional) – Custom path.out (
io.BufferedWriter
, optional) – A file-like object. Must be opened for writing in binary mode, if applicable.read_timeout (
float
|None
, optional) – Value to pass totelegram.request.BaseRequest.post.read_timeout
. Defaults toDEFAULT_NONE
.write_timeout (
float
|None
, optional) – Value to pass totelegram.request.BaseRequest.post.write_timeout
. Defaults toDEFAULT_NONE
.connect_timeout (
float
|None
, optional) – Value to pass totelegram.request.BaseRequest.post.connect_timeout
. Defaults toDEFAULT_NONE
.pool_timeout (
float
|None
, optional) – Value to pass totelegram.request.BaseRequest.post.pool_timeout
. Defaults toDEFAULT_NONE
.
- Returns
- The same object as
out
if specified. Otherwise, returns the filename downloaded to or the file path of the local file.
- The same object as
- Return type
- Raises
ValueError – If both
custom_path
andout
are passed.
- set_credentials(credentials)[source]¶
Sets the passport credentials for the file.
- Parameters
credentials (
telegram.FileCredentials
) – The credentials.