Skip to content
This repository was archived by the owner on Dec 23, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyrogram/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@
from .client import Client
from .client import ParseMode
from .client.input_media import InputMedia
from .client.input_phone_contact import InputPhoneContact
from .client import Emoji
57 changes: 53 additions & 4 deletions pyrogram/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def __init__(self,

self.peers_by_id = {}
self.peers_by_username = {}
self.peers_by_phone = {}

self.channels_pts = {}

Expand Down Expand Up @@ -183,6 +184,7 @@ def start(self):

self.rnd_id = MsgId
self.get_dialogs()
self.get_contacts()

for i in range(self.UPDATES_WORKERS):
Thread(target=self.updates_worker, name="UpdatesWorker#{}".format(i + 1)).start()
Expand Down Expand Up @@ -224,6 +226,7 @@ def fetch_peers(self, entities: list):
continue

username = entity.username
phone = entity.phone

input_peer = InputPeerUser(
user_id=user_id,
Expand All @@ -235,6 +238,9 @@ def fetch_peers(self, entities: list):
if username is not None:
self.peers_by_username[username] = input_peer

if phone is not None:
self.peers_by_phone[phone] = input_peer

if isinstance(entity, Chat):
chat_id = entity.id

Expand Down Expand Up @@ -794,12 +800,20 @@ def resolve_peer(self, peer_id: int or str):
if peer_id in ("self", "me"):
return InputPeerSelf()

peer_id = peer_id.lower().strip("@")
peer_id = peer_id.lower().strip("@+")

try:
return self.peers_by_username[peer_id]
except KeyError:
return self.resolve_username(peer_id)
int(peer_id)
except ValueError:
try:
return self.peers_by_username[peer_id]
except KeyError:
return self.resolve_username(peer_id)
else:
try:
return self.peers_by_phone[peer_id]
except KeyError:
raise PeerIdInvalid

if type(peer_id) is not int:
if isinstance(peer_id, types.PeerUser):
Expand Down Expand Up @@ -2306,3 +2320,38 @@ def download_media(self, message: types.Message, file_name: str = None):
self.download_queue.put((media, file_name, done))

done.wait()

def add_contacts(self, contacts: list):
imported_contacts = self.send(
functions.contacts.ImportContacts(
contacts=contacts
)
)

self.fetch_peers(imported_contacts.users)

return imported_contacts

def delete_contacts(self, ids: list):
contacts = []

for i in ids:
try:
input_user = self.resolve_peer(i)
except PeerIdInvalid:
continue
else:
if isinstance(input_user, types.InputPeerUser):
contacts.append(input_user)

return self.send(
functions.contacts.DeleteContacts(
id=contacts
)
)

def get_contacts(self, _hash: int = 0):
contacts = self.send(functions.contacts.GetContacts(_hash))
self.fetch_peers(contacts.users)

return contacts
11 changes: 11 additions & 0 deletions pyrogram/client/input_phone_contact.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from pyrogram.api.types import InputPhoneContact as RawInputPhoneContact


class InputPhoneContact:
def __new__(cls, phone: str, first_name: str, last_name: str = ""):
return RawInputPhoneContact(
client_id=0,
phone="+" + phone.strip("+"),
first_name=first_name,
last_name=last_name
)