Skip to content

Commit 9a94c8f

Browse files
ColinSharkdelivrance
authored andcommitted
Add get_common_chats method (pyrogram#303)
* Add get_common_chats method * Return properly formatted list * Fix small format and docstring issues
1 parent d6bcfa2 commit 9a94c8f

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

pyrogram/client/methods/users/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from .block_user import BlockUser
2020
from .delete_profile_photos import DeleteProfilePhotos
21+
from .get_common_chats import GetCommonChats
2122
from .get_me import GetMe
2223
from .get_profile_photos import GetProfilePhotos
2324
from .get_profile_photos_count import GetProfilePhotosCount
@@ -30,6 +31,7 @@
3031

3132
class Users(
3233
BlockUser,
34+
GetCommonChats,
3335
GetProfilePhotos,
3436
SetProfilePhoto,
3537
DeleteProfilePhotos,
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2019 Dan Tès <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+
from typing import Union
20+
21+
import pyrogram
22+
from pyrogram.api import functions, types
23+
from ...ext import BaseClient
24+
25+
26+
class GetCommonChats(BaseClient):
27+
def get_common_chats(self, user_id: Union[int, str]) -> list:
28+
"""Get the common chats you have with a user.
29+
30+
Parameters:
31+
user_id (``int`` | ``str``):
32+
Unique identifier (int) or username (str) of the target chat.
33+
For your personal cloud (Saved Messages) you can simply use "me" or "self".
34+
For a contact that exists in your Telegram address book you can use his phone number (str).
35+
36+
Returns:
37+
List of :obj:`Chat`: On success, a list of the common chats is returned.
38+
39+
Raises:
40+
ValueError: If the user_id doesn't belong to a user.
41+
42+
Example:
43+
.. code-block:: python
44+
45+
common = app.get_common_chats("haskell")
46+
print(common)
47+
"""
48+
49+
peer = self.resolve_peer(user_id)
50+
51+
if isinstance(peer, types.InputPeerUser):
52+
r = self.send(
53+
functions.messages.GetCommonChats(
54+
user_id=peer,
55+
max_id=0,
56+
limit=100,
57+
)
58+
)
59+
60+
return pyrogram.List([pyrogram.Chat._parse_chat(self, x) for x in r.chats])
61+
62+
raise ValueError('The user_id "{}" doesn\'t belong to a user'.format(user_id))

0 commit comments

Comments
 (0)