diff --git a/Retrieving-an-entity.md b/Retrieving-an-entity.md new file mode 100644 index 0000000..decbda7 --- /dev/null +++ b/Retrieving-an-entity.md @@ -0,0 +1,28 @@ +An "entity" is used to refer to either an [`User`](https://lonamiwebs.github.io/Telethon/types/user.html) or a [`Chat`](https://lonamiwebs.github.io/Telethon/types/chat.html) (which includes a [`Channel`](https://lonamiwebs.github.io/Telethon/constructors/channel.html)). Perhaps the most straightforward way to get these is by [resolving their username](https://lonamiwebs.github.io/Telethon/methods/contacts/resolve_username.html): + +```python +from telethon.tl.functions.contacts import ResolveUsernameRequest + +result = client.invoke(ResolveUsernameRequest('username')) +found_chats = result.chats +found_users = result.users +# result.peer may be a PeerUser, PeerChat or PeerChannel +``` + +See [`Peer`](https://lonamiwebs.github.io/Telethon/types/peer.html) for more information about this result. + +If the user you want to talk to is a contact, you can use [`GetContactsRequest`](https://lonamiwebs.github.io/Telethon/methods/contacts/get_contacts.html): + +```python +from telethon.tl.functions.contacts import GetContactsRequest +from telethon.tl.types.contacts import Contacts + +# `.sign_in()ยด returns your User, otherwise use: +self_user = client.get_me() +contacts = client.invoke(GetContactsRequest(self_user.access_hash)) +if isinstance(contacts, Contacts): + users = contacts.users + contacts = contacts.contacts +``` + +If, on the other hand, all you know is that your contact is somewhere in your chat list, you can try [retrieving all dialogs](Retrieving-all-dialogs). \ No newline at end of file