From 714adc13eeb9ce6273a46858007d63e3e7f89904 Mon Sep 17 00:00:00 2001 From: ErgoZ Date: Tue, 8 Sep 2015 20:42:23 +0300 Subject: [PATCH 1/6] Add ability to set custom filename For commands that uses InputFile class --- telegram/bot.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/telegram/bot.py b/telegram/bot.py index 5b1033009..00bb858f5 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -252,6 +252,7 @@ class Bot(TelegramObject): def sendPhoto(self, chat_id, photo, + filename=None, caption=None, **kwargs): """Use this method to send photos. @@ -263,6 +264,9 @@ class Bot(TelegramObject): Photo to send. You can either pass a file_id as String to resend a photo that is already on the Telegram servers, or upload a new photo using multipart/form-data. + filename: + File name that shows in telegram message (it is usefull when you + send file generated by temp module, for example). [Optional] caption: Photo caption (may also be used when resending photos by file_id). [Optional] @@ -282,6 +286,8 @@ class Bot(TelegramObject): data = {'chat_id': chat_id, 'photo': photo} + if filename: + data['filename'] = filename if caption: data['caption'] = caption @@ -292,6 +298,7 @@ class Bot(TelegramObject): def sendAudio(self, chat_id, audio, + filename=None, duration=None, performer=None, title=None, @@ -314,6 +321,9 @@ class Bot(TelegramObject): Audio file to send. You can either pass a file_id as String to resend an audio that is already on the Telegram servers, or upload a new audio file using multipart/form-data. + filename: + File name that shows in telegram message (it is usefull when you + send file generated by temp module, for example). [Optional] duration: Duration of sent audio in seconds. [Optional] performer: @@ -336,6 +346,8 @@ class Bot(TelegramObject): data = {'chat_id': chat_id, 'audio': audio} + if filename: + data['filename'] = filename if duration: data['duration'] = duration if performer: @@ -350,6 +362,7 @@ class Bot(TelegramObject): def sendDocument(self, chat_id, document, + filename=None, **kwargs): """Use this method to send general files. @@ -360,6 +373,9 @@ class Bot(TelegramObject): File to send. You can either pass a file_id as String to resend a file that is already on the Telegram servers, or upload a new file using multipart/form-data. + filename: + File name that shows in telegram message (it is usefull when you + send file generated by temp module, for example). [Optional] reply_to_message_id: If the message is a reply, ID of the original message. [Optional] reply_markup: @@ -376,6 +392,9 @@ class Bot(TelegramObject): data = {'chat_id': chat_id, 'document': document} + if filename: + data['filename'] = filename + return url, data @log @@ -383,6 +402,7 @@ class Bot(TelegramObject): def sendSticker(self, chat_id, sticker, + filename=None, **kwargs): """Use this method to send .webp stickers. @@ -393,6 +413,9 @@ class Bot(TelegramObject): Sticker to send. You can either pass a file_id as String to resend a sticker that is already on the Telegram servers, or upload a new sticker using multipart/form-data. + filename: + File name that shows in telegram message (it is usefull when you + send file generated by temp module, for example). [Optional] reply_to_message_id: If the message is a reply, ID of the original message. [Optional] reply_markup: @@ -409,6 +432,9 @@ class Bot(TelegramObject): data = {'chat_id': chat_id, 'sticker': sticker} + if filename: + data['filename'] = filename + return url, data @log @@ -416,6 +442,7 @@ class Bot(TelegramObject): def sendVideo(self, chat_id, video, + filename=None, duration=None, caption=None, **kwargs): @@ -429,6 +456,9 @@ class Bot(TelegramObject): Video to send. You can either pass a file_id as String to resend a video that is already on the Telegram servers, or upload a new video file using multipart/form-data. + filename: + File name that shows in telegram message (it is usefull when you + send file generated by temp module, for example). [Optional] duration: Duration of sent video in seconds. [Optional] caption: @@ -450,6 +480,8 @@ class Bot(TelegramObject): data = {'chat_id': chat_id, 'video': video} + if filename: + data['filename'] = filename if duration: data['duration'] = duration if caption: @@ -462,6 +494,7 @@ class Bot(TelegramObject): def sendVoice(self, chat_id, voice, + filename=None, duration=None, **kwargs): """Use this method to send audio files, if you want Telegram clients to @@ -478,6 +511,9 @@ class Bot(TelegramObject): Audio file to send. You can either pass a file_id as String to resend an audio that is already on the Telegram servers, or upload a new audio file using multipart/form-data. + filename: + File name that shows in telegram message (it is usefull when you + send file generated by temp module, for example). [Optional] duration: Duration of sent audio in seconds. [Optional] reply_to_message_id: @@ -496,6 +532,8 @@ class Bot(TelegramObject): data = {'chat_id': chat_id, 'voice': voice} + if filename: + data['filename'] = filename if duration: data['duration'] = duration From 4a761d0611a3b78e206906ce0e8dc9c5a7b21c97 Mon Sep 17 00:00:00 2001 From: ErgoZ Date: Tue, 8 Sep 2015 20:43:28 +0300 Subject: [PATCH 2/6] Add ability to set custom filename (fix InputFile class) For commands that uses InputFile class --- telegram/inputfile.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/telegram/inputfile.py b/telegram/inputfile.py index 0d27ff194..ee77fe0e3 100644 --- a/telegram/inputfile.py +++ b/telegram/inputfile.py @@ -67,7 +67,10 @@ class InputFile(object): if isinstance(self.input_file, file): self.input_file_content = self.input_file.read() - self.filename = os.path.basename(self.input_file.name) + if self.data.has_key('filename') and self.data['filename']: + self.filename = self.data['filename'] + else: + self.filename = os.path.basename(self.input_file.name) self.mimetype = mimetypes.guess_type(self.filename)[0] or \ DEFAULT_MIME_TYPE From 04967815013c1eacd848ca8633dcbd337574b9b8 Mon Sep 17 00:00:00 2001 From: ErgoZ Riftbit Vaper Date: Tue, 8 Sep 2015 20:45:44 +0300 Subject: [PATCH 3/6] Update AUTHORS.rst --- AUTHORS.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS.rst b/AUTHORS.rst index 5ce4542de..9b6f57d20 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -10,6 +10,7 @@ The following wonderful people contributed directly or indirectly to this projec - `Avanatiker `_ - `bimmlerd `_ +- `ErgoZ Riftbit Vaper `_ - `franciscod `_ - `JASON0916 `_ - `JRoot3D `_ From 6237bb636c476194bd1bb2ff3e100279486ec96b Mon Sep 17 00:00:00 2001 From: ErgoZ Riftbit Vaper Date: Tue, 8 Sep 2015 20:54:50 +0300 Subject: [PATCH 4/6] Fix InputFile class for python 3+ support --- telegram/inputfile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegram/inputfile.py b/telegram/inputfile.py index ee77fe0e3..9a5103df2 100644 --- a/telegram/inputfile.py +++ b/telegram/inputfile.py @@ -67,7 +67,7 @@ class InputFile(object): if isinstance(self.input_file, file): self.input_file_content = self.input_file.read() - if self.data.has_key('filename') and self.data['filename']: + if 'filename' in self.data and self.data['filename']: self.filename = self.data['filename'] else: self.filename = os.path.basename(self.input_file.name) From 8a074fd719eb1ec3b1a561a5a7ac073f094703bd Mon Sep 17 00:00:00 2001 From: ErgoZ Riftbit Vaper Date: Tue, 8 Sep 2015 21:13:11 +0300 Subject: [PATCH 5/6] Delete unused check (forget from previous local version) --- telegram/inputfile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegram/inputfile.py b/telegram/inputfile.py index 9a5103df2..ec42b85ff 100644 --- a/telegram/inputfile.py +++ b/telegram/inputfile.py @@ -67,7 +67,7 @@ class InputFile(object): if isinstance(self.input_file, file): self.input_file_content = self.input_file.read() - if 'filename' in self.data and self.data['filename']: + if 'filename' in self.data: self.filename = self.data['filename'] else: self.filename = os.path.basename(self.input_file.name) From c7f2add463662ec94b302c91154aae9fb7bb0838 Mon Sep 17 00:00:00 2001 From: ErgoZ Riftbit Vaper Date: Tue, 8 Sep 2015 21:17:54 +0300 Subject: [PATCH 6/6] small pretty fix for self.data.pop('filename') --- telegram/inputfile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegram/inputfile.py b/telegram/inputfile.py index ec42b85ff..5d42f734b 100644 --- a/telegram/inputfile.py +++ b/telegram/inputfile.py @@ -68,7 +68,7 @@ class InputFile(object): if isinstance(self.input_file, file): self.input_file_content = self.input_file.read() if 'filename' in self.data: - self.filename = self.data['filename'] + self.filename = self.data.pop('filename') else: self.filename = os.path.basename(self.input_file.name) self.mimetype = mimetypes.guess_type(self.filename)[0] or \