From b9cd9a66396f272216ef52feabe7a43366bd3d89 Mon Sep 17 00:00:00 2001 From: Csaba Henk Date: Tue, 2 Jan 2018 09:56:37 +0100 Subject: [PATCH 1/3] fix get_dialogs() return type in example Catching up with 238198db where get_dialogs return type was changed. --- telethon_examples/interactive_telegram_client.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/telethon_examples/interactive_telegram_client.py b/telethon_examples/interactive_telegram_client.py index 52c2c356..501d557b 100644 --- a/telethon_examples/interactive_telegram_client.py +++ b/telethon_examples/interactive_telegram_client.py @@ -138,15 +138,15 @@ class InteractiveTelegramClient(TelegramClient): # Entities represent the user, chat or channel # corresponding to the dialog on the same index. - dialogs, entities = self.get_dialogs(limit=dialog_count) + dialogs = self.get_dialogs(limit=dialog_count) i = None while i is None: print_title('Dialogs window') # Display them so the user can choose - for i, entity in enumerate(entities, start=1): - sprint('{}. {}'.format(i, get_display_name(entity))) + for i, dialog in enumerate(dialogs, start=1): + sprint('{}. {}'.format(i, get_display_name(dialog.entity))) # Let the user decide who they want to talk to print() @@ -177,7 +177,7 @@ class InteractiveTelegramClient(TelegramClient): i = None # Retrieve the selected user (or chat, or channel) - entity = entities[i] + entity = dialogs[i].entity # Show some information print_title('Chat with "{}"'.format(get_display_name(entity))) From 78871b697e9f6a330b5984a0d69231ca0f5aaa13 Mon Sep 17 00:00:00 2001 From: Csaba Henk Date: Tue, 2 Jan 2018 13:30:29 +0100 Subject: [PATCH 2/3] client: return the message in send_file, too --- telethon/telegram_client.py | 39 +++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/telethon/telegram_client.py b/telethon/telegram_client.py index 70c2784c..730f7445 100644 --- a/telethon/telegram_client.py +++ b/telethon/telegram_client.py @@ -363,6 +363,22 @@ class TelegramClient(TelegramBareClient): drafts = [Draft._from_update(self, u) for u in response.updates] return drafts + @staticmethod + def _get_response_message(request, result): + # Telegram seems to send updateMessageID first, then updateNewMessage, + # however let's not rely on that just in case. + msg_id = None + for update in result.updates: + if isinstance(update, UpdateMessageID): + if update.random_id == request.random_id: + msg_id = update.id + break + + for update in result.updates: + if isinstance(update, (UpdateNewChannelMessage, UpdateNewMessage)): + if update.message.id == msg_id: + return update.message + def send_message(self, entity, message, @@ -415,21 +431,7 @@ class TelegramClient(TelegramBareClient): entities=result.entities ) - # Telegram seems to send updateMessageID first, then updateNewMessage, - # however let's not rely on that just in case. - msg_id = None - for update in result.updates: - if isinstance(update, UpdateMessageID): - if update.random_id == request.random_id: - msg_id = update.id - break - - for update in result.updates: - if isinstance(update, (UpdateNewChannelMessage, UpdateNewMessage)): - if update.message.id == msg_id: - return update.message - - return None # Should not happen + return self._get_response_message(request, result) def delete_messages(self, entity, message_ids, revoke=True): """ @@ -723,11 +725,14 @@ class TelegramClient(TelegramBareClient): # Once the media type is properly specified and the file uploaded, # send the media message to the desired entity. - self(SendMediaRequest( + request = SendMediaRequest( peer=self.get_input_entity(entity), media=media, reply_to_msg_id=self._get_reply_to(reply_to) - )) + ) + result = self(request) + + return self._get_response_message(request, result) def send_voice_note(self, entity, file, caption='', upload_progress=None, reply_to=None): From 2c437c51bb672cd1eaa4155b205dcfa20f535eb8 Mon Sep 17 00:00:00 2001 From: Csaba Henk Date: Wed, 3 Jan 2018 12:47:38 +0100 Subject: [PATCH 3/3] client: add thumbnail support for send_file() --- telethon/telegram_client.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/telethon/telegram_client.py b/telethon/telegram_client.py index 730f7445..3be2ac62 100644 --- a/telethon/telegram_client.py +++ b/telethon/telegram_client.py @@ -631,6 +631,7 @@ class TelegramClient(TelegramBareClient): force_document=False, progress_callback=None, reply_to=None, attributes=None, + thumb=None, **kwargs): """ Sends a file to the specified entity. @@ -658,6 +659,8 @@ class TelegramClient(TelegramBareClient): :param attributes: Optional attributes that override the inferred ones, like DocumentAttributeFilename and so on. + :param thumb: + Optional thumbnail (for videos). :param kwargs: If "is_voice_note" in kwargs, despite its value, and the file is sent as a document, it will be sent as a voice note. @@ -716,11 +719,16 @@ class TelegramClient(TelegramBareClient): if not mime_type: mime_type = 'application/octet-stream' + input_kw = {} + if thumb: + input_kw['thumb'] = self.upload_file(thumb) + media = InputMediaUploadedDocument( file=file_handle, mime_type=mime_type, attributes=list(attr_dict.values()), - caption=caption + caption=caption, + **input_kw ) # Once the media type is properly specified and the file uploaded,