Add search_contacts, set_account_ttl and get_account_ttl method

This commit is contained in:
KurimuzonAkuma 2024-08-04 15:31:31 +03:00
parent 151bbd7139
commit 438e9d2f0e
10 changed files with 283 additions and 1 deletions

View File

@ -314,6 +314,7 @@ def pyrogram_api():
import_contacts
get_contacts
get_contacts_count
search_contacts
""",
payments="""
Payments
@ -406,6 +407,11 @@ def pyrogram_api():
apply_boost
get_boosts
get_boosts_status
""",
account="""
Account
get_account_ttl
set_account_ttl
"""
)
@ -475,6 +481,7 @@ def pyrogram_api():
Folder
GroupCallMember
ChatColor
FoundContacts
""",
messages_media="""
Messages & Media

View File

@ -216,6 +216,19 @@ Payments
{payments}
Account
-------
.. autosummary::
:nosignatures:
{account}
.. toctree::
:hidden:
{account}
Advanced
--------

View File

@ -16,6 +16,7 @@
# 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 .account import Account
from .advanced import Advanced
from .auth import Auth
from .business import Business
@ -35,6 +36,7 @@ from .utilities import Utilities
class Methods(
Account,
Advanced,
Auth,
Business,

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -21,6 +21,7 @@ from .delete_contacts import DeleteContacts
from .get_contacts import GetContacts
from .get_contacts_count import GetContactsCount
from .import_contacts import ImportContacts
from .search_contacts import SearchContacts
class Contacts(
@ -28,6 +29,7 @@ class Contacts(
DeleteContacts,
ImportContacts,
GetContactsCount,
AddContact
AddContact,
SearchContacts
):
pass

View File

@ -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)

View File

@ -40,6 +40,7 @@ from .chat_reactions import ChatReactions
from .dialog import Dialog
from .emoji_status import EmojiStatus
from .folder import Folder
from .found_contacts import FoundContacts
from .group_call_member import GroupCallMember
from .invite_link_importer import InviteLinkImporter
from .restriction import Restriction
@ -82,6 +83,7 @@ __all__ = [
"ChatJoiner",
"EmojiStatus",
"Folder",
"FoundContacts",
"GroupCallMember",
"ChatReactions"
]

View File

@ -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.
global_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,
global_results: Optional["types.Chat"] = None
):
super().__init__(client)
self.my_results = my_results
self.global_results = global_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 = []
global_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)
global_results.append(types.Chat._parse_chat(client, peer))
return FoundContacts(
my_results=types.List(my_results) or None,
global_results=types.List(global_results) or None,
client=client
)