mirror of
https://github.com/TeamPGM/pyrogram.git
synced 2024-11-16 20:59:29 +00:00
Merge pull request #23 from 1pyxa1/add_contact
Add add_contacts, delete_contacts, get_contacts
This commit is contained in:
commit
314d19e09d
@ -30,4 +30,5 @@ from .client import ChatAction
|
|||||||
from .client import Client
|
from .client import Client
|
||||||
from .client import ParseMode
|
from .client import ParseMode
|
||||||
from .client.input_media import InputMedia
|
from .client.input_media import InputMedia
|
||||||
|
from .client.input_phone_contact import InputPhoneContact
|
||||||
from .client import Emoji
|
from .client import Emoji
|
||||||
|
@ -135,6 +135,7 @@ class Client:
|
|||||||
|
|
||||||
self.peers_by_id = {}
|
self.peers_by_id = {}
|
||||||
self.peers_by_username = {}
|
self.peers_by_username = {}
|
||||||
|
self.peers_by_phone = {}
|
||||||
|
|
||||||
self.channels_pts = {}
|
self.channels_pts = {}
|
||||||
|
|
||||||
@ -183,6 +184,7 @@ class Client:
|
|||||||
|
|
||||||
self.rnd_id = MsgId
|
self.rnd_id = MsgId
|
||||||
self.get_dialogs()
|
self.get_dialogs()
|
||||||
|
self.get_contacts()
|
||||||
|
|
||||||
for i in range(self.UPDATES_WORKERS):
|
for i in range(self.UPDATES_WORKERS):
|
||||||
Thread(target=self.updates_worker, name="UpdatesWorker#{}".format(i + 1)).start()
|
Thread(target=self.updates_worker, name="UpdatesWorker#{}".format(i + 1)).start()
|
||||||
@ -224,6 +226,7 @@ class Client:
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
username = entity.username
|
username = entity.username
|
||||||
|
phone = entity.phone
|
||||||
|
|
||||||
input_peer = InputPeerUser(
|
input_peer = InputPeerUser(
|
||||||
user_id=user_id,
|
user_id=user_id,
|
||||||
@ -235,6 +238,9 @@ class Client:
|
|||||||
if username is not None:
|
if username is not None:
|
||||||
self.peers_by_username[username] = input_peer
|
self.peers_by_username[username] = input_peer
|
||||||
|
|
||||||
|
if phone is not None:
|
||||||
|
self.peers_by_phone[phone] = input_peer
|
||||||
|
|
||||||
if isinstance(entity, Chat):
|
if isinstance(entity, Chat):
|
||||||
chat_id = entity.id
|
chat_id = entity.id
|
||||||
|
|
||||||
@ -794,12 +800,20 @@ class Client:
|
|||||||
if peer_id in ("self", "me"):
|
if peer_id in ("self", "me"):
|
||||||
return InputPeerSelf()
|
return InputPeerSelf()
|
||||||
|
|
||||||
peer_id = peer_id.lower().strip("@")
|
peer_id = peer_id.lower().strip("@+")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return self.peers_by_username[peer_id]
|
int(peer_id)
|
||||||
except KeyError:
|
except ValueError:
|
||||||
return self.resolve_username(peer_id)
|
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 type(peer_id) is not int:
|
||||||
if isinstance(peer_id, types.PeerUser):
|
if isinstance(peer_id, types.PeerUser):
|
||||||
@ -2306,3 +2320,38 @@ class Client:
|
|||||||
self.download_queue.put((media, file_name, done))
|
self.download_queue.put((media, file_name, done))
|
||||||
|
|
||||||
done.wait()
|
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
pyrogram/client/input_phone_contact.py
Normal file
11
pyrogram/client/input_phone_contact.py
Normal file
@ -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
|
||||||
|
)
|
Loading…
Reference in New Issue
Block a user