From 534cbecfa8f41836729a1d8c82dee1f20ffa97c7 Mon Sep 17 00:00:00 2001 From: Lonami Date: Fri, 9 Jun 2017 17:01:33 +0200 Subject: [PATCH] Show how to get entities by MessageFwdHeader --- Retrieving-an-entity.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/Retrieving-an-entity.md b/Retrieving-an-entity.md index decbda7..c936bf8 100644 --- a/Retrieving-an-entity.md +++ b/Retrieving-an-entity.md @@ -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). \ No newline at end of file +## Via open dialogs + +[There's a page for that](Retrieving-all-dialogs). \ No newline at end of file