Skip to content

Commit 30e304f

Browse files
authored
Merge pull request eternnoir#1204 from floydya/file-name-patch
Allows to set visible document file_name on send.
2 parents 430b34c + a791ff4 commit 30e304f

3 files changed

Lines changed: 19 additions & 4 deletions

File tree

‎telebot/__init__.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,8 @@ def send_document(
11231123
timeout: Optional[int]=None,
11241124
thumb: Optional[Union[Any, str]]=None,
11251125
caption_entities: Optional[List[types.MessageEntity]]=None,
1126-
allow_sending_without_reply: Optional[bool]=None) -> types.Message:
1126+
allow_sending_without_reply: Optional[bool]=None,
1127+
file_name: Optional[str]=None) -> types.Message:
11271128
"""
11281129
Use this method to send general files.
11291130
:param chat_id:
@@ -1137,6 +1138,7 @@ def send_document(
11371138
:param thumb: InputFile or String : Thumbnail of the file sent
11381139
:param caption_entities:
11391140
:param allow_sending_without_reply:
1141+
:param file_name:
11401142
:return: API reply.
11411143
"""
11421144
parse_mode = self.parse_mode if (parse_mode is None) else parse_mode
@@ -1145,7 +1147,7 @@ def send_document(
11451147
apihelper.send_data(
11461148
self.token, chat_id, data, 'document', reply_to_message_id, reply_markup,
11471149
parse_mode, disable_notification, timeout, caption, thumb, caption_entities,
1148-
allow_sending_without_reply))
1150+
allow_sending_without_reply, file_name))
11491151

11501152
def send_sticker(
11511153
self, chat_id: Union[int, str], data: Union[Any, str],

‎telebot/apihelper.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -806,12 +806,15 @@ def send_audio(token, chat_id, audio, caption=None, duration=None, performer=Non
806806

807807
def send_data(token, chat_id, data, data_type, reply_to_message_id=None, reply_markup=None, parse_mode=None,
808808
disable_notification=None, timeout=None, caption=None, thumb=None, caption_entities=None,
809-
allow_sending_without_reply=None, disable_content_type_detection=None):
809+
allow_sending_without_reply=None, disable_content_type_detection=None, file_name=None):
810810
method_url = get_method_by_type(data_type)
811811
payload = {'chat_id': chat_id}
812812
files = None
813813
if not util.is_string(data):
814-
files = {data_type: data}
814+
file_data = data
815+
if file_name is not None:
816+
file_data = (file_name, data)
817+
files = {data_type: file_data}
815818
else:
816819
payload[data_type] = data
817820
if reply_to_message_id:

‎tests/test_telebot.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,16 @@ def test_send_file(self):
126126
ret_msg = tb.send_document(CHAT_ID, ret_msg.document.file_id)
127127
assert ret_msg.message_id
128128

129+
def test_send_file_with_filename(self):
130+
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
131+
tb = telebot.TeleBot(TOKEN)
132+
133+
ret_msg = tb.send_document(CHAT_ID, file_data)
134+
assert ret_msg.message_id
135+
136+
ret_msg = tb.send_document(CHAT_ID, file_data, file_name="test.jpg")
137+
assert ret_msg.message_id
138+
129139
def test_send_file_dis_noti(self):
130140
file_data = open('../examples/detailed_example/kitten.jpg', 'rb')
131141
tb = telebot.TeleBot(TOKEN)

0 commit comments

Comments
 (0)