forked from pyrogram/pyrogram
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add search_contacts, set_account_ttl and get_account_ttl method
- Loading branch information
1 parent
151bbd7
commit 4dfdb81
Showing
10 changed files
with
283 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Pyrogram - Telegram MTProto API Client Library for Python | ||
# Copyright (C) 2017-present Dan <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/>. | ||
|
||
from .get_account_ttl import GetAccountTTL | ||
from .set_account_ttl import SetAccountTTL | ||
|
||
|
||
class Account( | ||
GetAccountTTL, | ||
SetAccountTTL | ||
): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Pyrogram - Telegram MTProto API Client Library for Python | ||
# Copyright (C) 2017-present Dan <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 pyrogram | ||
from pyrogram import raw | ||
|
||
|
||
class GetAccountTTL: | ||
async def get_account_ttl( | ||
self: "pyrogram.Client", | ||
): | ||
"""Get days to live of account. | ||
.. include:: /_includes/usable-by/users.rst | ||
Returns: | ||
``int``: Time to live in days of the current account. | ||
Example: | ||
.. code-block:: python | ||
# Get ttl in days | ||
await app.get_account_ttl() | ||
""" | ||
r = await self.invoke( | ||
raw.functions.account.GetAccountTTL() | ||
) | ||
|
||
return r.days |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Pyrogram - Telegram MTProto API Client Library for Python | ||
# Copyright (C) 2017-present Dan <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 pyrogram | ||
from pyrogram import raw | ||
|
||
|
||
class SetAccountTTL: | ||
async def set_account_ttl( | ||
self: "pyrogram.Client", | ||
days: int | ||
): | ||
"""Set days to live of account. | ||
.. include:: /_includes/usable-by/users.rst | ||
Parameters: | ||
days (``int``): | ||
Time to live in days. | ||
Returns: | ||
``bool``: On success, True is returned. | ||
Example: | ||
.. code-block:: python | ||
# Set ttl in days | ||
await app.set_account_ttl(365) | ||
""" | ||
r = await self.invoke( | ||
raw.functions.account.SetAccountTTL( | ||
ttl=raw.types.AccountDaysTTL(days=days) | ||
) | ||
) | ||
|
||
return r |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Pyrogram - Telegram MTProto API Client Library for Python | ||
# Copyright (C) 2017-present Dan <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 pyrogram | ||
from pyrogram import raw | ||
from pyrogram import types | ||
|
||
|
||
class SearchContacts: | ||
async def search_contacts( | ||
self: "pyrogram.Client", | ||
query: str, | ||
limit: int = 0 | ||
): | ||
"""Returns users or channels found by name substring and auxiliary data. | ||
.. include:: /_includes/usable-by/users.rst | ||
Parameters: | ||
query (``str``): | ||
Target substring. | ||
limit (``int``, *optional*): | ||
Maximum number of users to be returned. | ||
Returns: | ||
:obj:`~pyrogram.types.FoundContacts`: On success, a list of chats is returned. | ||
Example: | ||
.. code-block:: python | ||
await app.search_contacts("pyrogram") | ||
""" | ||
total = limit or (1 << 31) - 1 | ||
limit = min(100, total) | ||
|
||
r = await self.invoke( | ||
raw.functions.contacts.Search( | ||
q=query, | ||
limit=limit | ||
) | ||
) | ||
|
||
return types.FoundContacts._parse(self, r) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Pyrogram - Telegram MTProto API Client Library for Python | ||
# Copyright (C) 2017-present Dan <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/>. | ||
|
||
from typing import Optional | ||
|
||
import pyrogram | ||
from pyrogram import raw | ||
from pyrogram import utils | ||
from pyrogram import types | ||
from ..object import Object | ||
|
||
|
||
class FoundContacts(Object): | ||
"""Chats found by name substring and auxiliary data. | ||
Parameters: | ||
my_results (List of :obj:`~pyrogram.types.Chat`, *optional*): | ||
Personalized results. | ||
results (List of :obj:`~pyrogram.types.Chat`, *optional*): | ||
List of found chats in global search. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
*, | ||
client: "pyrogram.Client" = None, | ||
my_results: Optional["types.Chat"] = None, | ||
results: Optional["types.Chat"] = None | ||
): | ||
super().__init__(client) | ||
|
||
self.my_results = my_results | ||
self.results = results | ||
|
||
@staticmethod | ||
def _parse(client, found: "raw.types.contacts.Found") -> "FoundContacts": | ||
users = {u.id: u for u in found.users} | ||
chats = {c.id: c for c in found.chats} | ||
|
||
my_results = [] | ||
results = [] | ||
|
||
for result in found.my_results: | ||
peer_id = utils.get_raw_peer_id(result) | ||
peer = users.get(peer_id) or chats.get(peer_id) | ||
|
||
my_results.append(types.Chat._parse_chat(client, peer)) | ||
|
||
for result in found.results: | ||
peer_id = utils.get_raw_peer_id(result) | ||
peer = users.get(peer_id) or chats.get(peer_id) | ||
|
||
results.append(types.Chat._parse_chat(client, peer)) | ||
|
||
return FoundContacts( | ||
my_results=types.List(my_results) or None, | ||
results=types.List(results) or None, | ||
client=client | ||
) |