From 95051d7fb1946c7718e6b56e9154f29233bbc871 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 17 Aug 2019 22:23:34 +0200 Subject: [PATCH] Add get_nearby_chats method --- compiler/docs/compiler.py | 1 + pyrogram/client/methods/chats/__init__.py | 4 +- .../client/methods/chats/get_nearby_chats.py | 71 +++++++++++++++++++ pyrogram/client/types/user_and_chats/chat.py | 8 ++- 4 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 pyrogram/client/methods/chats/get_nearby_chats.py diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index ca49538f..212cf370 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -202,6 +202,7 @@ def pyrogram_api(): get_dialogs_count update_chat_username get_common_chats + get_nearby_chats archive_chats unarchive_chats add_chat_members diff --git a/pyrogram/client/methods/chats/__init__.py b/pyrogram/client/methods/chats/__init__.py index fddb48ce..13829029 100644 --- a/pyrogram/client/methods/chats/__init__.py +++ b/pyrogram/client/methods/chats/__init__.py @@ -47,6 +47,7 @@ from .unarchive_chats import UnarchiveChats from .unban_chat_member import UnbanChatMember from .unpin_chat_message import UnpinChatMessage from .update_chat_username import UpdateChatUsername +from .get_nearby_chats import GetNearbyChats class Chats( @@ -80,6 +81,7 @@ class Chats( CreateChannel, AddChatMembers, DeleteChannel, - DeleteSupergroup + DeleteSupergroup, + GetNearbyChats ): pass diff --git a/pyrogram/client/methods/chats/get_nearby_chats.py b/pyrogram/client/methods/chats/get_nearby_chats.py new file mode 100644 index 00000000..7f9e3614 --- /dev/null +++ b/pyrogram/client/methods/chats/get_nearby_chats.py @@ -0,0 +1,71 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2019 Dan Tès +# +# 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 . + +from typing import List + +import pyrogram +from pyrogram.api import functions, types +from ...ext import BaseClient, utils + + +class GetNearbyChats(BaseClient): + def get_nearby_chats( + self, + latitude: float, + longitude: float + ) -> List["pyrogram.Chat"]: + """Get nearby chats. + + Parameters: + latitude (``float``): + Latitude of the location. + + longitude (``float``): + Longitude of the location. + + Returns: + List of :obj:`Chat`: On success, a list of nearby chats is returned. + + Example: + .. code-block:: python + + chats = app.get_nearby_chats(51.500729, -0.124583) + print(chats) + """ + + r = self.send( + functions.contacts.GetLocated( + geo_point=types.InputGeoPoint( + lat=latitude, + long=longitude + ) + ) + ) + + chats = pyrogram.List([pyrogram.Chat._parse_chat(self, chat) for chat in r.chats]) + peers = r.updates[0].peers + + for peer in peers: + chat_id = utils.get_channel_id(peer.peer.channel_id) + + for chat in chats: + if chat.id == chat_id: + chat.distance = peer.distance + break + + return chats diff --git a/pyrogram/client/types/user_and_chats/chat.py b/pyrogram/client/types/user_and_chats/chat.py index f6e938df..64507cb5 100644 --- a/pyrogram/client/types/user_and_chats/chat.py +++ b/pyrogram/client/types/user_and_chats/chat.py @@ -93,6 +93,10 @@ class Chat(Object): permissions (:obj:`ChatPermissions` *optional*): Default chat member permissions, for groups and supergroups. + + distance (``int``, *optional*): + Distance in meters of this group chat from your location. + Returned only in :meth:`~Client.get_nearby_chats`. """ def __init__( @@ -117,7 +121,8 @@ class Chat(Object): can_set_sticker_set: bool = None, members_count: int = None, restriction_reason: str = None, - permissions: "pyrogram.ChatPermissions" = None + permissions: "pyrogram.ChatPermissions" = None, + distance: int = None ): super().__init__(client) @@ -140,6 +145,7 @@ class Chat(Object): self.members_count = members_count self.restriction_reason = restriction_reason self.permissions = permissions + self.distance = distance @staticmethod def _parse_user_chat(client, user: types.User) -> "Chat":