Skip to content
This repository was archived by the owner on Dec 23, 2024. It is now read-only.
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add universal helper function instead
  • Loading branch information
kai3341 committed Dec 20, 2022
commit 7ee3b77830888d4ab02dc5cf838667a99bc5ba70
33 changes: 15 additions & 18 deletions pyrogram/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@
log = logging.getLogger(__name__)


def getattr_nested(target, *args: str, default=None):
"""
TODO: move into utils?
"""
result = target
for attr in args:
if hasattr(result, attr):
result = getattr(result, attr)
else:
return default

return result


class Client(Methods):
"""Pyrogram Client, the main means for interacting with Telegram.

Expand Down Expand Up @@ -483,31 +497,14 @@ async def fetch_peers(self, peers: List[Union[raw.types.User, raw.types.Chat, ra

return is_min

def _handle_updates__extract_channel_id__from_message(self, update):
"""
Re-implementation extraction `channel_id` logic as is
Here we trying to extract it from message
"""
message = getattr(update, "message", None)

if message is None:
return

peer_id = getattr(message, "peer_id", None)

if peer_id is None:
return

return getattr(peer_id, "channel_id", None)

def _handle_updates__extract_channel_id(self, update):
"""
Re-implementation extraction `channel_id` logic as is
TODO: maybe it would be easier to check update type?
"""

return (
self._handle_updates__extract_channel_id__from_message(update) or
getattr_nested(update, "message", "peer_id", "channel_id") or
getattr(update, "channel_id", None)
)

Expand Down