MCPcopy Index your code
hub / github.com/python-telegram-bot/python-telegram-bot / InputFile

Class InputFile

src/telegram/_files/inputfile.py:32–142  ·  view source on GitHub ↗

This object represents a Telegram InputFile. .. versionchanged:: 20.0 * The former attribute ``attach`` was renamed to :attr:`attach_name`. * Method ``is_image`` was removed. If you pass :obj:`bytes` to :paramref:`obj` and would like to have the mime type automaticall

Source from the content-addressed store, hash-verified

30
31
32class InputFile:
33 """This object represents a Telegram InputFile.
34
35 .. versionchanged:: 20.0
36
37 * The former attribute ``attach`` was renamed to :attr:`attach_name`.
38 * Method ``is_image`` was removed. If you pass :obj:`bytes` to :paramref:`obj` and would
39 like to have the mime type automatically guessed, please pass :paramref:`filename`
40 in addition.
41
42 Args:
43 obj (:term:`file object` | :obj:`bytes` | :obj:`str`): An open file descriptor or the files
44 content as bytes or string.
45
46 Note:
47 If :paramref:`obj` is a string, it will be encoded as bytes via
48 :external:obj:`obj.encode('utf-8') <str.encode>`.
49
50 .. versionchanged:: 20.0
51 Accept string input.
52 filename (:obj:`str`, optional): Filename for this InputFile.
53 attach (:obj:`bool`, optional): Pass :obj:`True` if the parameter this file belongs to in
54 the request to Telegram should point to the multipart data via an ``attach://`` URI.
55 Defaults to `False`.
56 read_file_handle (:obj:`bool`, optional): If :obj:`True` and :paramref:`obj` is a file
57 handle, the data will be read from the file handle on initialization of this object.
58 If :obj:`False`, the file handle will be passed on to the
59 :attr:`networking backend <telegram.request.BaseRequest.do_request>` which will have
60 to handle the reading. Defaults to :obj:`True`.
61
62 Tip:
63 If you upload extremely large files, you may want to set this to :obj:`False` to
64 avoid reading the complete file into memory. Additionally, this may be supported
65 better by the networking backend (in particular it is handled better by
66 the default :class:`~telegram.request.HTTPXRequest`).
67
68 Important:
69 If you set this to :obj:`False`, you have to ensure that the file handle is still
70 open when the request is made. In particular, the following snippet can *not* work
71 as expected.
72
73 .. code-block:: python
74
75 with open('file.txt', 'rb') as file:
76 input_file = InputFile(file, read_file_handle=False)
77
78 # here the file handle is already closed and the upload will fail
79 await bot.send_document(chat_id, input_file)
80
81 .. versionadded:: 21.5
82
83
84 Attributes:
85 input_file_content (:obj:`bytes` | :class:`IO`): The binary content of the file to send.
86 attach_name (:obj:`str`): Optional. If present, the parameter this file belongs to in
87 the request to Telegram should point to the multipart data via a an URI of the form
88 ``attach://<attach_name>`` URI.
89 filename (:obj:`str`): Filename for the file to be sent.

Calls

no outgoing calls

Used in the wild real call sites across dependent graphs

searching dependent graphs…