forked from pyrogram/pyrogram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.py
More file actions
349 lines (266 loc) · 10.7 KB
/
user.py
File metadata and controls
349 lines (266 loc) · 10.7 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2019 Dan Tès <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 html
from typing import List
import pyrogram
from pyrogram.api import types
from .chat_photo import ChatPhoto
from .restriction import Restriction
from ..object import Object
from ..update import Update
class User(Object, Update):
"""A Telegram user or bot.
Parameters:
id (``int``):
Unique identifier for this user or bot.
is_self(``bool``, *optional*):
True, if this user is you yourself.
is_contact(``bool``, *optional*):
True, if this user is in your contacts.
is_mutual_contact(``bool``, *optional*):
True, if you both have each other's contact.
is_deleted(``bool``, *optional*):
True, if this user is deleted.
is_bot (``bool``, *optional*):
True, if this user is a bot.
is_verified (``bool``, *optional*):
True, if this user has been verified by Telegram.
is_restricted (``bool``, *optional*):
True, if this user has been restricted. Bots only.
See *restriction_reason* for details.
is_scam (``bool``, *optional*):
True, if this user has been flagged for scam.
is_support (``bool``, *optional*):
True, if this user is part of the Telegram support team.
first_name (``str``, *optional*):
User's or bot's first name.
last_name (``str``, *optional*):
User's or bot's last name.
status (``str``, *optional*):
User's Last Seen & Online status.
Can be one of the following:
"*online*", user is online right now.
"*offline*", user is currently offline.
"*recently*", user with hidden last seen time who was online between 1 second and 2-3 days ago.
"*within_week*", user with hidden last seen time who was online between 2-3 and seven days ago.
"*within_month*", user with hidden last seen time who was online between 6-7 days and a month ago.
"*long_time_ago*", blocked user or user with hidden last seen time who was online more than a month ago.
*None*, for bots.
last_online_date (``int``, *optional*):
Last online date of a user. Only available in case status is "*offline*".
next_offline_date (``int``, *optional*):
Date when a user will automatically go offline. Only available in case status is "*online*".
username (``str``, *optional*):
User's or bot's username.
language_code (``str``, *optional*):
IETF language tag of the user's language.
dc_id (``int``, *optional*):
User's or bot's assigned DC (data center). Available only in case the user has set a public profile photo.
Note that this information is approximate; it is based on where Telegram stores a user profile pictures and
does not by any means tell you the user location (i.e. a user might travel far away, but will still connect
to its assigned DC). More info at `FAQs </faq#what-are-the-ip-addresses-of-telegram-data-centers>`_.
phone_number (``str``, *optional*):
User's phone number.
photo (:obj:`ChatPhoto <pyrogram.ChatPhoto>`, *optional*):
User's or bot's current profile photo. Suitable for downloads only.
restrictions (List of :obj:`Restriction`, *optional*):
The list of reasons why this bot might be unavailable to some users.
This field is available only in case *is_restricted* is True.
"""
def __init__(
self,
*,
client: "pyrogram.BaseClient" = None,
id: int,
is_self: bool = None,
is_contact: bool = None,
is_mutual_contact: bool = None,
is_deleted: bool = None,
is_bot: bool = None,
is_verified: bool = None,
is_restricted: bool = None,
is_scam: bool = None,
is_support: bool = None,
first_name: str = None,
last_name: str = None,
status: str = None,
last_online_date: int = None,
next_offline_date: int = None,
username: str = None,
language_code: str = None,
dc_id: int = None,
phone_number: str = None,
photo: ChatPhoto = None,
restrictions: List[Restriction] = None
):
super().__init__(client)
self.id = id
self.is_self = is_self
self.is_contact = is_contact
self.is_mutual_contact = is_mutual_contact
self.is_deleted = is_deleted
self.is_bot = is_bot
self.is_verified = is_verified
self.is_restricted = is_restricted
self.is_scam = is_scam
self.is_support = is_support
self.first_name = first_name
self.last_name = last_name
self.status = status
self.last_online_date = last_online_date
self.next_offline_date = next_offline_date
self.username = username
self.language_code = language_code
self.dc_id = dc_id
self.phone_number = phone_number
self.photo = photo
self.restrictions = restrictions
def __format__(self, format_spec):
if format_spec == "mention":
return '<a href="tg://user?id={0}">{1}</a>'.format(self.id, html.escape(self.first_name))
return html.escape(str(self))
@staticmethod
def _parse(client, user: types.User) -> "User" or None:
if user is None:
return None
return User(
id=user.id,
is_self=user.is_self,
is_contact=user.contact,
is_mutual_contact=user.mutual_contact,
is_deleted=user.deleted,
is_bot=user.bot,
is_verified=user.verified,
is_restricted=user.restricted,
is_scam=user.scam,
is_support=user.support,
first_name=user.first_name,
last_name=user.last_name,
**User._parse_status(user.status, user.bot),
username=user.username,
language_code=user.lang_code,
dc_id=getattr(user.photo, "dc_id", None),
phone_number=user.phone,
photo=ChatPhoto._parse(client, user.photo, user.id, user.access_hash),
restrictions=pyrogram.List([Restriction._parse(r) for r in user.restriction_reason]) or None,
client=client
)
@staticmethod
def _parse_status(user_status: types.UpdateUserStatus, is_bot: bool = False):
if isinstance(user_status, types.UserStatusOnline):
status, date = "online", user_status.expires
elif isinstance(user_status, types.UserStatusOffline):
status, date = "offline", user_status.was_online
elif isinstance(user_status, types.UserStatusRecently):
status, date = "recently", None
elif isinstance(user_status, types.UserStatusLastWeek):
status, date = "within_week", None
elif isinstance(user_status, types.UserStatusLastMonth):
status, date = "within_month", None
else:
status, date = "long_time_ago", None
last_online_date = None
next_offline_date = None
if is_bot:
status = None
if status == "online":
next_offline_date = date
if status == "offline":
last_online_date = date
return {
"status": status,
"last_online_date": last_online_date,
"next_offline_date": next_offline_date
}
@staticmethod
def _parse_user_status(client, user_status: types.UpdateUserStatus):
return User(
id=user_status.user_id,
**User._parse_status(user_status.status),
client=client
)
def archive(self):
"""Bound method *archive* of :obj:`User`.
Use as a shortcut for:
.. code-block:: python
client.archive_chats(123456789)
Example:
.. code-block:: python
user.archive()
Returns:
True on success.
Raises:
RPCError: In case of a Telegram RPC error.
"""
return self._client.archive_chats(self.id)
def unarchive(self):
"""Bound method *unarchive* of :obj:`User`.
Use as a shortcut for:
.. code-block:: python
client.unarchive_chats(123456789)
Example:
.. code-block:: python
user.unarchive()
Returns:
True on success.
Raises:
RPCError: In case of a Telegram RPC error.
"""
return self._client.unarchive_chats(self.id)
def block(self):
"""Bound method *block* of :obj:`User`.
Use as a shortcut for:
.. code-block:: python
client.block_user(123456789)
Example:
.. code-block:: python
user.block()
Returns:
True on success.
Raises:
RPCError: In case of a Telegram RPC error.
"""
return self._client.block_user(self.id)
def unblock(self):
"""Bound method *unblock* of :obj:`User`.
Use as a shortcut for:
.. code-block:: python
client.unblock_user(123456789)
Example:
.. code-block:: python
user.unblock()
Returns:
True on success.
Raises:
RPCError: In case of a Telegram RPC error.
"""
return self._client.unblock_user(self.id)
def get_common_chats(self):
"""Bound method *get_common_chats* of :obj:`User`.
Use as a shortcut for:
.. code-block:: python
client.get_common_chats(123456789)
Example:
.. code-block:: python
user.get_common_chats()
Returns:
True on success.
Raises:
RPCError: In case of a Telegram RPC error.
"""
return self._client.get_common_chats(self.id)