Skip to content

Commit 95051d7

Browse files
committed
Add get_nearby_chats method
1 parent 74ecd2b commit 95051d7

4 files changed

Lines changed: 82 additions & 2 deletions

File tree

compiler/docs/compiler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ def get_title_list(s: str) -> list:
202202
get_dialogs_count
203203
update_chat_username
204204
get_common_chats
205+
get_nearby_chats
205206
archive_chats
206207
unarchive_chats
207208
add_chat_members

pyrogram/client/methods/chats/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
from .unban_chat_member import UnbanChatMember
4848
from .unpin_chat_message import UnpinChatMessage
4949
from .update_chat_username import UpdateChatUsername
50+
from .get_nearby_chats import GetNearbyChats
5051

5152

5253
class Chats(
@@ -80,6 +81,7 @@ class Chats(
8081
CreateChannel,
8182
AddChatMembers,
8283
DeleteChannel,
83-
DeleteSupergroup
84+
DeleteSupergroup,
85+
GetNearbyChats
8486
):
8587
pass
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 List
20+
21+
import pyrogram
22+
from pyrogram.api import functions, types
23+
from ...ext import BaseClient, utils
24+
25+
26+
class GetNearbyChats(BaseClient):
27+
def get_nearby_chats(
28+
self,
29+
latitude: float,
30+
longitude: float
31+
) -> List["pyrogram.Chat"]:
32+
"""Get nearby chats.
33+
34+
Parameters:
35+
latitude (``float``):
36+
Latitude of the location.
37+
38+
longitude (``float``):
39+
Longitude of the location.
40+
41+
Returns:
42+
List of :obj:`Chat`: On success, a list of nearby chats is returned.
43+
44+
Example:
45+
.. code-block:: python
46+
47+
chats = app.get_nearby_chats(51.500729, -0.124583)
48+
print(chats)
49+
"""
50+
51+
r = self.send(
52+
functions.contacts.GetLocated(
53+
geo_point=types.InputGeoPoint(
54+
lat=latitude,
55+
long=longitude
56+
)
57+
)
58+
)
59+
60+
chats = pyrogram.List([pyrogram.Chat._parse_chat(self, chat) for chat in r.chats])
61+
peers = r.updates[0].peers
62+
63+
for peer in peers:
64+
chat_id = utils.get_channel_id(peer.peer.channel_id)
65+
66+
for chat in chats:
67+
if chat.id == chat_id:
68+
chat.distance = peer.distance
69+
break
70+
71+
return chats

pyrogram/client/types/user_and_chats/chat.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ class Chat(Object):
9393
9494
permissions (:obj:`ChatPermissions` *optional*):
9595
Default chat member permissions, for groups and supergroups.
96+
97+
distance (``int``, *optional*):
98+
Distance in meters of this group chat from your location.
99+
Returned only in :meth:`~Client.get_nearby_chats`.
96100
"""
97101

98102
def __init__(
@@ -117,7 +121,8 @@ def __init__(
117121
can_set_sticker_set: bool = None,
118122
members_count: int = None,
119123
restriction_reason: str = None,
120-
permissions: "pyrogram.ChatPermissions" = None
124+
permissions: "pyrogram.ChatPermissions" = None,
125+
distance: int = None
121126
):
122127
super().__init__(client)
123128

@@ -140,6 +145,7 @@ def __init__(
140145
self.members_count = members_count
141146
self.restriction_reason = restriction_reason
142147
self.permissions = permissions
148+
self.distance = distance
143149

144150
@staticmethod
145151
def _parse_user_chat(client, user: types.User) -> "Chat":

0 commit comments

Comments
 (0)