Python telegram bot get file

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 calling telegram.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.

  • Maximum file size to download is 20 MB .
  • If you obtain an instance of this class from telegram.PassportFile.get_file , then it will automatically be decrypted as it downloads when you call download() .
  • 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. Use download to get the file.
  • bot ( telegram.Bot , optional) – Bot to use with shortcut method.
  • **kwargs ( dict ) – Arbitrary keyword arguments.

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.

Optional. File size in bytes.

Optional. File path. Use download() to get the file.

async download ( custom_path = None , out = None , read_timeout = None , write_timeout = None , connect_timeout = None , pool_timeout = None ) [source] #

Читайте также:  Php mysqli with port

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. If out is defined, the file contents will be saved to that object using the out.write method.

  • custom_path and out are mutually exclusive.
  • If neither custom_path nor out is provided and file_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.
  • custom_path parameter now also accepts pathlib.Path as argument.
  • Returns pathlib.Path object in cases where previously a str was returned.
  • 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 to telegram.request.BaseRequest.post.read_timeout . Defaults to DEFAULT_NONE .
  • write_timeout ( float | None , optional) – Value to pass to telegram.request.BaseRequest.post.write_timeout . Defaults to DEFAULT_NONE .
  • connect_timeout ( float | None , optional) – Value to pass to telegram.request.BaseRequest.post.connect_timeout . Defaults to DEFAULT_NONE .
  • pool_timeout ( float | None , optional) – Value to pass to telegram.request.BaseRequest.post.pool_timeout . Defaults to DEFAULT_NONE .

specified. Otherwise, returns the filename downloaded to or the file path of the local file.

async download_as_bytearray ( buf = None ) [source] #

Download this file and return it as a bytearray.

buf ( bytearray , optional) – Extend the given bytearray with the downloaded data.

The same object as buf if it was specified. Otherwise a

Sets the passport credentials for the file.

Источник

File¶

class telegram. File ( file_id , file_unique_id , file_size = None , file_path = None , * , api_kwargs = None ) [source] ¶ Bases: telegram.TelegramObject This object represents a file ready to be downloaded. The file can be e.g. downloaded with download_to_drive . 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 telegram.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.

Changed in version 20.0: download was split into download_to_drive() and download_to_memory() .

  • Maximum file size to download is 20 MB .
  • If you obtain an instance of this class from telegram.PassportFile.get_file , then it will automatically be decrypted as it downloads when you call e.g. download_to_drive() .
  • 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) – File size in bytes, if known.
  • file_path ( str , optional) – File path. Use e.g. download_to_drive() to get the file.

Identifier for this file, which can be used to download or reuse the file.

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.

Optional. File size in bytes, if known.

Optional. File path. Use e.g. download_to_drive() to get the file.

async download_as_bytearray ( buf = None , * , read_timeout = None , write_timeout = None , connect_timeout = None , pool_timeout = None ) [source] ¶

Download this file and return it as a bytearray.

buf ( bytearray , optional) – Extend the given bytearray with the downloaded data.

The same object as buf if it was specified. Otherwise a newly allocated bytearray .

async download_to_drive ( custom_path = 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 file_path as file name. If the file has no filename, the file ID will be used as filename. If custom_path is supplied as a str or pathlib.Path , it will be saved to that path.

If custom_path isn’t provided and file_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.

The only exception to this are encrypted files (e.g. a passport file). For these, a file with the prefix decrypted_ will be created in the same directory as the original file in order to decrypt the file without changing the existing one in-place.

  • custom_path parameter now also accepts pathlib.Path as argument.
  • Returns pathlib.Path object in cases where previously a str was returned.
  • This method was previously called download . It was split into download_to_drive() and download_to_memory() .

custom_path ( pathlib.Path | str , optional) – The path where the file will be saved to. If not specified, will be saved in the current working directory with file_path as file name or the file_id if file_path is not set.

  • read_timeout ( float | None , optional) – Value to pass to telegram.request.BaseRequest.post.read_timeout . Defaults to DEFAULT_NONE .
  • write_timeout ( float | None , optional) – Value to pass to telegram.request.BaseRequest.post.write_timeout . Defaults to DEFAULT_NONE .
  • connect_timeout ( float | None , optional) – Value to pass to telegram.request.BaseRequest.post.connect_timeout . Defaults to DEFAULT_NONE .
  • pool_timeout ( float | None , optional) – Value to pass to telegram.request.BaseRequest.post.pool_timeout . Defaults to DEFAULT_NONE .

Returns the Path object the file was downloaded to.

async download_to_memory ( out , * , read_timeout = None , write_timeout = None , connect_timeout = None , pool_timeout = None ) [source] ¶

Download this file into memory. out needs to be supplied with a io.BufferedIOBase , the file contents will be saved to that object using the out.write method.

out ( io.BufferedIOBase ) – A file-like object. Must be opened for writing in binary mode.

  • read_timeout ( float | None , optional) – Value to pass to telegram.request.BaseRequest.post.read_timeout . Defaults to DEFAULT_NONE .
  • write_timeout ( float | None , optional) – Value to pass to telegram.request.BaseRequest.post.write_timeout . Defaults to DEFAULT_NONE .
  • connect_timeout ( float | None , optional) – Value to pass to telegram.request.BaseRequest.post.connect_timeout . Defaults to DEFAULT_NONE .
  • pool_timeout ( float | None , optional) – Value to pass to telegram.request.BaseRequest.post.pool_timeout . Defaults to DEFAULT_NONE .

Sets the passport credentials for the file.

Источник

Document¶

class telegram. Document ( file_id , file_unique_id , thumb = None , file_name = None , mime_type = None , file_size = None , thumbnail = None , * , api_kwargs = None ) [source] ¶ Bases: telegram.TelegramObject This object represents a general file (as opposed to photos, voice messages and audio files). 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.

  • 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.
  • thumb ( telegram.PhotoSize , optional) – Document thumbnail as defined by sender.

Identifier for this file, which can be used to download or reuse the file.

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.

Optional. Original filename as defined by sender.

Optional. MIME type of the file as defined by sender.

Optional. File size in bytes.

Optional. Document thumbnail as defined by sender.

async get_file ( * , read_timeout = None , write_timeout = None , connect_timeout = None , pool_timeout = None , api_kwargs = None ) [source] ¶

For the documentation of the arguments, please see telegram.Bot.get_file() .

Optional. Thumbnail as defined by sender.

Deprecated since version 20.2: As of Bot API 6.6 this attribute is deprecated in favor of thumbnail .

Источник

telegram.File¶

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 calling telegram.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.

  • Maximum file size to download is 20 MB.
  • If you obtain an instance of this class from telegram.PassportFile.get_file , then it will automatically be decrypted as it downloads when you call download() .
  • 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, if known.
  • file_path ( str , optional) – File path. Use download to get the file.
  • bot ( telegram.Bot , optional) – Bot to use with shortcut method.
  • **kwargs ( dict ) – Arbitrary keyword arguments.

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.

Optional. File path. Use download to get the file.

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 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. If out is defined, the file contents will be saved to that object using the out.write method.

  • custom_path and out are mutually exclusive.
  • If neither custom_path nor out is provided and file_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.
  • 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).

The same object as out if specified. Otherwise, returns the filename downloaded to or the file path of the local file.

ValueError – If both custom_path and out are passed.

download_as_bytearray ( buf = None ) ¶

Download this file and return it as a bytearray.

buf ( bytearray , optional) – Extend the given bytearray with the downloaded data.

The same object as buf if it was specified. Otherwise a newly allocated bytearray .

Sets the passport credentials for the file.

credentials ( telegram.FileCredentials ) – The credentials.

© Copyright 2015-2021, Leandro Toledo. Revision e982a5a5 .

Источник

Оцените статью