mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-07-13 01:22:21 +03:00
Favour get_input_entity instead get_entity
parent
19a30eea3b
commit
7bea7a6ad1
|
@ -24,21 +24,23 @@ from telethon.tl.types import InputPeerUser
|
|||
peer = InputPeerUser(user_id, user_hash)
|
||||
```
|
||||
|
||||
Or we call `.get_entity()`:
|
||||
Or we call `.get_input_entity()`:
|
||||
```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
|
||||
from telethon import utils
|
||||
user = client.get_entity('someone')
|
||||
peer = utils.get_input_user(user)
|
||||
peer = utils.get_input_user(entity)
|
||||
```
|
||||
|
||||
Now you the library will avoid converting the user into the right type every time you need an `InputPeer`.
|
||||
|
||||
Finally, we have everything we need. To `.invoke()` our request we do:
|
||||
After this small parenthesis about `.get_entity` versus `.get_input_entity`, we have everything we need. To `.invoke()` our request we do:
|
||||
```python
|
||||
result = client(SendMessageRequest(peer, 'Hello there!'))
|
||||
# __call__ is an alias for client.invoke(request). Both will work
|
||||
|
|
Loading…
Reference in New Issue
Block a user