-
Notifications
You must be signed in to change notification settings - Fork 1.4k
add add_contacts, delete_contacts, get_contacts #23
Changes from 1 commit
7d72738
2ab8fbe
3ff3f5d
c99bc91
c33719b
c5281eb
5a44b93
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,14 +45,17 @@ | |
| from pyrogram.api.types import ( | ||
| User, Chat, Channel, | ||
| PeerUser, PeerChannel, | ||
| InputUser, | ||
| InputPeerEmpty, InputPeerSelf, | ||
| InputPeerUser, InputPeerChat, InputPeerChannel | ||
| InputPeerUser, InputPeerChat, InputPeerChannel, | ||
| InputPhoneContact | ||
| ) | ||
| from pyrogram.crypto import AES | ||
| from pyrogram.session import Auth, Session | ||
| from pyrogram.session.internals import MsgId | ||
| from .input_media import InputMedia | ||
| from .style import Markdown, HTML | ||
| from typing import List, Union | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
|
|
@@ -2306,3 +2309,100 @@ 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, | ||
| phone: Union[int, str] = None, | ||
| first_name: str = None, | ||
| last_name: str = None, | ||
| input_phone_contact_list: List[InputPhoneContact] = None): | ||
| if (phone is None or first_name is None) and \ | ||
| input_phone_contact_list is None: | ||
| log.warning("(phone and first_name) or input_phone_contact_list " | ||
| "must be not None") | ||
| return None | ||
|
|
||
| if phone is not None and first_name is not None: | ||
| if str(phone)[0] != '+': | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| phone = '+' + str(phone) | ||
| input_phone_contact_list = [] | ||
| input_phone_contact = InputPhoneContact(client_id=0, | ||
| phone=phone, | ||
| first_name=first_name, | ||
| last_name=last_name or '') | ||
| input_phone_contact_list.append(input_phone_contact) | ||
|
|
||
| # make sure that we send only InputPhoneContact | ||
| inner_input_phone_contact_list = [] | ||
| for contact in input_phone_contact_list: | ||
| if isinstance(contact, InputPhoneContact): | ||
| inner_input_phone_contact_list.append(contact) | ||
|
|
||
| imported_contacts = self.send( | ||
| functions.contacts.ImportContacts(inner_input_phone_contact_list)) | ||
|
|
||
| for user in imported_contacts.users: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This loop can be replaced by a call to |
||
| if isinstance(user, User): | ||
| if user.id in self.peers_by_id: | ||
| continue | ||
|
|
||
| if user.access_hash is None: | ||
| continue | ||
|
|
||
| input_peer = InputPeerUser( | ||
| user_id=user.id, | ||
| access_hash=user.access_hash | ||
| ) | ||
|
|
||
| self.peers_by_id[user.id] = input_peer | ||
|
|
||
| if user.username is not None: | ||
| self.peers_by_username[user.username] = input_peer | ||
|
|
||
| return imported_contacts | ||
|
|
||
| def delete_contacts(self, _id: int = None, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer to keep just one argument (the list), like the raw function does. To add/delete a single contact you simply pass a list with a single element, e.g. [123456789]
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. well, it's up to you of course
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's also about consistency, other methods like forward_messages, and delete_messages work like this |
||
| ids_list: Union[ | ||
| List[int], List[InputUser]] = None): | ||
| if _id is None and ids_list is None: | ||
| log.warning('id or ids_list must be not None') | ||
| return False | ||
|
|
||
| contacts = self.get_contacts() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no need to check whether the Contact we want to remove exists in the user's contacts list
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if you don't have a conversation with a contact you want to remove, you need to get
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if a call to resolve_peer returns nothing we simply ignore that
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so, in order to make it working for contacts, the lib must also fetch contacts when it starts and add users in the internal dictionaries, just like get_dialogs
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so, if you don't have a conversation with a contact you cannot remove it? or i didn't get it
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope,
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but if you restart the Client,
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i mean in that case if you haven't start a dialog with new contact
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, of course it won't add new contacts, because the code was missing, I've added it just now. Check new commits on this PR please
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, i've understood your idea. thanks! |
||
|
|
||
| if _id is not None: | ||
| if not isinstance(_id, int): | ||
| log.warning('id is not int') | ||
| return False | ||
|
|
||
| input_user = None | ||
| for user in contacts.users: | ||
| if isinstance(user, User): | ||
| if _id == user.id: | ||
| input_user = InputUser(user_id=user.id, | ||
| access_hash=user.access_hash) | ||
| break | ||
|
|
||
| ids_list = [input_user] | ||
|
|
||
| inner_ids_list = [] | ||
| for _id in ids_list: | ||
| if isinstance(_id, InputUser): | ||
| inner_ids_list.append(_id) | ||
|
|
||
| if isinstance(_id, int): | ||
| input_user = None | ||
| for user in contacts.users: | ||
| if isinstance(user, User): | ||
| if _id == user.id: | ||
| input_user = InputUser( | ||
| user_id=user.id, | ||
| access_hash=user.access_hash) | ||
| break | ||
| inner_ids_list.append(input_user) | ||
|
|
||
| res = self.send(functions.contacts.DeleteContacts(inner_ids_list)) | ||
| return res | ||
|
|
||
| def get_contacts(self, _hash: int = 0): | ||
| return self.send(functions.contacts.GetContacts(_hash)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another call to
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another call to |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately the
typingmodule is only available for Python 3.5+. We should remove this in order to support 3.4