Skip to content

Commit

Permalink
inputfile fix #2320
Browse files Browse the repository at this point in the history
  • Loading branch information
coder2020official committed Jul 3, 2024
1 parent f558724 commit 040e236
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 3 deletions.
4 changes: 4 additions & 0 deletions telebot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2598,6 +2598,10 @@ def send_document(
logger.warning('The parameter "thumb" is deprecated. Use "thumbnail" instead.')
thumbnail = thumb

if isinstance(document, types.InputFile) and visible_file_name:
# inputfile name ignored, warn
logger.warning('Cannot use both InputFile and visible_file_name. InputFile name will be ignored.')

return types.Message.de_json(
apihelper.send_data(
self.token, chat_id, document, 'document',
Expand Down
2 changes: 1 addition & 1 deletion telebot/apihelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def _make_request(token, method_name, method='get', params=None, files=None):
# process types.InputFile
for key, value in files_copy.items():
if isinstance(value, types.InputFile):
files[key] = value.file
files[key] = (value.file_name, value.file)
elif isinstance(value, tuple) and (len(value) == 2) and isinstance(value[1], types.InputFile):
files[key] = (value[0], value[1].file)

Expand Down
4 changes: 4 additions & 0 deletions telebot/async_telebot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4023,6 +4023,10 @@ async def send_document(
if reply_parameters and (reply_parameters.allow_sending_without_reply is None):
reply_parameters.allow_sending_without_reply = self.allow_sending_without_reply

if isinstance(document, types.InputFile) and visible_file_name:
# inputfile name ignored, warn
logger.warning('Cannot use both InputFile and visible_file_name. InputFile name will be ignored.')

return types.Message.de_json(
await asyncio_helper.send_data(
self.token, chat_id, document, 'document',
Expand Down
2 changes: 2 additions & 0 deletions telebot/asyncio_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ def _prepare_data(params=None, files=None):
if isinstance(f, tuple):
if len(f) == 2:
file_name, file = f
if isinstance(file, types.InputFile):
file = file.file
else:
raise ValueError('Tuple must have exactly 2 elements: filename, fileobj')
elif isinstance(f, types.InputFile):
Expand Down
14 changes: 12 additions & 2 deletions telebot/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -7735,8 +7735,11 @@ class InputFile:
InputFile(pathlib.Path('/path/to/file/file.txt'))
)
"""
def __init__(self, file) -> None:
self._file, self.file_name = self._resolve_file(file)
def __init__(self, file: Union[str, IOBase, Path], file_name: Optional[str] = None):
self._file, self._file_name = self._resolve_file(file)
if file_name:
self._file_name = file_name


@staticmethod
def _resolve_file(file):
Expand All @@ -7757,6 +7760,13 @@ def file(self):
File object.
"""
return self._file

@property
def file_name(self):
"""
File name.
"""
return self._file_name


class ForumTopicCreated(JsonDeserializable):
Expand Down

0 comments on commit 040e236

Please sign in to comment.