Show how to get entities by MessageFwdHeader

Lonami 2017-06-09 17:01:33 +02:00
parent 41cb97a460
commit 534cbecfa8

@ -1,3 +1,4 @@
## Via ResolveUsernameRequest
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
@ -11,6 +12,20 @@ found_users = result.users
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)
```
## Via GetContactsRequest
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
@ -25,4 +40,6 @@ if isinstance(contacts, Contacts):
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).
## Via open dialogs
[There's a page for that](Retrieving-all-dialogs).