From c971616808751c51d81c6e8540e10b49065a7507 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 20 Mar 2021 10:13:40 +0100 Subject: [PATCH] Add add_contact, improve delete_contacts and import_contacts --- compiler/docs/compiler.py | 5 +- pyrogram/methods/contacts/__init__.py | 8 +- pyrogram/methods/contacts/add_contact.py | 73 +++++++++++++++++++ pyrogram/methods/contacts/delete_contacts.py | 46 +++++++----- .../{add_contacts.py => import_contacts.py} | 6 +- 5 files changed, 110 insertions(+), 28 deletions(-) create mode 100644 pyrogram/methods/contacts/add_contact.py rename pyrogram/methods/contacts/{add_contacts.py => import_contacts.py} (93%) diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index fc8734eb..3c32e851 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -254,10 +254,11 @@ def pyrogram_api(): """, contacts=""" Contacts - add_contacts + add_contact + delete_contacts + import_contacts get_contacts get_contacts_count - delete_contacts """, password=""" Password diff --git a/pyrogram/methods/contacts/__init__.py b/pyrogram/methods/contacts/__init__.py index 3af26f32..b542b665 100644 --- a/pyrogram/methods/contacts/__init__.py +++ b/pyrogram/methods/contacts/__init__.py @@ -16,16 +16,18 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from .add_contacts import AddContacts +from .add_contact import AddContact from .delete_contacts import DeleteContacts from .get_contacts import GetContacts from .get_contacts_count import GetContactsCount +from .import_contacts import ImportContacts class Contacts( GetContacts, DeleteContacts, - AddContacts, - GetContactsCount + ImportContacts, + GetContactsCount, + AddContact ): pass diff --git a/pyrogram/methods/contacts/add_contact.py b/pyrogram/methods/contacts/add_contact.py new file mode 100644 index 00000000..5d00253b --- /dev/null +++ b/pyrogram/methods/contacts/add_contact.py @@ -0,0 +1,73 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2021 Dan +# +# 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 Union + +from pyrogram import raw +from pyrogram import types +from pyrogram.scaffold import Scaffold + + +class AddContact(Scaffold): + async def add_contact( + self, + user_id: Union[int, str], + first_name: str, + last_name: str = "", + phone_number: str = "", + share_phone_number: bool = False + ): + """Add an existing Telegram user as contact, even without a phone number. + + Parameters: + user_id (``int`` | ``str``): + Unique identifier (int) or username (str) of the target user. + + first_name (``str``, *optional*): + User's first name. + + last_name (``str``, *optional*): + User's last name. + + phone_number (``str``, *optional*): + User's phone number. + + share_phone_number (``bool``, *optional*): + Whether or not to share the phone number with the user. + Defaults to False. + + Returns: + :obj:`~pyrogram.types.User`: On success the user is returned. + + Example: + .. code-block:: python + + app.add_contact(12345678, "Foo") + app.add_contact("username", "Bar") + """ + r = await self.send( + raw.functions.contacts.AddContact( + id=await self.resolve_peer(user_id), + first_name=first_name, + last_name=last_name, + phone=phone_number, + add_phone_privacy_exception=share_phone_number + ) + ) + + return types.User._parse(self, r.users[0]) diff --git a/pyrogram/methods/contacts/delete_contacts.py b/pyrogram/methods/contacts/delete_contacts.py index 9612c12f..6b73fa74 100644 --- a/pyrogram/methods/contacts/delete_contacts.py +++ b/pyrogram/methods/contacts/delete_contacts.py @@ -16,46 +16,52 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from typing import List +from typing import List, Union -from pyrogram import raw -from pyrogram.errors import PeerIdInvalid +from pyrogram import raw, types from pyrogram.scaffold import Scaffold class DeleteContacts(Scaffold): async def delete_contacts( self, - ids: List[int] - ): + user_ids: Union[int, str, List[Union[int, str]]] + ) -> Union["types.User", List["types.User"], None]: """Delete contacts from your Telegram address book. Parameters: - ids (List of ``int``): - A list of unique identifiers for the target users. - Can be an ID (int), a username (string) or phone number (string). + user_ids (``int`` | ``str`` | List of ``int`` or ``str``): + A single user id/username o a list of user identifiers (id or username). Returns: - ``bool``: True on success. + :obj:`~pyrogram.types.User` | List of :obj:`~pyrogram.types.User` | ``None``: In case *user_ids* was an + integer or a string, a single User object is returned. In case *user_ids* was a list, a list of User objects + is returned. In case nothing changed after calling the method (for example, when deleting a non-existent + contact), None is returned. Example: .. code-block:: python + app.delete_contacts(user_id) app.delete_contacts([user_id1, user_id2, user_id3]) """ - contacts = [] + is_user_ids_list = isinstance(user_ids, list) - for i in ids: - try: - input_user = await self.resolve_peer(i) - except PeerIdInvalid: - continue - else: - if isinstance(input_user, raw.types.InputPeerUser): - contacts.append(input_user) + if not is_user_ids_list: + user_ids = [user_ids] - return await self.send( + r = await self.send( raw.functions.contacts.DeleteContacts( - id=contacts + id=[await self.resolve_peer(i) for i in user_ids] ) ) + + if not r.updates: + return None + + users = types.List([types.User._parse(self, i) for i in r.users]) + + if is_user_ids_list: + return users + else: + return users[0] diff --git a/pyrogram/methods/contacts/add_contacts.py b/pyrogram/methods/contacts/import_contacts.py similarity index 93% rename from pyrogram/methods/contacts/add_contacts.py rename to pyrogram/methods/contacts/import_contacts.py index 3e41f615..92410fc1 100644 --- a/pyrogram/methods/contacts/add_contacts.py +++ b/pyrogram/methods/contacts/import_contacts.py @@ -23,12 +23,12 @@ from pyrogram import types from pyrogram.scaffold import Scaffold -class AddContacts(Scaffold): - async def add_contacts( +class ImportContacts(Scaffold): + async def import_contacts( self, contacts: List["types.InputPhoneContact"] ): - """Add contacts to your Telegram address book. + """Import contacts to your Telegram address book. Parameters: contacts (List of :obj:`~pyrogram.types.InputPhoneContact`):