Skip to content

Commit

Permalink
Support "string" peers on .get_input_entity
Browse files Browse the repository at this point in the history
  • Loading branch information
Lonami committed Oct 1, 2017
1 parent 8df4d7c commit d30fdbe
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions telethon/telegram_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
socks = None

from . import TelegramBareClient
from . import helpers as utils
from . import helpers, utils
from .errors import (
RPCError, UnauthorizedError, InvalidParameterError, PhoneCodeEmptyError,
PhoneCodeExpiredError, PhoneCodeHashEmptyError, PhoneCodeInvalidError
Expand Down Expand Up @@ -47,7 +47,6 @@
UpdateNewMessage, UpdateShortSentMessage,
PeerUser, InputPeerUser, InputPeerChat, InputPeerChannel)
from .tl.types.messages import DialogsSlice
from .utils import find_user_or_chat, get_extension


class TelegramClient(TelegramBareClient):
Expand Down Expand Up @@ -184,7 +183,7 @@ def sign_in(self, phone=None, code=None,
elif password:
salt = self(GetPasswordRequest()).current_salt
result = self(CheckPasswordRequest(
utils.get_password_hash(password, salt)
helpers.get_password_hash(password, salt)
))
elif bot_token:
result = self(ImportBotAuthorizationRequest(
Expand Down Expand Up @@ -285,19 +284,20 @@ def get_dialogs(self,
break

offset_date = r.messages[-1].date
offset_peer = find_user_or_chat(r.dialogs[-1].peer, entities,
entities)
offset_peer = utils.find_user_or_chat(
r.dialogs[-1].peer, entities, entities
)
offset_id = r.messages[-1].id & 4294967296 # Telegram/danog magic

# Sort by message date
no_date = datetime.fromtimestamp(0)
dialogs = sorted(
ds = sorted(
list(dialogs.values()),
key=lambda d: getattr(messages[d.top_message], 'date', no_date)
)
return (
dialogs,
[find_user_or_chat(d.peer, entities, entities) for d in dialogs]
ds,
[utils.find_user_or_chat(d.peer, entities, entities) for d in ds]
)

# endregion
Expand Down Expand Up @@ -393,10 +393,13 @@ def get_message_history(self,
total_messages = getattr(result, 'count', len(result.messages))

# Iterate over all the messages and find the sender User
entities = [find_user_or_chat(m.from_id, result.users, result.chats)
if m.from_id is not None else
find_user_or_chat(m.to_id, result.users, result.chats)
for m in result.messages]
entities = [
utils.find_user_or_chat(m.from_id, result.users, result.chats)
if m.from_id is not None else
utils.find_user_or_chat(m.to_id, result.users, result.chats)

for m in result.messages
]

return total_messages, result.messages, entities

Expand Down Expand Up @@ -698,7 +701,7 @@ def _download_document(self, mm_doc, file, date, progress_callback):
))

file = self._get_proper_filename(
file, 'document', get_extension(mm_doc),
file, 'document', utils.get_extension(mm_doc),
date=date, possible_names=possible_names
)

Expand Down Expand Up @@ -895,6 +898,10 @@ def get_input_entity(self, peer):
If even after
"""
if isinstance(peer, str):
# Let .get_entity resolve the username or phone
return utils.get_input_peer(self.get_entity(peer))

is_peer = False
if isinstance(peer, int):
peer = PeerUser(peer)
Expand Down

0 comments on commit d30fdbe

Please sign in to comment.