forked from TelegramPlayground/PyroTGFork
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolve_peer.py
More file actions
139 lines (120 loc) · 5.02 KB
/
Copy pathresolve_peer.py
File metadata and controls
139 lines (120 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
#
# This file is part of Pyrogram.
#
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrogram is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import logging
import re
from typing import Union
import pyrogram
from pyrogram import raw, utils
from pyrogram.errors import PeerIdInvalid
log = logging.getLogger(__name__)
class ResolvePeer:
async def resolve_peer(
self: "pyrogram.Client",
peer_id: Union[int, str]
) -> Union[raw.base.InputPeer, raw.base.InputUser, raw.base.InputChannel]:
"""Get the InputPeer of a known peer id.
Useful whenever an InputPeer type is required.
.. note::
This is a utility method intended to be used **only** when working with raw
:obj:`functions <pyrogram.api.functions>` (i.e: a Telegram API method you wish to use which is not
available yet in the Client class as an easy-to-use method).
.. include:: /_includes/usable-by/users-bots.rst
Parameters:
peer_id (``int`` | ``str``):
The peer id you want to extract the InputPeer from.
Can be a direct id (int), a username (str) or a phone number (str).
Returns:
``InputPeer``: On success, the resolved peer id is returned in form of an InputPeer object.
Raises:
KeyError: In case the peer doesn't exist in the internal database.
"""
if not self.is_connected:
raise ConnectionError("Client has not been started yet")
if peer_id in ("self", "me"):
return raw.types.InputPeerSelf()
try:
return await self.storage.get_peer_by_id(peer_id)
except KeyError:
if isinstance(peer_id, str):
peer_id = re.sub(r"[@+\s]", "", peer_id.lower())
try:
int(peer_id)
except ValueError:
try:
return await self.storage.get_peer_by_username(peer_id)
except KeyError:
r = await self.invoke(
raw.functions.contacts.ResolveUsername(
username=peer_id
)
)
userid = getattr(
r.peer,
"user_id",
None
)
channelid = getattr(
r.peer,
"channel_id",
None
)
if userid:
return await self.storage.get_peer_by_id(userid)
if channelid:
return await self.storage.get_peer_by_id(utils.get_channel_id(channelid))
return await self.storage.get_peer_by_username(peer_id)
else:
try:
return await self.storage.get_peer_by_phone_number(peer_id)
except KeyError:
raise PeerIdInvalid
peer_type = utils.get_peer_type(peer_id)
if peer_type == "user":
await self.fetch_peers(
await self.invoke(
raw.functions.users.GetUsers(
id=[
raw.types.InputUser(
user_id=peer_id,
access_hash=0
)
]
)
)
)
elif peer_type == "chat":
await self.invoke(
raw.functions.messages.GetChats(
id=[-peer_id]
)
)
else:
await self.invoke(
raw.functions.channels.GetChannels(
id=[
raw.types.InputChannel(
channel_id=utils.get_channel_id(peer_id),
access_hash=0
)
]
)
)
try:
return await self.storage.get_peer_by_id(peer_id)
except KeyError:
raise PeerIdInvalid