mirror of
https://github.com/TeamPGM/pyrogram.git
synced 2024-11-23 23:34:28 +00:00
Add add_contact, improve delete_contacts and import_contacts
This commit is contained in:
parent
3445507a9a
commit
c971616808
@ -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
|
||||
|
@ -16,16 +16,18 @@
|
||||
# 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 .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
|
||||
|
73
pyrogram/methods/contacts/add_contact.py
Normal file
73
pyrogram/methods/contacts/add_contact.py
Normal 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])
|
@ -16,46 +16,52 @@
|
||||
# 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 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]
|
||||
|
@ -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`):
|
Loading…
Reference in New Issue
Block a user