Add add_contact, improve delete_contacts and import_contacts

This commit is contained in:
Dan 2021-03-20 10:13:40 +01:00
parent 3445507a9a
commit c971616808
5 changed files with 110 additions and 28 deletions

View File

@ -254,10 +254,11 @@ def pyrogram_api():
""", """,
contacts=""" contacts="""
Contacts Contacts
add_contacts add_contact
delete_contacts
import_contacts
get_contacts get_contacts
get_contacts_count get_contacts_count
delete_contacts
""", """,
password=""" password="""
Password Password

View File

@ -16,16 +16,18 @@
# You should have received a copy of the GNU Lesser General Public License # You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from .add_contacts import AddContacts from .add_contact import AddContact
from .delete_contacts import DeleteContacts from .delete_contacts import DeleteContacts
from .get_contacts import GetContacts from .get_contacts import GetContacts
from .get_contacts_count import GetContactsCount from .get_contacts_count import GetContactsCount
from .import_contacts import ImportContacts
class Contacts( class Contacts(
GetContacts, GetContacts,
DeleteContacts, DeleteContacts,
AddContacts, ImportContacts,
GetContactsCount GetContactsCount,
AddContact
): ):
pass pass

View File

@ -0,0 +1,73 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2021 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 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])

View File

@ -16,46 +16,52 @@
# You should have received a copy of the GNU Lesser General Public License # You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from typing import List from typing import List, Union
from pyrogram import raw from pyrogram import raw, types
from pyrogram.errors import PeerIdInvalid
from pyrogram.scaffold import Scaffold from pyrogram.scaffold import Scaffold
class DeleteContacts(Scaffold): class DeleteContacts(Scaffold):
async def delete_contacts( async def delete_contacts(
self, 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. """Delete contacts from your Telegram address book.
Parameters: Parameters:
ids (List of ``int``): user_ids (``int`` | ``str`` | List of ``int`` or ``str``):
A list of unique identifiers for the target users. A single user id/username o a list of user identifiers (id or username).
Can be an ID (int), a username (string) or phone number (string).
Returns: 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: Example:
.. code-block:: python .. code-block:: python
app.delete_contacts(user_id)
app.delete_contacts([user_id1, user_id2, user_id3]) app.delete_contacts([user_id1, user_id2, user_id3])
""" """
contacts = [] is_user_ids_list = isinstance(user_ids, list)
for i in ids: if not is_user_ids_list:
try: user_ids = [user_ids]
input_user = await self.resolve_peer(i)
except PeerIdInvalid:
continue
else:
if isinstance(input_user, raw.types.InputPeerUser):
contacts.append(input_user)
return await self.send( r = await self.send(
raw.functions.contacts.DeleteContacts( 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]

View File

@ -23,12 +23,12 @@ from pyrogram import types
from pyrogram.scaffold import Scaffold from pyrogram.scaffold import Scaffold
class AddContacts(Scaffold): class ImportContacts(Scaffold):
async def add_contacts( async def import_contacts(
self, self,
contacts: List["types.InputPhoneContact"] contacts: List["types.InputPhoneContact"]
): ):
"""Add contacts to your Telegram address book. """Import contacts to your Telegram address book.
Parameters: Parameters:
contacts (List of :obj:`~pyrogram.types.InputPhoneContact`): contacts (List of :obj:`~pyrogram.types.InputPhoneContact`):