From 73fe7258d17c2b13464626e0a166c7e51d8a2f15 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Wed, 13 Sep 2017 12:00:27 +0200 Subject: [PATCH] Modify .send_message to return the sent message object --- telethon/telegram_client.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/telethon/telegram_client.py b/telethon/telegram_client.py index 61394555..559bdba1 100644 --- a/telethon/telegram_client.py +++ b/telethon/telegram_client.py @@ -40,7 +40,8 @@ from .tl.types import ( InputDocumentFileLocation, InputFileLocation, InputMediaUploadedDocument, InputMediaUploadedPhoto, InputPeerEmpty, Message, MessageMediaContact, MessageMediaDocument, MessageMediaPhoto, - InputUserSelf, UserProfilePhoto, ChatPhoto + InputUserSelf, UserProfilePhoto, ChatPhoto, UpdateMessageID, + UpdateNewMessage ) from .utils import find_user_or_chat, get_extension @@ -411,7 +412,7 @@ class TelegramClient(TelegramBareClient): reply_to=None, link_preview=True): """Sends a message to the given entity (or input peer) - and returns the sent message ID. + and returns the sent message as a Telegram object. If 'reply_to' is set to either a message or a message ID, the sent message will be replying to such message. @@ -424,7 +425,21 @@ class TelegramClient(TelegramBareClient): reply_to_msg_id=self._get_reply_to(reply_to) ) result = self(request) - return request.random_id + # 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, UpdateNewMessage): + if update.message.id == msg_id: + return update.message + + return None # Should not happen def get_message_history(self, entity,