Skip to content

Commit 04cace4

Browse files
committed
Add get_user method
1 parent e70b421 commit 04cace4

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

pyrogram/methods/users/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from .get_common_chats import GetCommonChats
2525
from .get_default_emoji_statuses import GetDefaultEmojiStatuses
2626
from .get_me import GetMe
27+
from .get_user import GetUser
2728
from .get_users import GetUsers
2829
from .set_emoji_status import SetEmojiStatus
2930
from .set_personal_channel import SetPersonalChannel
@@ -43,6 +44,7 @@ class Users(
4344
SetPersonalChannel,
4445
SetProfilePhoto,
4546
DeleteProfilePhotos,
47+
GetUser,
4648
GetUsers,
4749
GetMe,
4850
SetUsername,

pyrogram/methods/users/get_user.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
import asyncio
20+
from typing import Union
21+
22+
import pyrogram
23+
from pyrogram import raw
24+
from pyrogram import types
25+
26+
27+
class GetUser:
28+
async def get_user(
29+
self: "pyrogram.Client",
30+
user_id: Union[int, str]
31+
) -> "types.User":
32+
"""Get information about a single user.
33+
34+
.. include:: /_includes/usable-by/users-bots.rst
35+
36+
Parameters:
37+
user_id (``int`` | ``str``):
38+
A single user identifier (id or username).
39+
For a contact that exists in your Telegram address book you can use his phone number (str).
40+
41+
Returns:
42+
:obj:`~pyrogram.types.User`: A single user is returned.
43+
44+
Example:
45+
.. code-block:: python
46+
47+
# Get information about one user
48+
await app.get_user("me")
49+
50+
# Get information by user ID
51+
await app.get_user(12345)
52+
53+
# Get information by username
54+
await app.get_user("@username")
55+
56+
Raises:
57+
ValueError: In case of invalid arguments or if the user does not exist.
58+
"""
59+
peer = await self.resolve_peer(user_id)
60+
61+
r = await self.invoke(
62+
raw.functions.users.GetUsers(
63+
id=[peer]
64+
)
65+
)
66+
67+
users = types.List()
68+
69+
for i in r:
70+
users.append(types.User._parse(self, i))
71+
72+
if users:
73+
return users[0]
74+
75+
msg = "User does not exist"
76+
raise ValueError(msg)

0 commit comments

Comments
 (0)