Telethon API
This documentation was generated straight from the scheme.tl
provided by Telegram. However, there is no official documentation per se
on what the methods, constructors and types mean. Nevertheless, this
page aims to provide easy access to all the available methods, their
definition and parameters.
Although this documentation was generated for Telethon, it may be useful for any other Telegram library out there.
Index
Methods
Currently there are {method_count} methods available for the layer
{layer}. The complete list can be seen here.
Methods, also known as requests, are used to interact with
the Telegram API itself and are invoked with a call to .invoke()
.
Only these can be passed to .invoke()
! You cannot
.invoke()
types or constructors, only requests. After this,
Telegram will return a result
, which may be, for instance,
a bunch of messages, some dialogs, users, etc.
Types
Currently there are {type_count} types. You can see the full list here.
The Telegram types are the abstract results that you receive
after invoking a request. They are "abstract" because they can have
multiple constructors. For instance, the abstract type User
can be either UserEmpty
or User
. You should,
most of the time, make sure you received the desired type by using
the isinstance(result, Constructor)
Python function.
When a request needs a Telegram type as argument, you should create
an instance of it by using one of its, possibly multiple, constructors.
Constructors
Currently there are {constructor_count} constructors. You can see the full list here.
Constructors are the way you can create instances of the abstract types described above, and also the instances which are actually returned from the functions although they all share a common abstract type.
Core types
Core types are types from which the rest of Telegram types build upon:
- int:
The value should be an integer type, like 42.
It should have 32 bits or less. You can check the bit length by
calling
a.bit_length()
, wherea
is an integer variable. - long: Different name for an integer type. The numbers given should have 64 bits or less.
- int128: Another integer type, should have 128 bits or less.
- int256: The largest integer type, allowing 256 bits or less.
- double: The value should be a floating point value, such as 123.456.
- Vector<T>:
If a type
T
is wrapped aroundVector<T>
, then it means that the argument should be a list of it. For instance, a valid value forVector<int>
would be[1, 2, 3]
. - string: A valid UTF-8 string should be supplied. This is right how Python strings work, no further encoding is required.
- Bool:
Either
True
orFalse
. - true:
These arguments aren't actually sent but rather encoded as flags.
Any truthy value (
True
,7
) will enable this flag, although it's recommended to useTrue
orNone
to symbolize that it's not present. - bytes:
A sequence of bytes, like
b'hello'
, should be supplied. - date:
Although this type is internally used as an
int
, you can pass adatetime
object instead to work with date parameters.
Full example
The following example demonstrates:
- How to create a
TelegramClient
. - Connecting to the Telegram servers and authorizing an user.
- Retrieving a list of chats (dialogs).
- Invoking a request without the built-in methods.
#!/usr/bin/python3 from telethon import TelegramClient from telethon.tl.functions.messages import GetHistoryRequest from telethon.utils import get_input_peer # (1) Use your own values here api_id = 12345 api_hash = '0123456789abcdef0123456789abcdef' phone = '+34600000000' # (2) Create the client and connect client = TelegramClient('username', api_id, api_hash) client.connect() # Ensure you're authorized if not client.is_user_authorized(): client.send_code_request(phone) client.sign_in(phone, input('Enter the code: ')) # (3) Using built-in methods dialogs, entities = client.get_dialogs(10) entity = entities[0] # (4) !! Invoking a request manually !! result = client.invoke( GetHistoryRequest( get_input_peer(entity), limit=20, offset_date=None, offset_id=0, max_id=0, min_id=0, add_offset=0)) # Now you have access to the first 20 messages messages = result.messages
As it can be seen, manually invoking requests with
client.invoke()
is way more verbose than using the built-in
methods (such as client.get_dialogs()
. However, and given
that there are so many methods available, it's impossible to provide a nice
interface to things that may change over time. To get full access, however,
you're still able to invoke these methods manually.