Favour get_input_entity instead get_entity

Lonami 2017-10-25 13:28:32 +02:00
parent 19a30eea3b
commit 7bea7a6ad1

@ -24,21 +24,23 @@ from telethon.tl.types import InputPeerUser
peer = InputPeerUser(user_id, user_hash) peer = InputPeerUser(user_id, user_hash)
``` ```
Or we call `.get_entity()`: Or we call `.get_input_entity()`:
```python ```python
peer = client.get_entity('someone') peer = client.get_input_entity('someone')
``` ```
The library will cast the [`User`](https://lonamiwebs.github.io/Telethon/types/user) that `.get_entity()` returns into the appropriated `InputPeer`. If performance is uttermost, you should do this conversion yourself: When you're going to invoke an API method, most require you to pass an `InputUser`, `InputChat`, or so on, this is why using `.get_input_entity()` is more straightforward (and sometimes immediate, if you know the ID of the user for instance). If you also need to have information about the whole user, use `.get_entity()` instead:
```python
entity = client.get_entity('someone')
```
In the later case, when you use the entity, the library will cast it to its "input" version for you. If you already have the complete user and want to cache its input version so the library doesn't have to do this every time its used, simply call `.get_input_peer`:
```python ```python
from telethon import utils from telethon import utils
user = client.get_entity('someone') peer = utils.get_input_user(entity)
peer = utils.get_input_user(user)
``` ```
Now you the library will avoid converting the user into the right type every time you need an `InputPeer`. After this small parenthesis about `.get_entity` versus `.get_input_entity`, we have everything we need. To `.invoke()` our request we do:
Finally, we have everything we need. To `.invoke()` our request we do:
```python ```python
result = client(SendMessageRequest(peer, 'Hello there!')) result = client(SendMessageRequest(peer, 'Hello there!'))
# __call__ is an alias for client.invoke(request). Both will work # __call__ is an alias for client.invoke(request). Both will work