InputFile¶
- class telegram.InputFile(obj, filename=None, attach=False, read_file_handle=True)[source]¶
Bases:
object
This object represents a Telegram InputFile.
Use In
Available In
Changed in version 20.0:
The former attribute
attach
was renamed toattach_name
.Method
is_image
was removed. If you passbytes
toobj
and would like to have the mime type automatically guessed, please passfilename
in addition.
- Parameters:
obj (file object |
bytes
|str
) –An open file descriptor or the files content as bytes or string.
Note
If
obj
is a string, it will be encoded as bytes viaobj.encode('utf-8')
.Changed in version 20.0: Accept string input.
attach (
bool
, optional) – PassTrue
if the parameter this file belongs to in the request to Telegram should point to the multipart data via anattach://
URI. Defaults to False.read_file_handle (
bool
, optional) –If
True
andobj
is a file handle, the data will be read from the file handle on initialization of this object. IfFalse
, the file handle will be passed on to thenetworking backend
which will have to handle the reading. Defaults toTrue
.Tip
If you upload extremely large files, you may want to set this to
False
to avoid reading the complete file into memory. Additionally, this may be supported better by the networking backend (in particular it is handled better by the defaultHTTPXRequest
).Important
If you set this to
False
, you have to ensure that the file handle is still open when the request is made. In particular, the following snippet can not work as expected.with open('file.txt', 'rb') as file: input_file = InputFile(file, read_file_handle=False) # here the file handle is already closed and the upload will fail await bot.send_document(chat_id, input_file)
Added in version 21.5.
- attach_name[source]¶
Optional. If present, the parameter this file belongs to in the request to Telegram should point to the multipart data via a an URI of the form
attach://<attach_name>
URI.- Type:
- property attach_uri[source]¶
URI to insert into the JSON data for uploading the file. Returns
None
, ifattach_name
isNone
.