X Tutup
Skip to content

Commit ed77afa

Browse files
committed
Merge pull request python-telegram-bot#57 from ergoz/testing
Add ability for custom filename in send methods and InputFile
2 parents cfa5e8a + c7f2add commit ed77afa

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ The following wonderful people contributed directly or indirectly to this projec
1010

1111
- `Avanatiker <https://github.com/Avanatiker>`_
1212
- `bimmlerd <https://github.com/bimmlerd>`_
13+
- `ErgoZ Riftbit Vaper <https://github.com/ergoz>`_
1314
- `franciscod <https://github.com/franciscod>`_
1415
- `JASON0916 <https://github.com/JASON0916>`_
1516
- `JRoot3D <https://github.com/JRoot3D>`_

telegram/bot.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ def forwardMessage(self,
252252
def sendPhoto(self,
253253
chat_id,
254254
photo,
255+
filename=None,
255256
caption=None,
256257
**kwargs):
257258
"""Use this method to send photos.
@@ -263,6 +264,9 @@ def sendPhoto(self,
263264
Photo to send. You can either pass a file_id as String to resend a
264265
photo that is already on the Telegram servers, or upload a new
265266
photo using multipart/form-data.
267+
filename:
268+
File name that shows in telegram message (it is usefull when you
269+
send file generated by temp module, for example). [Optional]
266270
caption:
267271
Photo caption (may also be used when resending photos by file_id).
268272
[Optional]
@@ -282,6 +286,8 @@ def sendPhoto(self,
282286
data = {'chat_id': chat_id,
283287
'photo': photo}
284288

289+
if filename:
290+
data['filename'] = filename
285291
if caption:
286292
data['caption'] = caption
287293

@@ -292,6 +298,7 @@ def sendPhoto(self,
292298
def sendAudio(self,
293299
chat_id,
294300
audio,
301+
filename=None,
295302
duration=None,
296303
performer=None,
297304
title=None,
@@ -314,6 +321,9 @@ def sendAudio(self,
314321
Audio file to send. You can either pass a file_id as String to
315322
resend an audio that is already on the Telegram servers, or upload
316323
a new audio file using multipart/form-data.
324+
filename:
325+
File name that shows in telegram message (it is usefull when you
326+
send file generated by temp module, for example). [Optional]
317327
duration:
318328
Duration of sent audio in seconds. [Optional]
319329
performer:
@@ -336,6 +346,8 @@ def sendAudio(self,
336346
data = {'chat_id': chat_id,
337347
'audio': audio}
338348

349+
if filename:
350+
data['filename'] = filename
339351
if duration:
340352
data['duration'] = duration
341353
if performer:
@@ -350,6 +362,7 @@ def sendAudio(self,
350362
def sendDocument(self,
351363
chat_id,
352364
document,
365+
filename=None,
353366
**kwargs):
354367
"""Use this method to send general files.
355368
@@ -360,6 +373,9 @@ def sendDocument(self,
360373
File to send. You can either pass a file_id as String to resend a
361374
file that is already on the Telegram servers, or upload a new file
362375
using multipart/form-data.
376+
filename:
377+
File name that shows in telegram message (it is usefull when you
378+
send file generated by temp module, for example). [Optional]
363379
reply_to_message_id:
364380
If the message is a reply, ID of the original message. [Optional]
365381
reply_markup:
@@ -376,13 +392,17 @@ def sendDocument(self,
376392
data = {'chat_id': chat_id,
377393
'document': document}
378394

395+
if filename:
396+
data['filename'] = filename
397+
379398
return url, data
380399

381400
@log
382401
@message
383402
def sendSticker(self,
384403
chat_id,
385404
sticker,
405+
filename=None,
386406
**kwargs):
387407
"""Use this method to send .webp stickers.
388408
@@ -393,6 +413,9 @@ def sendSticker(self,
393413
Sticker to send. You can either pass a file_id as String to resend
394414
a sticker that is already on the Telegram servers, or upload a new
395415
sticker using multipart/form-data.
416+
filename:
417+
File name that shows in telegram message (it is usefull when you
418+
send file generated by temp module, for example). [Optional]
396419
reply_to_message_id:
397420
If the message is a reply, ID of the original message. [Optional]
398421
reply_markup:
@@ -409,13 +432,17 @@ def sendSticker(self,
409432
data = {'chat_id': chat_id,
410433
'sticker': sticker}
411434

435+
if filename:
436+
data['filename'] = filename
437+
412438
return url, data
413439

414440
@log
415441
@message
416442
def sendVideo(self,
417443
chat_id,
418444
video,
445+
filename=None,
419446
duration=None,
420447
caption=None,
421448
**kwargs):
@@ -429,6 +456,9 @@ def sendVideo(self,
429456
Video to send. You can either pass a file_id as String to resend a
430457
video that is already on the Telegram servers, or upload a new
431458
video file using multipart/form-data.
459+
filename:
460+
File name that shows in telegram message (it is usefull when you
461+
send file generated by temp module, for example). [Optional]
432462
duration:
433463
Duration of sent video in seconds. [Optional]
434464
caption:
@@ -450,6 +480,8 @@ def sendVideo(self,
450480
data = {'chat_id': chat_id,
451481
'video': video}
452482

483+
if filename:
484+
data['filename'] = filename
453485
if duration:
454486
data['duration'] = duration
455487
if caption:
@@ -462,6 +494,7 @@ def sendVideo(self,
462494
def sendVoice(self,
463495
chat_id,
464496
voice,
497+
filename=None,
465498
duration=None,
466499
**kwargs):
467500
"""Use this method to send audio files, if you want Telegram clients to
@@ -478,6 +511,9 @@ def sendVoice(self,
478511
Audio file to send. You can either pass a file_id as String to
479512
resend an audio that is already on the Telegram servers, or upload
480513
a new audio file using multipart/form-data.
514+
filename:
515+
File name that shows in telegram message (it is usefull when you
516+
send file generated by temp module, for example). [Optional]
481517
duration:
482518
Duration of sent audio in seconds. [Optional]
483519
reply_to_message_id:
@@ -496,6 +532,8 @@ def sendVoice(self,
496532
data = {'chat_id': chat_id,
497533
'voice': voice}
498534

535+
if filename:
536+
data['filename'] = filename
499537
if duration:
500538
data['duration'] = duration
501539

telegram/inputfile.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ def __init__(self,
6767

6868
if isinstance(self.input_file, file):
6969
self.input_file_content = self.input_file.read()
70-
self.filename = os.path.basename(self.input_file.name)
70+
if 'filename' in self.data:
71+
self.filename = self.data.pop('filename')
72+
else:
73+
self.filename = os.path.basename(self.input_file.name)
7174
self.mimetype = mimetypes.guess_type(self.filename)[0] or \
7275
DEFAULT_MIME_TYPE
7376

0 commit comments

Comments
 (0)
X Tutup