Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions telegram/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,7 @@ def send_document(
parse_mode: str = None,
thumb: FileLike = None,
api_kwargs: JSONDict = None,
disable_content_type_detection: bool = None,
) -> Optional[Message]:
"""
Use this method to send general files.
Expand All @@ -752,6 +753,8 @@ def send_document(
when you send file generated by temp module, for example). Undocumented.
caption (:obj:`str`, optional): Document caption (may also be used when resending
documents by file_id), 0-1024 characters after entities parsing.
disable_content_type_detection (:obj:`bool`, optional): Disables automatic server-side
content type detection for files uploaded using multipart/form-data.
parse_mode (:obj:`str`, optional): Send Markdown or HTML, if you want Telegram apps to
show bold, italic, fixed-width text or inline URLs in the media caption. See the
constants in :class:`telegram.ParseMode` for the available modes.
Expand Down Expand Up @@ -790,6 +793,8 @@ def send_document(
data['caption'] = caption
if parse_mode:
data['parse_mode'] = parse_mode
if disable_content_type_detection is not None:
data['disable_content_type_detection'] = disable_content_type_detection
if thumb:
if InputFile.is_file(thumb):
thumb = cast(IO, thumb)
Expand Down
4 changes: 4 additions & 0 deletions telegram/files/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Audio(TelegramObject):
performer (:obj:`str`): Optional. Performer of the audio as defined by sender or by audio
tags.
title (:obj:`str`): Optional. Title of the audio as defined by sender or by audio tags.
file_name (:obj:`str`): Optional. Original filename as defined by sender.
mime_type (:obj:`str`): Optional. MIME type of the file as defined by sender.
file_size (:obj:`int`): Optional. File size.
thumb (:class:`telegram.PhotoSize`): Optional. Thumbnail of the album cover to
Expand All @@ -57,6 +58,7 @@ class Audio(TelegramObject):
performer (:obj:`str`, optional): Performer of the audio as defined by sender or by audio
tags.
title (:obj:`str`, optional): Title of the audio as defined by sender or by audio tags.
file_name (:obj:`str`, optional): Original filename as defined by sender.
mime_type (:obj:`str`, optional): MIME type of the file as defined by sender.
file_size (:obj:`int`, optional): File size.
thumb (:class:`telegram.PhotoSize`, optional): Thumbnail of the album cover to
Expand All @@ -77,6 +79,7 @@ def __init__(
file_size: int = None,
thumb: PhotoSize = None,
bot: 'Bot' = None,
file_name: str = None,
**_kwargs: Any,
):
# Required
Expand All @@ -86,6 +89,7 @@ def __init__(
# Optionals
self.performer = performer
self.title = title
self.file_name = file_name
self.mime_type = mime_type
self.file_size = file_size
self.thumb = thumb
Expand Down
8 changes: 8 additions & 0 deletions telegram/files/inputmedia.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ class InputMediaDocument(InputMedia):
caption (:obj:`str`): Optional. Caption of the document to be sent.
parse_mode (:obj:`str`): Optional. The parse mode to use for text formatting.
thumb (:class:`telegram.InputFile`): Optional. Thumbnail of the file to send.
disable_content_type_detection (:obj:`bool`): Optional. Disables automatic server-side
content type detection for files uploaded using multipart/form-data. Always true, if
the document is sent as part of an album.

Args:
media (:obj:`str` | `filelike object` | :class:`telegram.Document`): File to send. Pass a
Expand All @@ -352,6 +355,9 @@ class InputMediaDocument(InputMedia):
in JPEG format and less than 200 kB in size. A thumbnail's width and height should
not exceed 320. Ignored if the file is not uploaded using multipart/form-data.
Thumbnails can't be reused and can be only uploaded as a new file.
disable_content_type_detection (:obj:`bool`, optional): Disables automatic server-side
content type detection for files uploaded using multipart/form-data. Always true, if
the document is sent as part of an album.
"""

def __init__(
Expand All @@ -360,6 +366,7 @@ def __init__(
thumb: FileLike = None,
caption: str = None,
parse_mode: Union[str, DefaultValue] = DEFAULT_NONE,
disable_content_type_detection: bool = None,
):
self.type = 'document'

Expand All @@ -381,3 +388,4 @@ def __init__(
if caption:
self.caption = caption
self.parse_mode = parse_mode
self.disable_content_type_detection = disable_content_type_detection
4 changes: 4 additions & 0 deletions telegram/files/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Video(TelegramObject):
height (:obj:`int`): Video height as defined by sender.
duration (:obj:`int`): Duration of the video in seconds as defined by sender.
thumb (:class:`telegram.PhotoSize`): Optional. Video thumbnail.
file_name (:obj:`str`): Optional. Original filename as defined by sender.
mime_type (:obj:`str`): Optional. Mime type of a file as defined by sender.
file_size (:obj:`int`): Optional. File size.
bot (:class:`telegram.Bot`): Optional. The Bot to use for instance methods.
Expand All @@ -56,6 +57,7 @@ class Video(TelegramObject):
height (:obj:`int`): Video height as defined by sender.
duration (:obj:`int`): Duration of the video in seconds as defined by sender.
thumb (:class:`telegram.PhotoSize`, optional): Video thumbnail.
file_name (:obj:`str`, optional): Original filename as defined by sender.
mime_type (:obj:`str`, optional): Mime type of a file as defined by sender.
file_size (:obj:`int`, optional): File size.
bot (:class:`telegram.Bot`, optional): The Bot to use for instance methods.
Expand All @@ -74,6 +76,7 @@ def __init__(
mime_type: str = None,
file_size: int = None,
bot: 'Bot' = None,
file_name: str = None,
**_kwargs: Any,
):
# Required
Expand All @@ -84,6 +87,7 @@ def __init__(
self.duration = int(duration)
# Optionals
self.thumb = thumb
self.file_name = file_name
self.mime_type = mime_type
self.file_size = file_size
self.bot = bot
Expand Down
5 changes: 5 additions & 0 deletions tests/test_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class TestAudio:
caption = 'Test *audio*'
performer = 'Leandro Toledo'
title = 'Teste'
file_name = 'telegram.mp3'
duration = 3
# audio_file_url = 'https://python-telegram-bot.org/static/testfiles/telegram.mp3'
# Shortened link, the above one is cached with the wrong duration.
Expand Down Expand Up @@ -99,6 +100,7 @@ def test_send_all_args(self, bot, chat_id, audio_file, thumb_file):
assert message.audio.duration == self.duration
assert message.audio.performer == self.performer
assert message.audio.title == self.title
assert message.audio.file_name == self.file_name
assert message.audio.mime_type == self.mime_type
assert message.audio.file_size == self.file_size
assert message.audio.thumb.file_size == self.thumb_file_size
Expand Down Expand Up @@ -192,6 +194,7 @@ def test_de_json(self, bot, audio):
'duration': self.duration,
'performer': self.performer,
'title': self.title,
'file_name': self.file_name,
'caption': self.caption,
'mime_type': self.mime_type,
'file_size': self.file_size,
Expand All @@ -204,6 +207,7 @@ def test_de_json(self, bot, audio):
assert json_audio.duration == self.duration
assert json_audio.performer == self.performer
assert json_audio.title == self.title
assert json_audio.file_name == self.file_name
assert json_audio.mime_type == self.mime_type
assert json_audio.file_size == self.file_size
assert json_audio.thumb == audio.thumb
Expand All @@ -217,6 +221,7 @@ def test_to_dict(self, audio):
assert audio_dict['duration'] == audio.duration
assert audio_dict['mime_type'] == audio.mime_type
assert audio_dict['file_size'] == audio.file_size
assert audio_dict['file_name'] == audio.file_name

@flaky(3, 1)
@pytest.mark.timeout(10)
Expand Down
22 changes: 16 additions & 6 deletions tests/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,23 @@ def test_send_resend(self, bot, chat_id, document):

assert message.document == document

def test_send_with_document(self, monkeypatch, bot, chat_id, document):
def test(url, data, **kwargs):
return data['document'] == document.file_id
@pytest.mark.parametrize('disable_content_type_detection', [True, False, None])
def test_send_with_document(
self, monkeypatch, bot, chat_id, document, disable_content_type_detection
):
def make_assertion(url, data, **kwargs):
type_detection = (
data.get('disable_content_type_detection') == disable_content_type_detection
)
return data['document'] == document.file_id and type_detection

monkeypatch.setattr(bot.request, 'post', make_assertion)

monkeypatch.setattr(bot.request, 'post', test)

message = bot.send_document(document=document, chat_id=chat_id)
message = bot.send_document(
document=document,
chat_id=chat_id,
disable_content_type_detection=disable_content_type_detection,
)

assert message

Expand Down
10 changes: 10 additions & 0 deletions tests/test_inputmedia.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def input_media_document(class_thumb_file):
caption=TestInputMediaDocument.caption,
thumb=class_thumb_file,
parse_mode=TestInputMediaDocument.parse_mode,
disable_content_type_detection=TestInputMediaDocument.disable_content_type_detection,
)


Expand Down Expand Up @@ -282,12 +283,17 @@ class TestInputMediaDocument:
media = "NOTAREALFILEID"
caption = "My Caption"
parse_mode = 'HTML'
disable_content_type_detection = True

def test_expected_values(self, input_media_document):
assert input_media_document.type == self.type_
assert input_media_document.media == self.media
assert input_media_document.caption == self.caption
assert input_media_document.parse_mode == self.parse_mode
assert (
input_media_document.disable_content_type_detection
== self.disable_content_type_detection
)
assert isinstance(input_media_document.thumb, InputFile)

def test_to_dict(self, input_media_document):
Expand All @@ -296,6 +302,10 @@ def test_to_dict(self, input_media_document):
assert input_media_document_dict['media'] == input_media_document.media
assert input_media_document_dict['caption'] == input_media_document.caption
assert input_media_document_dict['parse_mode'] == input_media_document.parse_mode
assert (
input_media_document['disable_content_type_detection']
== input_media_document.disable_content_type_detection
)

def test_with_document(self, document): # noqa: F811
# fixture found in test_document
Expand Down
6 changes: 6 additions & 0 deletions tests/test_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class TestVideo:
file_size = 326534
mime_type = 'video/mp4'
supports_streaming = True
file_name = 'telegram.mp4'

thumb_width = 180
thumb_height = 320
Expand Down Expand Up @@ -109,6 +110,8 @@ def test_send_all_args(self, bot, chat_id, video_file, video, thumb_file):
assert message.video.thumb.width == self.thumb_width
assert message.video.thumb.height == self.thumb_height

assert message.video.file_name == self.file_name

@flaky(3, 1)
@pytest.mark.timeout(10)
def test_get_and_download(self, bot, video):
Expand Down Expand Up @@ -208,6 +211,7 @@ def test_de_json(self, bot):
'duration': self.duration,
'mime_type': self.mime_type,
'file_size': self.file_size,
'file_name': self.file_name,
}
json_video = Video.de_json(json_dict, bot)

Expand All @@ -218,6 +222,7 @@ def test_de_json(self, bot):
assert json_video.duration == self.duration
assert json_video.mime_type == self.mime_type
assert json_video.file_size == self.file_size
assert json_video.file_name == self.file_name

def test_to_dict(self, video):
video_dict = video.to_dict()
Expand All @@ -230,6 +235,7 @@ def test_to_dict(self, video):
assert video_dict['duration'] == video.duration
assert video_dict['mime_type'] == video.mime_type
assert video_dict['file_size'] == video.file_size
assert video_dict['file_name'] == video.file_name

@flaky(3, 1)
@pytest.mark.timeout(10)
Expand Down