From 7c572394393d0cb43702e4dfd1206d8219d28b34 Mon Sep 17 00:00:00 2001 From: Lonami Date: Mon, 25 Jun 2018 09:29:06 +0200 Subject: [PATCH] Destroyed Retrieving an entity (markdown) --- Retrieving-an-entity.md | 70 ----------------------------------------- 1 file changed, 70 deletions(-) delete mode 100644 Retrieving-an-entity.md diff --git a/Retrieving-an-entity.md b/Retrieving-an-entity.md deleted file mode 100644 index 2871bc6..0000000 --- a/Retrieving-an-entity.md +++ /dev/null @@ -1,70 +0,0 @@ -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)). The most straightforward way to get an entity is to use `TelegramClient.get_entity()`. This method accepts either a string, which can be a username, phone number or [t.me](https://t.me)-like link, or an integer that will be the ID of an **user**. You can use it like so: -```python -# all of these work -lonami = client.get_entity('lonami') -lonami = client.get_entity('t.me/lonami') -lonami = client.get_entity('https://telegram.dog/lonami') - -# other kind of entities -channel = client.get_entity('telegram.me/joinchat/AAAAAEkk2WdoDrB4-Q8-gg') -contact = client.get_entity('+34xxxxxxxxx') -friend = client.get_entity(friend_id) -``` - -For the last one to work, the library must have "seen" the user at least once. The library will "see" the user as long as any request contains them, so if you've called `.get_dialogs()` for instance, and your friend was there, the library will know about them. For more, read about the [Session Files](Session-Files). - -If you want to get a channel or chat by ID, you need to specify that they are a channel or a chat. The library can't infer what they are by just their ID (unless the ID is marked, but this is only done internally), so you need to wrap the ID around a [`Peer`](https://lonamiwebs.github.io/Telethon/types/peer.html) object: -```python -from telethon.tl.types import PeerUser, PeerChat, PeerChannel -my_user = client.get_entity(PeerUser(some_id)) -my_chat = client.get_entity(PeerChat(some_id)) -my_channel = client.get_entity(PeerChannel(some_id)) -``` - -**Note** that most requests don't ask for an `User`, or a `Chat`, but rather for `InputUser`, `InputChat`, and so on. If this is the case, you should prefer `.get_input_entity()` over `.get_entity()`, as it will be immediate if you provide an ID (whereas `.get_entity()` may need to find who the entity is first). - -## Via your open "chats" (dialogs) - -Simply call `client.get_dialogs()`. If you pass `limit=None`, all dialogs will be retrieved. These contain an `.entity` attribute. Use `help(result)` for more. - -## Via ResolveUsernameRequest -This is the request used by `.get_entity` internally, but you can also use it by hand: - -```python -from telethon.tl.functions.contacts import ResolveUsernameRequest - -result = client(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. - -## Via MessageFwdHeader -If all you have is a [`MessageFwdHeader`](https://lonamiwebs.github.io/Telethon/constructors/message_fwd_header.html) after you retrieved a bunch of messages, this gives you access to the `from_id` (if forwarded from an user) and `channel_id` (if forwarded from a channel). Invoking [`GetMessagesRequest`](https://lonamiwebs.github.io/Telethon/methods/messages/get_messages.html) also returns a list of `chats` and `users`, and you can find the desired entity there: - -```python -# Logic to retrieve messages with `GetMessagesRequest´ -messages = foo() -fwd_header = bar() - -user = next(u for u in messages.users if u.id == fwd_header.from_id) -channel = next(c for c in messages.chats if c.id == fwd_header.channel_id) -``` - -Or you can just call `.get_entity()` with the ID, as you should have seen that user or channel before. A call to `GetMessagesRequest` may still be neeed. - -## Via GetContactsRequest - -The library will call this for you if you pass a phone number to `.get_entity`, but again, it can be done manually. 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 - -contacts = client(GetContactsRequest(0)) -if isinstance(contacts, Contacts): - users = contacts.users - contacts = contacts.contacts -``` \ No newline at end of file