From 78f2c9549c2cd592a4bf2286f738be5d293440be Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 13 Feb 2018 14:28:45 +0100 Subject: [PATCH] Modify resolve_peer to accommodate Peer* types --- pyrogram/client/client.py | 44 +++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index 1f1b2ce1..d87b2042 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -703,25 +703,33 @@ class Client: return input_peer - def resolve_peer(self, peer_id: int or str): - if peer_id in ("self", "me"): - return InputPeerSelf() - else: - if type(peer_id) is str: - peer_id = peer_id.lower().strip("@") + def resolve_peer(self, peer_id: int or str or types.PeerUser or types.PeerChat or types.PeerChannel): + if type(peer_id) is str: + if peer_id in ("self", "me"): + return InputPeerSelf() - try: - return self.peers_by_username[peer_id] - except KeyError: - return self.resolve_username(peer_id) - else: - try: - return self.peers_by_id[peer_id] - except KeyError: - try: - return self.peers_by_id[int("-100" + str(peer_id))] - except KeyError: - raise PeerIdInvalid + peer_id = peer_id.lower().strip("@") + + try: + return self.peers_by_username[peer_id] + except KeyError: + return self.resolve_username(peer_id) + + if type(peer_id) is not int: + if isinstance(peer_id, types.PeerUser): + peer_id = peer_id.user_id + elif isinstance(peer_id, types.PeerChat): + peer_id = peer_id.chat_id + elif isinstance(peer_id, types.PeerChannel): + peer_id = int("-100" + str(peer_id.channel_id)) + + try: + return self.peers_by_id[peer_id] + except KeyError: + try: + return self.peers_by_id[int("-100" + str(peer_id))] + except KeyError: + raise PeerIdInvalid def get_me(self): """A simple method for testing the user authorization. Requires no parameters.