Merge pull request #519 from csabahenk/master

* Fix interactive example (get dialogs)
* Return message in send_file
* Support thumbnail for send_file
This commit is contained in:
Lonami 2018-01-04 15:30:42 +01:00 committed by GitHub
commit 32505b905f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 22 deletions

View File

@ -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):
"""
@ -629,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.
@ -656,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.
@ -714,20 +719,28 @@ 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,
# 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):

View File

@ -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)))