Telethon
Telethon
Telethon
Release 1.26.0
Lonami
1 What is this? 3
i
2.34 TelegramClient . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
2.35 Update Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
2.36 Custom package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
2.37 Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
2.38 API Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
2.39 Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
2.40 Connection Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
2.41 Helpers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Index 267
ii
Telethon Documentation, Release 1.26.0
@client.on(events.NewMessage(pattern='(?i).*Hello'))
async def handler(event):
await event.reply('Hey!')
client.run_until_disconnected()
First Steps 1
Telethon Documentation, Release 1.26.0
2 First Steps
CHAPTER 1
What is this?
Telegram is a popular messaging application. This library is meant to make it easy for you to write Python programs
that can interact with Telegram. Think of it as a wrapper that has already done the heavy job for you, so you can focus
on developing an application.
3
Telethon Documentation, Release 1.26.0
If you are getting started with the library, you should follow the documentation in order by pressing the “Next” button
at the bottom-right of every page.
You can also use the menu on the left to quickly skip over sections.
2.1 Installation
Telethon is a Python library, which means you need to download and install Python from https://www.python.org/
downloads/ if you haven’t already. Once you have Python installed, upgrade pip and run:
If you want the latest unreleased changes, you can run the following command instead:
Note: The development version may have bugs and is not recommended for production use. However, when you are
reporting a library bug, you should try if the bug still occurs in this version.
2.1.2 Verification
To verify that the library is installed correctly, run the following command:
5
Telethon Documentation, Release 1.26.0
If cryptg is installed, the library will work a lot faster, since encryption and decryption will be made in C instead
of Python. If your code deals with a lot of updates or you are downloading/uploading a lot of files, you will notice
a considerable speed-up (from a hundred kilobytes per second to several megabytes per second, if your connection
allows it). If it’s not installed, pyaes will be used (which is pure Python, so it’s much slower).
If pillow is installed, large images will be automatically resized when sending photos to prevent Telegram from failing
with “invalid image”. Official clients also do this.
If aiohttp is installed, the library will be able to download WebDocument media files (otherwise you will get an error).
If hachoir is installed, it will be used to extract metadata from files when sending documents. Telegram uses this
information to show the song’s performer, artist, title, duration, and for videos too (including size). Otherwise, they
will default to empty values, and you can set the attributes manually.
Note: Some of the modules may require additional dependencies before being installed through pip. If you have an
apt-based system, consider installing the most commonly missing dependencies (with the right pip):
apt update
apt install clang lib{jpeg-turbo,webp}-dev python{,-dev} zlib-dev
pip install -U --user setuptools
pip install -U --user telethon cryptg pillow
2.2 Signing In
Before working with Telegram’s API, you need to get your own API ID and hash:
1. Login to your Telegram account with the phone number of the developer account to use.
2. Click under API Development tools.
3. A Create new application window will appear. Fill in your application details. There is no need to enter any
URL, and only the first two fields (App title and Short name) can currently be changed later.
4. Click on Create application at the end. Remember that your API hash is secret and Telegram won’t let you
revoke it. Don’t post it anywhere!
Note: This API ID and hash is the one used by your application, not your phone number. You can use this API ID
and hash with any phone number or even for bot accounts.
We will write our code inside hello.py, so you can use any text editor that you like. To run the code, use python3
hello.py from the terminal.
Important: Don’t call your script telethon.py! Python will try to import the client from there and it will fail
with an error such as “ImportError: cannot import name ‘TelegramClient’ . . . ”.
2.2.2 Signing In
# The first parameter is the .session file name (absolute paths allowed)
with TelegramClient('anon', api_id, api_hash) as client:
client.loop.run_until_complete(client.send_message('me', 'Hello, myself!'))
In the first line, we import the class name so we can create an instance of the client. Then, we define variables to store
our API ID and hash conveniently.
At last, we create a new TelegramClient instance and call it client. We can now use the client variable for
anything that we want, such as sending a message to ourselves.
Note: Since Telethon is an asynchronous library, you need to await coroutine functions to have them run (or
otherwise, run the loop until they are complete). In this tiny example, we don’t bother making an async def
main().
See Mastering asyncio to find out more.
Using a with block is the preferred way to use the library. It will automatically start() the client, logging or
signing up if necessary.
If the .session file already existed, it will not login again, so be aware of this if you move or rename the file!
You can also use Telethon for your bots (normal bot accounts, not users). You will still need an API ID and hash, but
the process is very similar:
api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef'
bot_token = '12345:0123456789abcdef0123456789abcdef'
2.2. Signing In 7
Telethon Documentation, Release 1.26.0
If you need to use a proxy to access Telegram, you will need to either:
• For Python >= 3.6 : install python-socks[asyncio]
• For Python <= 3.5 : install PySocks
and then change
with
(of course, replacing the protocol, IP and port with the protocol, IP and port of the proxy).
The proxy= argument should be a dict (or tuple, for backwards compatibility), consisting of parameters described in
PySocks usage.
The allowed values for the argument proxy_type are:
• For Python <= 3.5:
– socks.SOCKS5 or 'socks5'
– socks.SOCKS4 or 'socks4'
– socks.HTTP or 'http'
• For Python >= 3.6:
– All of the above
– python_socks.ProxyType.SOCKS5
– python_socks.ProxyType.SOCKS4
– python_socks.ProxyType.HTTP
Example:
proxy = {
'proxy_type': 'socks5', # (mandatory) protocol to use (see above)
'addr': '1.1.1.1', # (mandatory) proxy IP address
'port': 5555, # (mandatory) proxy port number
'username': 'foo', # (optional) username if the proxy requires auth
'password': 'bar', # (optional) password if the proxy requires auth
'rdns': True # (optional) whether to use remote or local resolve,
˓→default remote
For backwards compatibility with PySocks the following format is possible (but discouraged):
MTProto Proxies are Telegram’s alternative to normal proxies, and work a bit differently. The following protocols are
available:
• ConnectionTcpMTProxyAbridged
• ConnectionTcpMTProxyIntermediate
• ConnectionTcpMTProxyRandomizedIntermediate (preferred)
For now, you need to manually specify these special connection modes if you want to use a MTProto Proxy. Your
code would look like this:
client = TelegramClient(
'anon',
api_id,
api_hash,
In future updates, we may make it easier to use MTProto Proxies (such as avoiding the need to manually pass
connection=).
In short, the same code above but without comments to make it clearer:
client = TelegramClient(
'anon', api_id, api_hash,
connection=connection.ConnectionTcpMTProxyRandomizedIntermediate,
proxy=('mtproxy.example.com', 2002, 'secret')
)
2.3 Quick-Start
Let’s see a longer example to learn some of the methods that the library has to offer. These are known as “friendly
methods”, and you should always use these if possible.
2.3. Quick-Start 9
Telethon Documentation, Release 1.26.0
# You can print all the dialogs/conversations that you are part of:
async for dialog in client.iter_dialogs():
print(dialog.name, 'has ID', dialog.id)
# Sending a message returns the sent message object, which you can use
print(message.raw_text)
with client:
client.loop.run_until_complete(main())
Here, we show how to sign in, get information about yourself, send messages, files, getting chats, printing messages,
and downloading files.
You should make sure that you understand what the code shown here does, take note on how methods are called and
used and so on before proceeding. We will see all the available methods later on.
Important: Note that Telethon is an asynchronous library, and as such, you should get used to it and learn a bit of
basic asyncio. This will help a lot. As a quick start, this means you generally want to write all your code inside
some async def like so:
client = ...
with client:
client.loop.run_until_complete(main())
After you understand this, you may use the telethon.sync hack if you want do so (see Compatibility and Conve-
nience), but note you may run into other issues (iPython, Anaconda, etc. have some issues with it).
2.4 Updates
Updates are an important topic in a messaging platform like Telegram. After all, you want to be notified when a new
message arrives, when a member joins, when someone starts typing, etc. For that, you can use events.
Important: It is strongly advised to enable logging when working with events, since exceptions in event handlers are
hidden by default. Please add the following snippet to the very top of your file:
import logging
logging.basicConfig(format='[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s',
level=logging.WARNING)
2.4. Updates 11
Telethon Documentation, Release 1.26.0
@client.on(events.NewMessage)
async def my_event_handler(event):
if 'hello' in event.raw_text:
await event.reply('hi!')
client.start()
client.run_until_disconnected()
This code isn’t much, but there might be some things unclear. Let’s break it down:
This is normal creation (of course, pass session name, API ID and hash). Nothing we don’t know already.
@client.on(events.NewMessage)
This Python decorator will attach itself to the my_event_handler definition, and basically means that on a
NewMessage event, the callback function you’re about to define will be called:
If a NewMessage event occurs, and 'hello' is in the text of the message, we reply() to the event with a 'hi!'
message.
Note: Event handlers must be async def. After all, Telethon is an asynchronous library based on asyncio,
which is a safer and often faster approach to threads.
You must await all method calls that use network requests, which is most of them.
@client.on(events.NewMessage(outgoing=True, pattern=r'\.save'))
async def handler(event):
if event.is_reply:
replied = await event.get_reply_message()
sender = replied.sender
await client.download_profile_photo(sender)
await event.respond('Saved your photo {}'.format(sender.username))
We could also get replies. This event filters outgoing messages (only those that we send will trigger the method), then
we filter by the regex r'\.save', which will match messages starting with ".save".
Inside the method, we check whether the event is replying to another message or not. If it is, we get the reply message
and the sender of that message, and download their profile photo.
Let’s delete messages which contain “heck”. We don’t allow swearing here.
@client.on(events.NewMessage(pattern=r'(?i).*heck'))
async def handler(event):
await event.delete()
With the r'(?i).*heck' regex, we match case-insensitive “heck” anywhere in the message. Regex is very power-
ful and you can learn more at https://regexone.com/.
So far, we have only seen the NewMessage, but there are many more which will be covered later. This is only a small
introduction to updates.
2.4.3 Entities
When you need the user or chat where an event occurred, you must use the following methods:
Events are like messages, but don’t have all the information a message has! When you manually get a message, it will
have all the information it needs. When you receive an update about a message, it won’t have all the information, so
you have to use the methods, not the properties.
Make sure you understand the code seen here before continuing! As a rule of thumb, remember that new message
events behave just like message objects, so you can do with them everything you can do with a message object.
These basic first steps should have gotten you started with the library.
By now, you should know how to call friendly methods and how to work with the returned objects, how things work
inside event handlers, etc.
Next, we will see a quick reference summary of all the methods and properties that you will need when using the
library. If you follow the links there, you will expand the documentation for the method and property, with more
examples on how to use them.
Therefore, you can find an example on every method of the client to learn how to use it, as well as a description of
all the arguments.
After that, we will go in-depth with some other important concepts that are worth learning and understanding.
From now on, you can keep pressing the “Next” button if you want, or use the menu on the left, since some pages are
quite lengthy.
If you’re using the library to make an actual application (and not just automate things), you should make sure to
comply with the ToS:
[. . . ] when logging in as an existing user, apps are supposed to call [GetTermsOfServiceUpdate] to
check for any updates to the Terms of Service; this call should be repeated after expires seconds have
elapsed. If an update to the Terms Of Service is available, clients are supposed to show a consent popup;
if accepted, clients should call [AcceptTermsOfService], providing the termsOfService id JSON
object; in case of denial, clients are to delete the account using [DeleteAccount], providing Decline ToS
update as deletion reason.
However, if you use the library to automate or enhance your Telegram experience, it’s very likely that you are using
other applications doing this check for you (so you wouldn’t run the risk of violating the ToS).
The library itself will not automatically perform this check or accept the ToS because it should require user action (the
only exception is during sign-up).
2.6 FAQ
Let’s start the quick references section with some useful tips to keep in mind, with the hope that you will understand
why certain things work the way that they do.
Contents
• FAQ
– Code without errors doesn’t work
– How can I except FloodWaitError?
– My account was deleted/limited when using the library
– How can I use a proxy?
– How do I access a field?
– AttributeError: ‘coroutine’ object has no attribute ‘id’
– sqlite3.OperationalError: database is locked
– event.chat or event.sender is None
– What does “bases ChatGetter” mean?
– Can I use Flask with the library?
– Can I use Anaconda/Spyder/IPython with the library?
Then it probably has errors, but you haven’t enabled logging yet. To enable logging, at the following code to the top
of your main file:
import logging
logging.basicConfig(format='[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s',
level=logging.WARNING)
You can change the logging level to be something different, from less to more information:
try:
await client.send_message(chat, 'Hi')
except errors.FloodWaitError as e:
# e.seconds is how many seconds you have
# to wait before making the request again.
print('Flood for', e.seconds)
The library will only do things that you tell it to do. If you use the library with bad intentions, Telegram will hopefully
ban you.
However, you may also be part of a limited country, such as Iran or Russia. In that case, we have bad news for you.
Telegram is much more likely to ban these numbers, as they are often used to spam other accounts, likely through the
use of libraries like this one. The best advice we can give you is to not abuse the API, like calling many requests really
quickly, and to sign up with these phones through an official application.
We have also had reports from Kazakhstan and China, where connecting would fail. To solve these connection prob-
lems, you should use a proxy.
Telegram may also ban virtual (VoIP) phone numbers, as again, they’re likely to be used for spam.
If you want to check if your account has been limited, simply send a private message to @SpamBot through Telegram
itself. You should notice this by getting errors like PeerFloodError, which means you’re limited, for instance,
when sending a message to some accounts but not others.
For more discussion, please see issue 297.
2.6. FAQ 15
Telethon Documentation, Release 1.26.0
This is basic Python knowledge. You should use the dot operator:
me = await client.get_me()
print(me.username)
# ^ we used the dot operator to access the username attribute
import telethon.sync
# ^^^^^ import sync
Or:
An older process is still running and is using the same 'session' file.
This error occurs when two or more clients use the same session, that is, when you write the same session name to
be used in the client:
• You have an older process using the same session file.
• You have two different scripts running (interactive sessions count too).
• You have two clients in the same script running at the same time.
The solution is, if you need two clients, use two sessions. If the problem persists and you’re on Linux, you can use
fuser my.session to find out the process locking the file. As a last resort, you can reboot your system.
If you really dislike SQLite, use a different session storage. There is an entire section covering that at Session Files.
Telegram doesn’t always send this information in order to save bandwidth. If you need the information, you should
fetch it yourself, since the library won’t do unnecessary work unless you need to:
In Python, classes can base others. This is called inheritance. What it means is that “if a class bases another, you can
use the other’s methods too”.
For example, Message bases ChatGetter. In turn, ChatGetter defines things like obj.chat_id.
So if you have a message, you can access that too:
Telegram has a lot to offer, and inheritance helps the library reduce boilerplate, so it’s important to know this concept.
For newcomers, this may be a problem, so we explain what it means here in the FAQ.
Yes, if you know what you are doing. However, you will probably have a lot of headaches to get threads and asyncio
to work together. Instead, consider using Quart, an asyncio-based alternative to Flask.
Check out quart_login.py for an example web-application based on Quart.
Yes, but these interpreters run the asyncio event loop implicitly, which interferes with the telethon.sync magic
module.
If you use them, you should not import sync:
# ...with this:
from telethon import TelegramClient, ...
You are also more likely to get “sqlite3.OperationalError: database is locked” with them. If they cause too much
trouble, just write your code in a .py file and run that, or use the normal python interpreter.
2.6. FAQ 17
Telethon Documentation, Release 1.26.0
This page contains a summary of all the important methods and properties that you may need when using Telethon.
They are sorted by relevance and are not in alphabetical order.
You should use this page to learn about which methods are available, and if you need a usage example or further
description of the arguments, be sure to follow the links.
Contents
• Client Reference
– TelegramClient
* Auth
* Base
* Messages
* Uploads
* Downloads
* Dialogs
* Users
* Chats
* Parse Mode
* Updates
* Bots
* Buttons
* Account
2.7.1 TelegramClient
This is a summary of the methods and properties you will find at TelegramClient.
Auth
Base
Messages
Uploads
send_file Sends message with the given file to the specified entity.
upload_file Uploads a file to Telegram’s servers, without sending it.
Downloads
Dialogs
Users
Chats
Parse Mode
parse_mode This property is the default parse mode used when send-
ing messages.
Updates
Bots
Buttons
Account
Here you will find a quick summary of all the methods and properties that you can access when working with events.
You can access the client that creates this event by doing event.client, and you should view the description of
the events to find out what arguments it allows on creation and its attributes (the properties will be shown here).
Important: Remember that all events base ChatGetter! Please see FAQ if you don’t know what this means or
the implications of it.
Contents
• Events Reference
– NewMessage
– MessageEdited
– MessageDeleted
– MessageRead
– ChatAction
– UserUpdate
– CallbackQuery
– InlineQuery
– Album
– Raw
2.8.1 NewMessage
Note: The new message event should be treated as a normal Message, with the following exceptions:
• pattern_match is the match object returned by pattern=.
• message is not the message string. It’s the Message object.
Remember, this event is just a proxy over the message, so while you won’t see its attributes and properties, you can
still access them. Please see the full documentation for examples.
2.8.2 MessageEdited
Occurs whenever a message is edited. Just like NewMessage, you should treat this event as a Message.
Full documentation for the MessageEdited.
2.8.3 MessageDeleted
Occurs whenever a message is deleted. Note that this event isn’t 100% reliable, since Telegram doesn’t always notify
the clients that a message was deleted.
It only has the deleted_id and deleted_ids attributes (in addition to the chat if the deletion happened in a
channel).
Full documentation for the MessageDeleted.
2.8.4 MessageRead
2.8.5 ChatAction
Occurs on certain chat actions, such as chat title changes, user join or leaves, pinned messages, photo changes, etc.
Full documentation for the ChatAction.
2.8.6 UserUpdate
2.8.7 CallbackQuery
Occurs whenever you sign in as a bot and a user clicks one of the inline buttons on your messages.
Full documentation for the CallbackQuery.
2.8.8 InlineQuery
Occurs whenever you sign in as a bot and a user sends an inline query such as @bot query.
Full documentation for the InlineQuery.
2.8.9 Album
2.8.10 Raw
Raw events are not actual events. Instead, they are the raw Update object that Telegram sends. You normally shouldn’t
need these.
This is the quick reference for those objects returned by client methods or other useful modules that the library has to
offer. They are kept in a separate page to help finding and discovering them.
Remember that this page only shows properties and methods, not attributes. Make sure to open the full documentation
to find out about the attributes.
Contents
• Objects Reference
– ChatGetter
– SenderGetter
– Message
* Properties
* Methods
– File
– Conversation
– AdminLogEvent
– Button
– InlineResult
– Dialog
– Draft
– Utils
2.9.1 ChatGetter
All events base ChatGetter, and some of the objects below do too, so it’s important to know its methods.
chat Returns the User, Chat or Channel where this object be-
longs to.
input_chat This InputPeer is the input version of the chat where the
message was sent.
chat_id Returns the marked chat integer ID.
is_private True if the message was sent as a private message.
is_group True if the message was sent on a group or megagroup.
is_channel True if the message was sent on a megagroup or chan-
nel.
get_chat Returns chat, but will make an API call to find the chat
unless it’s already cached.
get_input_chat Returns input_chat, but will make an API call to
find the input chat unless it’s already cached.
2.9.2 SenderGetter
Similar to ChatGetter, a SenderGetter is the same, but it works for senders instead.
2.9.3 Message
The Message type is very important, mostly because we are working with a library for a messaging platform, so
messages are widely used: in events, when fetching history, replies, etc.
It bases ChatGetter and SenderGetter.
Properties
Note: We document custom properties here, not all the attributes of the Message (which is the information Telegram
actually returns).
Methods
2.9.4 File
The File type is a wrapper object returned by Message.file, and you can use it to easily access a document’s
attributes, such as its name, bot-API style file ID, etc.
2.9.5 Conversation
The Conversation object is returned by the client.conversation() method to easily send and receive
responses like a normal conversation.
It bases ChatGetter.
2.9.6 AdminLogEvent
The AdminLogEvent object is returned by the client.iter_admin_log() method to easily iterate over past
“events” (deleted messages, edits, title changes, leaving members. . . )
These are all the properties you can find in it:
2.9.7 Button
The Button class is used when you login as a bot account to send messages with reply markup, such as inline buttons
or custom keyboards.
These are the static methods you can use to create instances of the markup:
2.9.8 InlineResult
The InlineResult object is returned inside a list by the client.inline_query() method to make an inline
query to a bot that supports being used in inline mode, such as @like.
Note that the list returned is in fact a subclass of a list called InlineResults, which, in addition of being a list
(iterator, indexed access, etc.), has extra attributes and methods.
These are the constants for the types, properties and methods you can find the individual results:
ARTICLE
PHOTO
GIF
VIDEO
VIDEO_GIF
AUDIO
DOCUMENT
LOCATION
VENUE
CONTACT
GAME
type The always-present type of this result.
message The always-present BotInlineMessage that will be sent
if click is called on this result.
title The title for this inline result.
description The description for this inline result.
url The URL present in this inline results.
Continued on next page
2.9.9 Dialog
2.9.10 Draft
entity The entity that belongs to this dialog (user, chat or chan-
nel).
input_entity Input version of the entity.
get_entity Returns entity but will make an API call if necessary.
get_input_entity Returns input_entity but will make an API call if
necessary.
text The markdown text contained in the draft.
raw_text The raw (text without formatting) contained in the draft.
is_empty Convenience bool to determine if the draft is empty or
not.
set_message Changes the draft message on the Telegram servers.
send Sends the contents of this draft to the dialog.
delete Deletes this draft, and returns True on success.
2.9.11 Utils
The telethon.utils module has plenty of methods that make using the library a lot easier. Only the interesting
ones will be listed here.
get_display_name Gets the display name for the given User, Chat or Chan-
nel.
get_extension Gets the corresponding extension for any Telegram me-
dia.
get_inner_text Gets the inner text that’s surrounded by the given enti-
ties.
get_peer_id Convert the given peer into its marked ID by default.
Continued on next page
Debugging is really important. Telegram’s API is really big and there are a lot of things that you should know. Such
as, what attributes or fields does a result have? Well, the easiest thing to do is printing it:
entity = await client.get_entity('username')
print(entity)
˓→banned_rights=None, participants_count=None)
That’s a lot of text. But as you can see, all the properties are there. So if you want the title you don’t use regex or
anything like splitting str(entity) to get what you want. You just access the attribute you need:
title = entity.title
Now it’s easy to see how we could get, for example, the year value. It’s inside date:
channel_year = entity.date.year
You don’t need to print everything to see what all the possible values can be. You can just search in http://tl.telethon.
dev/.
Remember that you can use Python’s isinstance to check the type of something. For example:
if isinstance(entity.photo, types.ChatPhotoEmpty):
print('Channel has no photo')
2.11 Entities
The library widely uses the concept of “entities”. An entity will refer to any User, Chat or Channel object that the API
may return in response to certain methods, such as GetUsersRequest.
Note: When something “entity-like” is required, it means that you need to provide something that can be turned into
an entity. These things include, but are not limited to, usernames, exact titles, IDs, Peer objects, or even entire User,
Chat and Channel objects and even phone numbers from people you have in your contact list.
To “encounter” an ID, you would have to “find it” like you would in the normal app. If the peer is in your dialogs,
you would need to client.get_dialogs(). If the peer is someone in a group, you would similarly client.
get_participants(group).
Once you have encountered an ID, the library will (by default) have saved their access_hash for you, which is
needed to invoke most methods. This is why sometimes you might encounter this error when working with the library.
You should except ValueError and run code that you know should work to find the entity.
Contents
• Entities
– What is an Entity?
– Getting Entities
– Entities vs. Input Entities
– Full Entities
2.11. Entities 33
Telethon Documentation, Release 1.26.0
– Accessing Entities
– Summary
A lot of methods and requests require entities to work. For example, you send a message to an entity, get the username
of an entity, and so on.
There are a lot of things that work as entities: usernames, phone numbers, chat links, invite links, IDs, and the types
themselves. That is, you can use any of those when you see an “entity” is needed.
Note: Remember that the phone number must be in your contact list before you can use it.
If you need to be 99% sure that the code will work (sometimes it’s simply impossible for the library to find the input
entity), or if you will reuse the chat a lot, consider using the following instead:
Through the use of the Session Files, the library will automatically remember the ID and hash pair, along with some
extra information, so you’re able to just do this:
# You can be more explicit about the type for said ID by wrapping
# it inside a Peer instance. This is recommended but not necessary.
from telethon.tl.types import PeerUser, PeerChat, PeerChannel
Note: You don’t need to get the entity before using it! Just let the library do its job. Use a phone from your contacts,
username, ID or input entity (preferred but not necessary), whatever you already have.
All methods in the TelegramClient call .get_input_entity() prior to sending the request to save you from
the hassle of doing so manually. That way, convenience calls such as client.send_message('username',
'hi!') become possible.
Every entity the library encounters (in any response to any call) will by default be cached in the .session file
(an SQLite database), to avoid performing unnecessary API calls. If the entity cannot be found, additonal calls like
ResolveUsernameRequest or GetContactsRequest may be made to obtain the required information.
Note: This section is informative, but worth reading. The library will transparently handle all of these details for you.
On top of the normal types, the API also make use of what they call their Input* versions of objects. The input
version of an entity (e.g. InputPeerUser, InputChat, etc.) only contains the minimum information that’s required from
Telegram to be able to identify who you’re referring to: a Peer’s ID and hash. They are named like this because they
are input parameters in the requests.
Entities’ ID are the same for all user and bot accounts, however, the access hash is different for each account, so
trying to reuse the access hash from one account in another will not work.
Sometimes, Telegram only needs to indicate the type of the entity along with their ID. For this purpose, Peer versions
of the entities also exist, which just have the ID. You cannot get the hash out of them since you should not be needing
it. The library probably has cached it before.
Peers are enough to identify an entity, but they are not enough to make a request with them. You need to know
their hash before you can “use them”, and to know the hash you need to “encounter” them, let it be in your dialogs,
2.11. Entities 35
Telethon Documentation, Release 1.26.0
Note: You can use peers with the library. Behind the scenes, they are replaced with the input variant. Peers “aren’t
enough” on their own but the library will do some more work to use the right type.
As we just mentioned, API calls don’t need to know the whole information about the entities, only their ID and hash.
For this reason, another method, client.get_input_entity() is available. This will always use the cache
while possible, making zero API calls most of the time. When a request is made, if you provided the full entity, e.g.
an User, the library will convert it to the required InputPeer automatically for you.
You should always favour client.get_input_entity() over client.get_entity() for this reason!
Calling the latter will always make an API call to get the most recent information about said entity, but invoking
requests don’t need this information, just the InputPeer. Only use client.get_entity() if you need to get
actual information, like the username, name, title, etc. of the entity.
To further simplify the workflow, since the version 0.16.2 of the library, the raw requests you make to the API are
also able to call client.get_input_entity() wherever needed, so you can even do things like:
The library will call the .resolve() method of the request, which will resolve 'username' with the appropriated
InputPeer. Don’t worry if you don’t get this yet, but remember some of the details here are important.
In addition to PeerUser, InputPeerUser, User (and its variants for chats and channels), there is also the concept of
UserFull.
This full variant has additional information such as whether the user is blocked, its notification settings, the bio or
about of the user, etc.
There is also messages.ChatFull which is the equivalent of full entities for chats and channels, with also the about
section of the channel. Note that the users field only contains bots for the channel (so that clients can suggest
commands to use).
You can get both of these by invoking GetFullUser, GetFullChat and GetFullChannel respectively.
Although it’s explicitly noted in the documentation that messages subclass ChatGetter and SenderGetter,
some people still don’t get inheritance.
When the documentation says “Bases: telethon.tl.custom.chatgetter.ChatGetter” it means that the
class you’re looking at, also can act as the class it bases. In this case, ChatGetter knows how to get the chat where
a thing belongs to.
So, a Message is a ChatGetter. That means you can do this:
message.is_private
message.chat_id
await message.get_chat()
# ...etc
SenderGetter is similar:
message.user_id
await message.get_input_user()
message.user
# ...etc
Quite a few things implement them, so it makes sense to reuse the code. For example, all events (except raw updates)
implement ChatGetter since all events occur in some chat.
2.11.6 Summary
TL;DR; If you’re here because of “Could not find the input entity for”, you must ask yourself “how did I find this
entity through official applications”? Now do the same with the library. Use what applies:
Once the library has “seen” the entity, you can use their integer ID. You can’t use entities from IDs the library hasn’t
seen. You must make the library see them at least once and disconnect properly. You know where the entities are and
you must tell the library. It won’t guess for you.
Telegram’s raw API can get very confusing sometimes, in particular when it comes to talking about “chats”, “chan-
nels”, “groups”, “megagroups”, and all those concepts.
This section will try to explain what each of these concepts are.
2.12.1 Chats
A Chat can be used to talk about either the common “subclass” that both chats and channels share, or the concrete
Chat type.
Technically, both Chat and Channel are a form of the Chat type.
Most of the time, the term Chat is used to talk about small group chats. When you create a group through an official
application, this is the type that you get. Official applications refer to these as “Group”.
Both the bot API and Telethon will add a minus sign (negate) the real chat ID so that you can tell at a glance, with just
a number, the entity type.
For example, if you create a chat with CreateChatRequest, the real chat ID might be something like 123. If you try
printing it from a message.chat_id you will see -123. This ID helps Telethon know you’re talking about a Chat.
2.12.2 Channels
Official applications create a broadcast channel when you create a new channel (used to broadcast messages, only
administrators can post messages).
Official applications implicitly migrate an existing Chat to a megagroup Channel when you perform certain actions
(exceed user limit, add a public username, set certain permissions, etc.).
A Channel can be created directly with CreateChannelRequest, as either a megagroup or broadcast.
Official applications use the term “channel” only for broadcast channels.
The API refers to the different types of Channel with certain attributes:
• A broadcast channel is a Channel with the channel.broadcast attribute set to True.
• A megagroup channel is a Channel with the channel.megagroup attribute set to True. Official applica-
tions refer to this as “supergroup”.
• A gigagroup channel is a Channel with the channel.gigagroup attribute set to True. Official applica-
tions refer to this as “broadcast groups”, and is used when a megagroup becomes very large and administrators
want to transform it into something where only they can post messages.
Both the bot API and Telethon will “concatenate” -100 to the real chat ID so that you can tell at a glance, with just a
number, the entity type.
For example, if you create a new broadcast channel, the real channel ID might be something like 456. If you try
printing it from a message.chat_id you will see -1000000000456. This ID helps Telethon know you’re
talking about a Channel.
You can convert between the “marked” identifiers (prefixed with a minus sign) and the real ones with utils.
resolve_id. It will return a tuple with the real ID, and the peer type (the class):
print(real_id) # 456
print(peer_type) # <class 'telethon.tl.types.PeerChannel'>
peer = peer_type(real_id)
print(peer) # PeerChannel(channel_id=456)
print(utils.get_peer_id(types.PeerChannel(456))) # -1000000000456
Note that this function can also work with other types, like Chat or Channel instances.
If you need to convert other types like usernames which might need to perform API calls to find out the identifier, you
can use client.get_peer_id:
If there is no “mark” (no minus sign), Telethon will assume your identifier refers to a User. If this is not the case, you
can manually fix it:
Certain methods only work on a Chat, and some others only work on a Channel (and these may only work in broadcast,
or megagroup). Your code likely knows what it’s working with, so it shouldn’t be too much of an issue.
If you need to find the Channel from a Chat that migrated to it, access the migrated_to property:
# chat is a Chat
channel = await client.get_entity(chat.migrated_to)
# channel is now a Channel
Channels do not have a “migrated_from”, but a ChannelFull does. You can use GetFullChannelRequest to obtain this:
This way, you can also access the linked discussion megagroup of a broadcast channel:
You do not need to use client.get_entity to access the migrated_from_chat_id Chat or the
linked_chat_id Channel. They are in the full.chats attribute:
if full_channel.migrated_from_chat_id:
migrated_from_chat = next(c for c in full.chats if c.id == full_channel.migrated_
˓→from_chat_id)
print(migrated_from_chat.title)
if full_channel.linked_chat_id:
linked_group = next(c for c in full.chats if c.id == full_channel.linked_chat_id)
print(linked_group.username)
The event shown above acts just like a custom.Message, which means you can access all the properties it has, like
.sender.
However events are different to other methods in the client, like client.get_messages. Events may not send
information about the sender or chat, which means it can be None, but all the methods defined in the client always
have this information so it doesn’t need to be re-fetched. For this reason, you have get_ methods, which will make a
network call if necessary.
In short, you should do this:
@client.on(events.NewMessage)
async def handler(event):
# event.input_chat may be None, use event.get_input_chat()
chat = await event.get_input_chat()
sender = await event.get_sender()
buttons = await event.get_buttons()
Notice, properties (message.sender) don’t need an await, but methods (message.get_sender) do need an
await, and you should use methods in events for these properties that may need network.
The code of your application starts getting big, so you decide to separate the handlers into different files. But how can
you access the client from these files? You don’t need to! Just events.register them:
# handlers/welcome.py
from telethon import events
@events.register(events.NewMessage('(?i)hello'))
async def handler(event):
client = event.client
await event.respond('Hey!')
await client.send_message('me', 'I said hello to someone')
Registering events is a way of saying “this method is an event handler”. You can use telethon.events.
is_handler to check if any method is a handler. You can think of them as a different approach to Flask’s blueprints.
It’s important to note that this does not add the handler to any client! You never specified the client on which the
handler should be used. You only declared that it is a handler, and its type.
To actually use the handler, you need to client.add_event_handler to the client (or clients) where they should
be added to:
# main.py
from telethon import TelegramClient
import handlers.welcome
This also means that you can register an event handler once and then add it to many clients without re-declaring the
event.
If for any reason you don’t want to use telethon.events.register, you can explicitly pass the event handler
to use to the mentioned client.add_event_handler:
Note: The event type is ignored in client.add_event_handler if you have used telethon.events.
register on the callback before, since that’s the point of using such method at all.
There might be cases when an event handler is supposed to be used solitary and it makes no sense to process any other
handlers in the chain. For this case, it is possible to raise a telethon.events.StopPropagation exception
which will cause the propagation of the update through your handlers to stop:
@client.on(events.NewMessage)
async def _(event):
# ... some conditions
await event.delete()
@client.on(events.NewMessage)
async def _(event):
# Will never be reached, because it is the second handler
# in the chain.
pass
Remember to check Update Events if you’re looking for the methods reference.
With asyncio, the library has several tasks running in the background. One task is used for sending requests, another
task is used to receive them, and a third one is used to handle updates.
To handle updates, you must keep your script running. You can do this in several ways. For instance, if you are not
running asyncio’s event loop, you should use client.run_until_disconnected:
import asyncio
from telethon import TelegramClient
client = TelegramClient(...)
...
client.run_until_disconnected()
Behind the scenes, this method is await’ing on the client.disconnected property, so the code above and the
following are equivalent:
import asyncio
from telethon import TelegramClient
client = TelegramClient(...)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.run_until_complete(main())
If you need to process updates sequentially (i.e. not in parallel), you should set sequential_updates=True
when creating the client:
Contents
• Session Files
– What are Sessions?
– Different Session Storage
They are an important part for the library to be efficient, such as caching and handling your authorization key (or you
would have to login every time!).
The first parameter you pass to the constructor of the TelegramClient is the session, and defaults to be the ses-
sion name (or full path). That is, if you create a TelegramClient('anon') instance and connect, an anon.
session file will be created in the working directory.
Note that if you pass a string it will be a file in the current working directory, although you can also pass absolute
paths.
The session file contains enough information for you to login without re-sending the code, so if you have to enter the
code more than once, maybe you’re changing the working directory, renaming or removing the file, or using random
names.
These database files using sqlite3 contain the required information to talk to the Telegram servers, such as to which
IP the client should connect, port, authorization key so that messages can be encrypted, and so on.
These files will by default also save all the input entities that you’ve seen, so that you can get information about a user
or channel by just their ID. Telegram will not send their access_hash required to retrieve more information about
them, if it thinks you have already seem them. For this reason, the library needs to store this information offline.
The library will by default too save all the entities (chats and channels with their name and username, and users with
the phone too) in the session file, so that you can quickly access them by username or phone number.
If you’re not going to work with updates, or don’t need to cache the access_hash associated with the entities’ ID,
you can disable this by setting client.session.save_entities = False.
If you don’t want to use the default SQLite session storage, you can also use one of the other implementations or
implement your own storage.
While it’s often not the case, it’s possible that SQLite is slow enough to be noticeable, in which case you can also use
a different storage. Note that this is rare and most people won’t have this issue, but it’s worth a mention.
To use a custom session storage, simply pass the custom session instance to TelegramClient instead of the session
name.
Telethon contains three implementations of the abstract Session class:
• MemorySession: stores session data within memory.
• SQLiteSession: stores sessions within on-disk SQLite databases. Default.
• StringSession: stores session data within memory, but can be saved as a string.
You can import these from telethon.sessions. For example, using the StringSession is done as follows:
# Note that it's also possible to save any other session type
# as a string by using ``StringSession.save(session_instance)``:
client = TelegramClient('sqlite-session', api_id, api_hash)
string = StringSession.save(client.session)
The easiest way to create your own storage implementation is to use MemorySession as the base and check out
how SQLiteSession or one of the community-maintained implementations work. You can find the relevant Python
files under the sessions/ directory in the Telethon’s repository.
After you have made your own implementation, you can add it to the community-maintained session implementation
list above with a pull request.
StringSession are a convenient way to embed your login credentials directly into your code for extremely easy
portability, since all they take is a string to be able to login without asking for your phone and code (or faster start if
you’re using a bot token).
The easiest way to generate a string session is as follows:
Think of this as a way to export your authorization key (what’s needed to login into your account). This will print a
string in the standard output (likely your terminal).
Warning: Keep this string safe! Anyone with this string can use it to login into your account and do anything
they want to to do.
This is similar to leaking your *.session files online, but it is easier to leak a string than it is to leak a file.
Once you have the string (which is a bit long), load it into your script somehow. You can use a normal text file and
open(...).read() it or you can save it in a variable directly:
string = '1aaNk8EX-YRfwoRsebUkugFvht6DUPi_Q25UOCzOAqzc...'
with TelegramClient(StringSession(string), api_id, api_hash) as client:
client.loop.run_until_complete(client.send_message('me', 'Hi'))
These strings are really convenient for using in places like Heroku since their ephemeral filesystem will delete external
files once your application is over.
Important: While you have access to this, you should always use the friendly methods listed on Client Reference
unless you have a better reason not to, like a method not existing or you wanting more control.
The TelegramClient doesn’t offer a method for every single request the Telegram API supports. However, it’s very
simple to call or invoke any request. Whenever you need something, don’t forget to check the documentation and look
for the method you need. There you can go through a sorted list of everything you can do.
Note: The reason to keep both https://tl.telethon.dev and this documentation alive is that the former allows instant
search results as you type, and a “Copy import” button. If you like namespaces, you can also do from telethon.
tl import types, functions. Both work.
You should also refer to the documentation to see what the objects (constructors) Telegram returns look like. Every
constructor inherits from a common type, and that’s the reason for this distinction.
Say client.send_message() didn’t exist, we could use the search to look for “message”. There we would find
SendMessageRequest, which we can work with.
Every request is a Python class, and has the parameters needed for you to invoke it. You can also call
help(request) for information on what input parameters it takes. Remember to “Copy import to the clipboard”,
or your script won’t be aware of this class! Now we have:
We see that this request must take at least two parameters, a peer of type InputPeer, and a message which is just a
Python string.
How can we retrieve this InputPeer? We have two options. We manually construct one, for instance:
Or we call client.get_input_entity():
import telethon
client.loop.run_until_complete(main())
Note: Remember that await must occur inside an async def. Every full API example assumes you already know
and do this.
When you’re going to invoke an API method, most require you to pass an InputUser, InputChat, or so on, this is why
using client.get_input_entity() is more straightforward (and often immediate, if you’ve seen the user be-
fore, know their ID, etc.). If you also need to have information about the whole user, use client.get_entity()
instead:
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
telethon.utils.get_input_peer:
Note: Since v0.16.2 this is further simplified. The Request itself will call client.get_input_entity
for you when required, but it’s good to remember what’s happening.
Message sent! Of course, this is only an example. There are over 250 methods available as of layer 80, and you can
use every single of them as you wish. Remember to use the right types! To sum up:
Note: Note that some requests have a “hash” parameter. This is not your api_hash! It likely isn’t your self-user
.access_hash either.
It’s a special hash used by Telegram to only send a difference of new data that you don’t already have with that request,
so you can leave it to 0, and it should work (which means no hash is known yet).
For those requests having a “limit” parameter, you can often set it to zero to signify “return default amount”. This
won’t work for all of them though, for instance, in “messages.search” it will actually return 0 items.
The library will automatically merge outgoing requests into a single container. Telegram’s API supports sending
multiple requests in a single container, which is faster because it has less overhead and the server can run them without
waiting for others. You can also force using a container manually:
Note that you cannot guarantee the order in which they are run. Try running the above code more than one time. You
will see the order in which the messages arrive is different.
If you use the raw API (the first option), you can use ordered to tell the server that it should run the requests
sequentially. This will still be faster than going one by one, since the server knows all requests directly:
await client([
SendMessageRequest('me', 'Hello'),
SendMessageRequest('me', ', '),
SendMessageRequest('me', 'World'),
SendMessageRequest('me', '.')
], ordered=True)
If any of the requests fails with a Telegram error (not connection errors or any other unexpected events), the library will
raise telethon.errors.common.MultiError. You can except this and still access the successful results:
try:
await client([
SendMessageRequest('me', 'Hello'),
SendMessageRequest('me', ''),
SendMessageRequest('me', 'World')
], ordered=True)
except MultiError as e:
# The first and third requests worked.
first = e.results[0]
third = e.results[2]
(continues on next page)
RPC stands for Remote Procedure Call, and when the library raises a RPCError, it’s because you have invoked some
of the API methods incorrectly (wrong parameters, wrong permissions, or even something went wrong on Telegram’s
server).
You should import the errors from telethon.errors like so:
try:
async with client.takeout() as takeout:
...
except errors.TakeoutInitDelayError as e:
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ here we except TAKEOUT_INIT_DELAY
print('Must wait', e.seconds, 'before takeout')
There isn’t any official list of all possible RPC errors, so the list of known errors is provided on a best-effort basis.
When new methods are available, the list may be lacking since we simply don’t know what errors can raise from them.
Once we do find out about a new error and what causes it, the list is updated, so if you see an error without a specific
class, do report it (and what method caused it)!.
This list is used to generate documentation for the raw API page. For example, if we want to know what errors can
occur from messages.sendMessage we can simply navigate to its raw API page and find it has 24 known RPC errors
at the time of writing.
All the “base” errors are listed in API Errors. Any other more specific error will be a subclass of these.
If the library isn’t aware of a specific error just yet, it will instead raise one of these superclasses. This means you may
find stuff like this:
If you do, make sure to open an issue or send a pull request to update the list of known errors.
These are some of the errors you may normally need to deal with:
• FloodWaitError (420), the same request was repeated many times. Must wait .seconds (you can access
this attribute). For example:
...
from telethon import errors
try:
messages = await client.get_messages(chat)
print(messages[0].text)
except errors.FloodWaitError as e:
print('Have to sleep', e.seconds, 'seconds')
time.sleep(e.seconds)
• SessionPasswordNeededError, if you have setup two-steps verification on Telegram and are trying to
sign in.
• FilePartMissingError, if you have tried to upload an empty file.
• ChatAdminRequiredError, you don’t have permissions to perform said operation on a chat or channel.
Try avoiding filters, i.e. when searching messages.
The generic classes for different error codes are:
• InvalidDCError (303), the request must be repeated on another DC.
• BadRequestError (400), the request contained errors.
• UnauthorizedError (401), the user is not authorized yet.
• ForbiddenError (403), privacy violation error.
• NotFoundError (404), make sure you’re invoking Request’s!
If the error is not recognised, it will only be an RPCError.
You can refer to all errors from Python through the telethon.errors module. If you don’t know what attributes
they have, try printing their dir (like print(dir(e))).
2.16.3 Attributes
Some of the errors carry additional data in them. When they look like EMAIL_UNCONFIRMED_X, the _X value will
be accessible from the error instance. The current list of errors that do this is the following:
• EmailUnconfirmedError has .code_length.
• FileMigrateError has .new_dc.
• FilePartMissingError has .which.
• FloodTestPhoneWaitError has .seconds.
• FloodWaitError has .seconds.
• InterdcCallErrorError has .dc.
• InterdcCallRichErrorError has .dc.
• NetworkMigrateError has .new_dc.
• PhoneMigrateError has .new_dc.
• SlowModeWaitError has .seconds.
• TakeoutInitDelayError has .seconds.
• UserMigrateError has .new_dc.
Don’t spam. You won’t get FloodWaitError or your account banned or deleted if you use the library for legit use
cases. Make cool tools. Don’t spam! Nobody knows the exact limits for all requests since they depend on a lot of
factors, so don’t bother asking.
Still, if you do have a legit use case and still get those errors, the library will automatically sleep when they are smaller
than 60 seconds by default. You can set different “auto-sleep” thresholds:
VoIP numbers are very limited, and some countries are more limited too.
Telethon is more than just another viable alternative when developing bots for Telegram. If you haven’t decided which
wrapper library for bots to use yet, using Telethon from the beginning may save you some headaches later.
Contents
The Telegram Bot API, also known as HTTP Bot API and from now on referred to as simply “Bot API” is Telegram’s
official way for developers to control their own Telegram bots. Quoting their main page:
The Bot API is an HTTP-based interface created for developers keen on building bots for Telegram.
To learn how to create and set up a bot, please consult our Introduction to Bots and Bot FAQ.
Bot API is simply an HTTP endpoint which translates your requests to it into MTProto calls through tdlib, their bot
backend.
Configuration of your bot, such as its available commands and auto-completion, is configured through @BotFather.
MTProto is Telegram’s own protocol to communicate with their API when you connect to their servers.
Telethon is an alternative MTProto-based backend written entirely in Python and much easier to setup and use.
Both official applications and third-party clients (like your own applications) logged in as either user or bots can use
MTProto to communicate directly with Telegram’s API (which is not the HTTP bot API).
When we talk about MTProto, we often mean “MTProto-based clients”.
MTProto clients (like Telethon) connect directly to Telegram’s servers, which means there is no HTTP connection,
no “polling” or “web hooks”. This means less overhead, since the protocol used between you and the server is much
more compact than HTTP requests with responses in wasteful JSON.
Since there is a direct connection to Telegram’s servers, even if their Bot API endpoint is down, you can still have
connection to Telegram directly.
Using a MTProto client, you are also not limited to the public API that they expose, and instead, you have full control
of what your bot can do. Telethon offers you all the power with often much easier usage than any of the available
Python Bot API wrappers.
If your application ever needs user features because bots cannot do certain things, you will be able to easily login as a
user and even keep your bot without having to learn a new library.
If less overhead and full control didn’t convince you to use Telethon yet, check out the wiki page MTProto vs HTTP
Bot API with a more exhaustive and up-to-date list of differences.
It doesn’t matter if you wrote your bot with requests and you were making API requests manually, or if you used a
wrapper library like python-telegram-bot or pyTelegramBotAPI. It’s never too late to migrate to Telethon!
If you were using an asynchronous library like aiohttp or a wrapper like aiogram or dumbot, it will be even easier,
because Telethon is also an asynchronous library.
Next, we will see some examples from the most popular libraries.
def main():
"""Start the bot."""
updater = Updater("TOKEN")
dp = updater.dispatcher
dp.add_handler(CommandHandler("start", start))
dp.add_handler(MessageHandler(Filters.text & ~Filters.command, echo))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
@bot.on(events.NewMessage(pattern='/start'))
async def start(event):
"""Send a message when the command /start is issued."""
await event.respond('Hi!')
raise events.StopPropagation
@bot.on(events.NewMessage)
async def echo(event):
"""Echo the user message."""
await event.respond(event.text)
def main():
"""Start the bot."""
bot.run_until_disconnected()
if __name__ == '__main__':
main()
Key differences:
• The recommended way to do it imports fewer things.
• All handlers trigger by default, so we need events.StopPropagation.
• Adding handlers, responding and running is a lot less verbose.
• Telethon needs async def and await.
• The bot isn’t hidden away by Updater or Dispatcher.
import telebot
bot = telebot.TeleBot("TOKEN")
@bot.message_handler(commands=['start'])
def send_welcome(message):
bot.reply_to(message, "Howdy, how are you doing?")
@bot.message_handler(func=lambda m: True)
def echo_all(message):
bot.reply_to(message, message.text)
bot.polling()
@bot.on(events.NewMessage(pattern='/start'))
async def send_welcome(event):
await event.reply('Howdy, how are you doing?')
@bot.on(events.NewMessage)
async def echo_all(event):
await event.reply(event.text)
bot.run_until_disconnected()
Key differences:
• Instead of doing bot.reply_to(message), we can do event.reply. Note that the event behaves just
like their message.
• Telethon also supports func=lambda m: True, but it’s not necessary.
@dp.message_handler(commands=['start'])
async def send_welcome(message: types.Message):
"""
This handler will be called when client send `/start` command.
"""
await message.reply("Hi!\nI'm EchoBot!\nPowered by aiogram.")
@dp.message_handler(regexp='(^cat[s]?$|puss)')
(continues on next page)
@dp.message_handler()
async def echo(message: types.Message):
await bot.send_message(message.chat.id, message.text)
if __name__ == '__main__':
executor.start_polling(dp, skip_updates=True)
After rewrite:
@bot.on(events.NewMessage(pattern='/start'))
async def send_welcome(event):
await event.reply('Howdy, how are you doing?')
@bot.on(events.NewMessage(pattern='(^cat[s]?$|puss)'))
async def cats(event):
await event.reply('Cats is here ', file='data/cats.jpg')
@bot.on(events.NewMessage)
async def echo_all(event):
await event.reply(event.text)
if __name__ == '__main__':
bot.run_until_disconnected()
Key differences:
• Telethon offers convenience methods to avoid retyping bot.send_photo(message.chat.id, ...)
all the time, and instead let you type event.reply.
• Sending files is a lot easier. The methods for sending photos, documents, audios, etc. are all the same!
class Subbot(Bot):
async def init(self):
self.me = await self.getMe()
Subbot(token).run()
After rewriting:
class Subbot(TelegramClient):
def __init__(self, *a, **kw):
super().__init__(*a, **kw)
self.add_event_handler(self.on_update, events.NewMessage)
Key differences:
• Telethon method names are snake_case.
• dumbot does not offer friendly methods like update.reply.
• Telethon does not have an implicit on_update handler, so we need to manually register one.
Contents
• Mastering asyncio
– What’s asyncio?
– Why asyncio?
– What are asyncio basics?
– What does telethon.sync do?
– What are async, await and coroutines?
– Can I use threads?
– client.run_until_disconnected() blocks!
– What else can asyncio do?
– Why does client.start() work outside async?
– Where can I read more?
asyncio is a Python 3’s built-in library. This means it’s already installed if you have Python 3. Since Python 3.5, it
is convenient to work with asynchronous code. Before (Python 3.4) we didn’t have async or await, but now we do.
asyncio stands for Asynchronous Input Output. This is a very powerful concept to use whenever you work IO.
Interacting with the web or external APIs such as Telegram’s makes a lot of sense this way.
Asynchronous IO makes a lot of sense in a library like Telethon. You send a request to the server (such as “get some
message”), and thanks to asyncio, your code won’t block while a response arrives.
The alternative would be to spawn a thread for each update so that other code can run while the response arrives. That
is a lot more expensive.
The code will also run faster, because instead of switching back and forth between the OS and your script, your
script can handle it all. Avoiding switching saves quite a bit of time, in Python or any other language that supports
asynchronous IO. It will also be cheaper, because tasks are smaller than threads, which are smaller than processes.
The sync module rewrites most async def methods in Telethon to something similar to this:
def new_method():
result = original_method()
if loop.is_running():
# the loop is already running, return the await-able to the user
return result
(continues on next page)
print(client.get_me().username)
Instead of this:
me = client.loop.run_until_complete(client.get_me())
print(me.username)
As you can see, it’s a lot of boilerplate and noise having to type run_until_complete all the time, so you can let
the magic module to rewrite it for you. But notice the comment above: it won’t run the loop if it’s already running,
because it can’t. That means this:
Will fail. So if you’re inside an async def, then the loop is running, and if the loop is running, you must await
things yourself:
loop.run_until_complete(main())
The async keyword lets you define asynchronous functions, also known as coroutines, and also iterate over asyn-
chronous loops or use async with:
import asyncio
print(message.sender.username)
loop = asyncio.get_event_loop()
# ^ this assigns the default event loop from the main thread to a variable
loop.run_until_complete(main())
# ^ this runs the *entire* loop until the main() function finishes.
# While the main() function does not finish, the loop will be running.
# While the loop is running, you can't run it again.
The await keyword blocks the current task, and the loop can run other tasks. Tasks can be thought of as “threads”,
since many can run concurrently:
import asyncio
loop = asyncio.get_event_loop() # get the default loop for the main thread
loop.create_task(world(2)) # create the world task, passing 2 as delay
loop.create_task(hello(delay=1)) # another task, but with delay 1
try:
# run the event loop forever; ctrl+c to stop it
# we could also run the loop for three seconds:
# loop.run_until_complete(asyncio.sleep(3))
loop.run_forever()
except KeyboardInterrupt:
pass
import asyncio
loop = asyncio.get_event_loop()
loop.create_task(world(2))
loop.create_task(hello(1))
loop.run_until_complete(asyncio.sleep(3))
Yes, you can, but you must understand that the loops themselves are not thread safe. and you must be sure to know
what is happening. The easiest and cleanest option is to use asyncio.run to create and manage the new event loop
for you:
import asyncio
import threading
def go():
asyncio.run(actual_work())
threading.Thread(target=go).start()
Generally, you don’t need threads unless you know what you’re doing. Just create another task, as shown above. If
you’re using the Telethon with a library that uses threads, you must be careful to use threading.Lock whenever
you use the client, or enable the compatible mode. For that, see Compatibility and Convenience.
You may have seen this error:
It just means you didn’t create a loop for that thread, and if you don’t pass a loop when creating the client, it uses
asyncio.get_event_loop(), which only works in the main thread.
All of what client.run_until_disconnected() does is run the asyncio’s event loop until the client is
disconnected. That means the loop is running. And if the loop is running, it will run all the tasks in it. So if you want
to run other code, create tasks for it:
loop.create_task(clock())
...
client.run_until_disconnected()
This creates a task for a clock that prints the time every second. You don’t need to use client.
run_until_disconnected() either! You just need to make the loop is running, somehow. loop.
run_forever() and loop.run_until_complete() can also be used to run the loop, and Telethon will
be happy with any approach.
Of course, there are better tools to run code hourly or daily, see below.
Asynchronous IO is a really powerful tool, as we’ve seen. There are plenty of other useful libraries that also use
asyncio and that you can integrate with Telethon.
• aiohttp is like the infamous requests but asynchronous.
• quart is an asynchronous alternative to Flask.
• aiocron lets you schedule things to run things at a desired time, or run some tasks hourly, daily, etc.
And of course, asyncio itself! It has a lot of methods that let you do nice things. For example, you can run requests in
parallel:
async def main():
last, sent, download_path = await asyncio.gather(
client.get_messages('telegram', 10),
client.send_message('me', 'Using asyncio!'),
client.download_profile_photo('telegram')
)
loop.run_until_complete(main())
This code will get the 10 last messages from @telegram, send one to the chat with yourself, and also download the
profile photo of the channel. asyncio will run all these three tasks at the same time. You can run all the tasks you
want this way.
A different way would be:
loop.create_task(client.get_messages('telegram', 10))
loop.create_task(client.send_message('me', 'Using asyncio!'))
loop.create_task(client.download_profile_photo('telegram'))
They will run in the background as long as the loop is running too.
You can also start an asyncio server in the main script, and from another script, connect to it to achieve Inter-Process
Communication. You can get as creative as you want. You can program anything you want. When you use a library,
you’re not limited to use only its methods. You can combine all the libraries you want. People seem to forget this
simple fact!
Because it’s so common that it’s really convenient to offer said functionality by default. This means you can set up all
your event handlers and start the client without worrying about loops at all.
Using the client in a with block, start, run_until_disconnected, and disconnect all support this.
Check out my blog post about asyncio, which has some more examples and pictures to help you understand what
happens when the loop runs.
Full API is not how you are intended to use the library. You should always prefer the Client Reference. However, not
everything is implemented as a friendly method, so full API is your last resort.
If you select a method in Client Reference, you will most likely find an example for that method. This is how you are
intended to use the library.
Full API will break between different minor versions of the library, since Telegram changes very often. The friendly
methods will be kept compatible between major versions.
If you need to see real-world examples, please refer to the wiki page of projects using Telethon.
Note: These examples assume you have read The Full API.
Contents
Note that Chat are normal groups, and Channel are a special form of Chat, which can also be super-groups if their
megagroup member is True.
Once you have the entity of the channel you want to join to, you can make use of the JoinChannelRequest to join such
channel:
AAAAAFFszQPyPEZ7wgxLtd on this example, is the hash of the chat or channel. Now you can use ImportChat-
InviteRequest as follows:
If you don’t want to add yourself, maybe because you’re already in, you can always add someone else with the
AddChatUserRequest, which use is very straightforward, or InviteToChannelRequest for channels:
await client(InviteToChannelRequest(
channel,
[users_to_add]
))
Note that this method will only really work for friends or bot accounts. Trying to mass-add users with this approach
will not work, and can put both your account and group to risk, possibly being flagged as spam and limited.
If you don’t need to join but rather check whether it’s a group or a channel, you can use the CheckChatInviteRequest,
which takes in the hash of said channel or group.
It has been asked quite a few times (really, many), and while I don’t understand why so many people ask this, the
solution is to use GetMessagesViewsRequest, setting increment=True:
await client(GetMessagesViewsRequest(
peer=channel,
id=msg_ids,
increment=True
))
Note that you can only do this once or twice a day per account, running this in a loop will obviously not increase the
views forever unless you wait a day between each iteration. If you run it any sooner than that, the views simply won’t
be increased.
2.21 Users
Note: These examples assume you have read The Full API.
Contents
• Users
– Retrieving full information
– Updating your name and/or bio
– Updating your username
– Updating your profile photo
If you need to retrieve the bio, biography or about information for a user you should use GetFullUser:
bio = full.full_user.about
The first name, last name and bio (about) can all be changed with the same request. Omitted fields won’t change after
invoking UpdateProfile:
await client(UpdateProfileRequest(
about='This is a test from Telethon'
))
2.21. Users 63
Telethon Documentation, Release 1.26.0
await client(UpdateUsernameRequest('new_username'))
The easiest way is to upload a new file and use that as the profile photo through UploadProfilePhoto:
from telethon.tl.functions.photos import UploadProfilePhotoRequest
await client(UploadProfilePhotoRequest(
await client.upload_file('/path/to/some/file')
))
Note: These examples assume you have read The Full API.
Contents
Stickers are nothing else than files, and when you successfully retrieve the stickers for a certain sticker set, all you
will have are handles to these files. Remember, the files Telegram holds on their servers can be referenced through
this pair of ID/hash (unique per user), and you need to use this handle when sending a “document” message. This
working example will send yourself the very first sticker you have:
# Get all the sticker sets this user has
from telethon.tl.functions.messages import GetAllStickersRequest
sticker_sets = await client(GetAllStickersRequest(0))
It works very similar to replying to a message. You need to specify the chat, message ID you wish to react to, and
reaction, using SendReaction:
await client(SendReactionRequest(
peer=chat,
msg_id=42,
reaction=''
))
Note that you cannot use strings like :heart: for the reaction. You must use the desired emoji directly. You can
most easily achieve this by copy-pasting the emoji from an official application such as Telegram Desktop.
If for some reason you cannot embed emoji directly into the code, you can also use its unicode escape (which you can
find using websites like unicode-table.com), or install a different package, like emoji:
# All of these work exactly the same (you only need one):
import emoji
reaction = emoji.emojize(':red_heart:')
reaction = ''
reaction = '\u2764'
Please make sure to check the help pages of the respective websites you use if you need a more in-depth explanation
on how they work. Telethon only needs you to provide the emoji in some form. Some packages or websites can make
this easier.
The current markdown and HTML parsers do not offer a way to send spoilers yet. You need to use MessageEnti-
tySpoiler so that parts of the message text are shown under a spoiler.
The simplest way to do this is to modify the builtin parsers to support sending these new message entities with the
features they already provide.
The current markdown and HTML parsers do not offer a way to send custom emoji yet. You need to use MessageEn-
tityCustomEmoji so that parts of the message text with emoji are replaced with a custom one instead.
The simplest way to do this is to modify the builtin parsers to support sending these new message entities with the
features they already provide.
MessageEntityCustomEmoji must wrap an emoji in order to work (you can’t put it around arbitrary "text", it won’t
work), so be sure to keep this in mind when using it.
To find the document_id for the custom emoji, the simplest way is to send a message with an official client con-
taining the custom emoji you want, and then print the message.entities to find the document_id.
If you prefer, you can also use GetFeaturedEmojiStickers to find about the document_id of featured custom emoji.
2.23 Philosophy
The intention of the library is to have an existing MTProto library existing with hardly any dependencies (indeed,
wherever Python is available, you can run this library).
Being written in Python means that performance will be nowhere close to other implementations written in, for in-
stance, Java, C++, Rust, or pretty much any other compiled language. However, the library turns out to actually be
pretty decent for common operations such as sending messages, receiving updates, or other scripting. Uploading files
may be notably slower, but if you would like to contribute, pull requests are appreciated!
If libssl is available on your system, the library will make use of it to speed up some critical parts such as encrypting
and decrypting the messages. Files will notably be sent and downloaded faster.
The main focus is to keep everything clean and simple, for everyone to understand how working with MTProto and
Telegram works. Don’t be afraid to read the source, the code won’t bite you! It may prove useful when using the
library on your own use cases.
Note that Telegram has changed the length of login codes multiple times in the past, so if dc_id repeated five times
does not work, try repeating it six times.
The library itself is under the telethon/ directory. The __init__.py file there exposes the main
TelegramClient, a class that servers as a nice interface with the most commonly used methods on Telegram
such as sending messages, retrieving the message history, handling updates, etc.
The TelegramClient inherits from several mixing Method classes, since there are so many methods that having
them in a single file would make maintenance painful (it was three thousand lines before this separation happened!).
It’s a “god object”, but there is only a way to interact with Telegram really.
The TelegramBaseClient is an ABC which will support all of these mixins so they can work together nicely. It
doesn’t even know how to invoke things because they need to be resolved with user information first (to work with
input entities comfortably).
The client makes use of the network/mtprotosender.py. The MTProtoSender is responsible for con-
necting, reconnecting, packing, unpacking, sending and receiving items from the network. Basically, the low-level
communication with Telegram, and handling MTProto-related functions and types such as BadSalt.
The sender makes use of a Connection class which knows the format in which outgoing messages should be sent
(how to encode their length and their body, if they’re further encrypted).
The files under telethon_generator/ are used to generate the code that gets placed under telethon/tl/.
The parsers take in files in a specific format (such as .tl for objects and .json for errors) and spit out the generated
classes which represent, as Python classes, the request and types defined in the .tl file. It also constructs an index so
that they can be imported easily.
Custom documentation can also be generated to easily navigate through the vast amount of items offered by the API.
If you clone the repository, you will have to run python setup.py gen in order to generate the code. Installing
the library runs the generator too, but the mentioned command will just generate code.
Basically, make it readable, while keeping the style similar to the code of whatever file you’re working on.
Also note that not everyone has 4K screens for their primary monitors, so please try to stick to the 80-columns limit.
This makes it easy to git diff changes from a terminal before committing changes. If the line has to be long,
please don’t exceed 120 characters.
For the commit messages, please make them explanatory. Not only they’re helpful to troubleshoot when certain issues
could have been introduced, but they’re also used to construct the change log once a new version is ready.
If you don’t know enough Python, I strongly recommend reading Dive Into Python 3, available online for free. For
instance, remember to do if x is None or if x is not None instead if x == None!
2.27 Tests
Telethon uses Pytest, for testing, Tox for environment setup, and pytest-asyncio and pytest-cov for asyncio and cover-
age integration.
While reading the full documentation for these is probably a good idea, there is a lot to read, so a brief summary of
these tools is provided below for convienience.
Pytest is a tool for discovering and running python tests, as well as allowing modular reuse of test setup code using
fixtures.
Most Pytest tests will look something like this:
def test_my_thing(fixture):
assert my_thing(fixture) == 42
@pytest.mark.asyncio
async def test_my_thing(event_loop):
assert await my_other_thing(loop=event_loop) == 42
Note here:
1. The test imports one specific function. The role of unit tests is to test that the implementation of some unit, like
a function or class, works. It’s role is not so much to test that components interact well with each other. I/O,
such as connecting to remote servers, should be avoided. This helps with quickly identifying the source of an
error, finding silent breakage, and makes it easier to cover all possible code paths.
System or integration tests can also be useful, but are currently out of scope of Telethon’s automated testing.
2. A function test_my_thing is declared. Pytest searches for files starting with test_, classes starting with
Test and executes any functions or methods starting with test_ it finds.
3. The function is declared with a parameter fixture. Fixtures are used to request things required to run the
test, such as temporary directories, free TCP ports, Connections, etc. Fixtures are declared by simply adding
the fixture name as parameter. A full list of available fixtures can be found with the pytest --fixtures
command.
4. The test uses a simple assert to test some condition is valid. Pytest uses some magic to ensure that the errors
from this are readable and easy to debug.
5. The pytest.mark.asyncio fixture is provided by pytest-asyncio. It starts a loop and executes a test
function as coroutine. This should be used for testing asyncio code. It also declares the event_loop fixture,
which will request an asyncio event loop.
Tox is a tool for automated setup of virtual environments for testing. While the tests can be run directly by just running
pytest, this only tests one specific python version in your existing environment, which will not catch e.g. undeclared
dependencies, or version incompatabilities.
Tox environments are declared in the tox.ini file. The default environments, declared at the top, can be simply run
with tox. The option tox -e py36,flake can be used to request specific environments to be run.
Coverage is a useful metric for testing. It measures the lines of code and branches that are exercised by the tests. The
higher the coverage, the more likely it is that any coding errors will be caught by the tests.
A brief coverage report can be generated with the --cov option to tox, which will be passed on to pytest.
Additionally, the very useful HTML report can be generated with --cov --cov-report=html, which contains
a browsable copy of the source code, annotated with coverage information for each line.
Telegram’s Type Language (also known as TL, found on .tl files) is a concise way to define what other programming
languages commonly call classes or structs.
Every definition is written as follows for a Telegram object is defined as follows:
name#id argument_name:argument_type = CommonType
This means that in a single line you know what the TLObject name is. You know it’s unique ID, and you know what
arguments it has. It really isn’t that hard to write a generator for generating code to any platform!
The generated code should also be able to encode the TLObject (let this be a request or a type) into bytes, so they
can be sent over the network. This isn’t a big deal either, because you know how the TLObject’s are made, and how
the types should be serialized.
You can either write your own code generator, or use the one this library provides, but please be kind and keep some
special mention to this project for helping you out.
This is only a introduction. The TL language is not that easy. But it’s not that hard either. You’re free to sniff the
telethon_generator/ files and learn how to parse other more complex lines, such as flags (to indicate things
that may or may not be written at all) and vector’s.
If you’re going to use the code on this repository to guide you, please be kind and don’t forget to mention it helped
you!
You should start by reading the source code on the first release of the project, and start creating a MTProtoSender.
Once this is made, you should write by hand the code to authenticate on the Telegram’s server, which are some steps
required to get the key required to talk to them. Save it somewhere! Then, simply mimic, or reinvent other parts of the
code, and it will be ready to go within a few days.
Good luck!
Telethon was made for Python, and it has inspired other libraries such as gramjs (JavaScript) and grammers (Rust).
But there is a lot more beyond those, made independently by different developers.
If you’re looking for something like Telethon but in a different programming language, head over to Telegram API in
Other Languages in the official wiki for a (mostly) up-to-date list.
This page lists all the available versions of the library, in chronological order. You should read this when upgrading
the library to know where your code can break, and where it can take advantage of new goodies!
* Breaking Changes
– Rushed release to fix login (v1.24)
* Breaking Changes
– New schema and bug fixes (v1.23)
* Enhancements
* Bug fixes
– New schema and bug fixes (v1.22)
* Enhancements
* Bug fixes
– New schema and QoL improvements (v1.21)
* Additions
* Enhancements
* Bug fixes
– New schema and QoL improvements (v1.20)
* Additions
* Enhancements
* Bug fixes
– New raw API call methods (v1.19)
* Additions
* Enhancements
* Bug fixes
– New layer and QoL improvements (v1.18)
* Additions
* Enhancements
* Bug fixes
– Channel comments and Anonymous Admins (v1.17)
* Breaking Changes
* Additions
* Enhancements
* Bug fixes
– Bug Fixes (v1.16.1)
* Enhancements
* Bug Fixes
– Channel Statistics (v1.16)
* Breaking Changes
* Enhancements
– QR login (v1.15)
* Additions
* Enhancements
* Bug fixes
– Minor quality of life improvements (v1.14)
* Additions
* Bug fixes
* Enhancements
– Bug Fixes (v1.13)
* Bug fixes
– Bug Fixes (v1.12)
* Bug fixes
– Bug Fixes (v1.11)
* Bug fixes
* Enhancements
– Scheduled Messages (v1.10)
* Additions
* Bug fixes
* Enhancements
* Additions
* Bug fixes
* Enhancements
– Documentation Overhaul (v1.8)
* Breaking Changes
* Additions
* Bug fixes
* Enhancements
* Internal Changes
– Fix-up for Photo Downloads (v1.7.1)
– Easier Events (v1.7)
* Breaking Changes
* Additions
* New bugs
* Bug fixes
* Enhancements
– Tidying up Internals (v1.6)
* Breaking Changes
* Additions
* Bug fixes
* Enhancements
* Internal changes
– Layer Update (v1.5.5)
* Additions
* Bug fixes
* Enhancements
– Bug Fixes (v1.5.3)
* Breaking Changes
* Additions
* Bug fixes
* Enhancements
– Takeout Sessions (v1.5.2)
* Bug fixes
– object.to_json() (v1.5.1)
* Additions
* Bug fixes
* Enhancements
– Polls with the Latest Layer (v1.5)
* Breaking Changes
* Additions
* Bug fixes
* Enhancements
* Internal changes
– Error Descriptions in CSV files (v1.4.3)
* Bug fixes
* Enhancements
* Internal changes
– Bug Fixes (v1.4.2)
* Bug fixes
* Enhancements
– Connection Overhaul (v1.4)
* Breaking Changes
* Additions
* Bug fixes
* Enhancements
– Event Templates (v1.3)
* Breaking Changes
* Additions
* Bug fixes
* Enhancements
* Internal changes
– Conversations, String Sessions and More (v1.2)
* Additions
* Bug fixes
* Enhancements
– Better Custom Message (v1.1.1)
* Bug fixes
– Bot Friendly (v1.1)
* Additions
* Bug fixes
* Enhancements
* Internal changes
– New HTTP(S) Connection Mode (v1.0.4)
* Additions
* Bug fixes
* Enhancements
* Internal changes
– Iterate Messages in Reverse (v1.0.3)
* Additions
* Bug fixes
– Bug Fixes (v1.0.2)
– Bug Fixes (v1.0.1)
* Bug fixes
– Synchronous magic (v1.0)
* Breaking Changes
* Additions
* Bug fixes
* Enhancements
– Core Rewrite in asyncio (v1.0-rc1)
* Breaking Changes
* Additions
* Bug fixes
* Enhancements
* Internal changes
– Custom Message class (v0.19.1)
* Breaking Changes
* Additions
* Bug fixes
* Enhancements
* Internal changes
– Catching up on Updates (v0.19)
* Additions
* Bug fixes
* Enhancements
* Internal changes
– Pickle-able objects (v0.18.3)
* Breaking changes
* Additions
* Bug fixes
* Enhancements
* Internal changes
– Several bug fixes (v0.18.2)
* Additions
* Bug fixes
– Iterator methods (v0.18.1)
* Breaking changes
* Additions
* Bug fixes
* Enhancements
* Internal changes
– Sessions overhaul (v0.18)
* Breaking changes
* Additions
* Bug fixes
* Internal changes
– Further easing library usage (v0.17.4)
* Additions
* Bug fixes
* Internal changes
– New small convenience functions (v0.17.3)
* Additions
* Bug fixes
* Internal changes
– New small convenience functions (v0.17.2)
* Additions
* Bug fixes
* Internal changes
– Updates as Events (v0.17.1)
– Trust the Server with Updates (v0.17)
* Additions
* Enhancements
* Bug fixes
– New .resolve() method (v0.16.2)
* Additions
* Enhancements
* Bug fixes
* Internal changes
– MtProto 2.0 (v0.16.1)
* Additions
* Bug fixes
* Internal changes
– Sessions as sqlite databases (v0.16)
* Breaking changes
* Additions
* Bug fixes
* Internal changes
– IPv6 support (v0.15.5)
* Additions
* Enhancements
* Bug fixes
– General enhancements (v0.15.4)
* Additions
* Bug fixes
* Internal changes
– Bug fixes with updates (v0.15.3)
– Bug fixes and new small features (v0.15.2)
* Enhancements
* Bug fixes
* Internal changes
– Custom Entity Database (v0.15.1)
* Additions
* Enhancements
* Bug fixes
– Updates Overhaul Update (v0.15)
* Breaking changes
* Enhancements
* Bug fixes
* Internal changes
– Serialization bug fixes (v0.14.2)
* Bug fixes
* Internal changes
– Farewell, BinaryWriter (v0.14.1)
* Bug fixes
* Internal changes
– Several requests at once and upload compression (v0.14)
* Additions
* Enhancements
* Bug fixes
– Quick fix-up (v0.13.6)
– Attempts at more stability (v0.13.5)
* Bug fixes
* Enhancements
* Internal changes
– More bug fixes and enhancements (v0.13.4)
* Additions
* Bug fixes
* Internal changes
– Bug fixes and enhancements (v0.13.3)
* Bug fixes
* Enhancements
– New way to work with updates (v0.13.2)
* Bug fixes
– Invoke other requests from within update callbacks (v0.13.1)
– Connection modes (v0.13)
* Additions
* Enhancements
* Deprecation
– Added verification for CDN file (v0.12.2)
– CDN support (v0.12.1)
* Bug fixes
– Newbie friendly update (v0.12)
* Breaking changes
* Additions
* Bug fixes
– get_input_* now works with vectors (v0.11.5)
– get_input_* everywhere (v0.11.4)
– Quick .send_message() fix (v0.11.3)
– Callable TelegramClient (v0.11.2)
* Bugs fixes
– Improvements to the updates (v0.11.1)
* Bug fixes
– Support for parallel connections (v0.11)
* Breaking changes
* Additions
* Bug fixes
* Internal changes
– JSON session file (v0.10.1)
* Additions
* Enhancements
– Full support for different DCs and ++stable (v0.10)
* Enhancements
– Stability improvements (v0.9.1)
* Enhancements
– General improvements (v0.9)
* Additions
* Bug fixes
* Internal changes
– Bot login and proxy support (v0.8)
* Additions
* Bug fixes
– Long-run bug fix (v0.7.1)
– Two factor authentication (v0.7)
– Updated pip version (v0.6)
– Ready, pip, go! (v0.5)
This new layer includes things such as emoji status, more admin log events, forum topics and message reactions,
among other things. You can access these using raw API. It also contains a few bug fixes.
These were fixed in the v1.25 series:
• client.edit_admin did not work on small group chats.
• client.get_messages could stop early in some channels.
• client.download_profile_photo now should work even if User.min.
• client.disconnect should no longer hang when being called from within an event handlers.
• client.get_dialogs now initializes the update state for channels.
• The message sender should not need to be fetched in more cases.
• Lowered the severity of some log messages to be less spammy.
These are new to v1.26.0:
• Layer update.
• New documented RPC errors.
• Sometimes the first message update to a channel could be missed if said message was read immediately.
• client.get_dialogs would fail when the total count evenly divided the chunk size of 100.
• client.get_messages could get stuck during a global search.
• Potentially fixed some issues when sending certain videos.
• Update handling should be more resilient.
• The client should handle having its auth key destroyed more gracefully.
• Fixed some issues when logging certain messages.
This version should fix some of the problems that came with the revamped update handling.
• Some inline URLs were not parsing correctly with markdown.
• events.Raw was handling UpdateShort which it shouldn’t do.
• events.Album should now work again.
• CancelledError was being incorrectly logged as a fatal error.
• Some fixes to update handling primarly aimed for bot accounts.
• Update handling now can deal with more errors without crashing.
• Unhandled errors from update handling will now be propagated through client.
run_until_disconnected.
• Invite links with + are now recognized.
• Added new known RPC errors.
• telethon.types could not be used as a module.
• 0-length message entities are now stripped to avoid errors.
• client.send_message was not returning a message with reply_to in some cases.
• aggressive in client.iter_participants now does nothing (it did not really work anymore any-
way, and this should prevent other errors).
• client.iter_participants was failing in some groups.
• Text with HTML URLs could sometimes fail to parse.
• Added a hard timeout during disconnect in order to prevent the program from freezing.
Please be sure to report issues with update handling if you still encounter some errors!
I had plans to release v2 way earlier, but my motivation drained off, so that didn’t happen. The reason for another v1
release is that there was a clear need to fix some things regarding update handling (which were present in v2). I did not
want to make this release. But with the release date for v2 still being unclear, I find it necessary to release another v1
version. I apologize for the delay (I should’ve done this a lot sooner but didn’t because in my head I would’ve pushed
through and finished v2, but I underestimated how much work that was and I probably experienced burn-out).
I still don’t intend to make new additions to the v1 series (beyond updating the Telegram layer being used). I still have
plans to finish v2 some day. But in the meantime, new features, such as reactions, will have to be used through raw
API.
This update also backports the update overhaul from v2. If you experience issues with updates, please report them on
the GitHub page for the project. However, this new update handling should be more reliable, and catch_up should
actually work properly.
Breaking Changes
• In order for catch_up to work (new flag in the TelegramClient constructor), sessions need to impleemnt
the new get_update_states. Third-party session storages won’t have this implemented by the time this
version released, so catch_up may not work with those.
This is a rushed release. It contains a layer recent enough to not fail with UPDATE_APP_TO_LOGIN, but still not the
latest, to avoid breaking more than necessary.
Breaking Changes
• The biggest change is user identifiers (and chat identifiers, and others) now use up to 64 bits, rather than 32. If
you were storing them in some storage with fixed size, you may need to update (such as database tables storing
only integers).
There have been other changes which I currently don’t have the time to document. You can refer to the following link
to see them early: https://github.com/LonamiWebs/Telethon/compare/v1.23.0. . . v1.24.0
Enhancements
Bug fixes
Enhancements
Bug fixes
Additions
• Message.click() now supports a password parameter, needed when doing things like changing the
owner of a bot via @BotFather.
Enhancements
Bug fixes
• Message.edit wasn’t working in your own chat on events other than NewMessage.
• client.delete_dialog() was not working on chats.
• events.UserUpdate should now handle channels’ typing status.
• InputNotifyPeer auto-cast should now work on other TLObject.
• For some objects, False was not correctly serialized.
This code will leave a comment to the channel post with ID 1134 in channel.
In addition, the library now logs warning or error messages to stderr by default! You no longer should be left
wondering “why isn’t my event handler working” if you forgot to configure logging. It took so long for this change to
arrive because nobody noticed that Telethon was using a logging.NullHandler when it really shouldn’t have.
If you want the old behaviour of no messages being logged, you can configure logging to CRITICAL severity:
import logging
logging.basicConfig(level=logging.CRITICAL)
This is not considered a breaking change because stderr should only be used for logging purposes, not to emit
information others may consume (use stdout for that).
Additions
Enhancements
Bug fixes
Telegram has had group calls for some weeks now. This new version contains the raw API methods needed to initiate
and manage these group calls, however, the library will likely not offer ways to stream audio directly.
Telethon’s focus is being an asyncio-based, pure-Python implementation to interact with Telegram’s API. Streaming
audio is beyond the current scope of the project and would be a big undertaking.
However, that doesn’t mean calls are not possible with Telethon. If you want to help design a Python library to perform
audio calls, which can then be used with Telethon (so you can use Telethon + that new library to perform calls with
Telethon), please refer to @pytgcallschat and join the relevant chat to discuss and help with the implementation!
The above message was also posted in the official Telegram group, if you wish to discuss it further.
With that out of the way, let’s list the additions and bug fixes in this release:
Additions
Enhancements
Bug fixes
Mostly fixes, and added some new things that can be done in this new layer.
For proxy users, a pull request was merged that will use the python-socks library when available for proxy support.
This library natively supports asyncio, so it should work better than the old pysocks. pysocks will still be used
if the new library is not available, and both will be handled transparently by Telethon so you don’t need to worry about
it.
Additions
• New client.set_proxy() method which lets you change the proxy without recreating the client. You
will need to reconnect for it to take effect, but you won’t need to recreate the client. This is also an external
contribution.
Enhancements
Bug fixes
New minor version, new layer change! This time is a good one to remind every consumer of Python libraries that you
should always specify fixed versions of your dependencies! If you’re using a requirements.txt file and you
want to stick with the old version (or any version) for the time being, you can use the following syntax:
telethon~=1.16.0
This will install any version compatible with the written version (so, any in the 1.16 series). Patch releases will never
break your code (and if they do, it’s a bug). You can also use that syntax in pip install. Your code can’t know
what new versions will look like, so saying it will work with all versions is a lie and will cause issues.
The reason to bring this up is that Telegram has changed things again, and with the introduction of anonymous admin-
istrators and channel comments, the sender of a message may not be a User! To accomodate for this, the field is now
a Peer and not int. As a reminder, it’s always a good idea to use Telethon’s friendly methods and custom properties,
which have a higher stability guarantee than accessing raw API fields.
Even if you don’t update, your code will still need to account for the fact that the sender of a message might be one
of the accounts Telegram introduced to preserve backwards compatibility, because this is a server-side change, so it’s
better to update and not lag behind. As it’s mostly just a single person driving the project on their free time, bug-fixes
are not backported.
This version also updates the format of SQLite sessions (the default), so after upgrading and using an old session, the
session will be updated, which means trying to use it back in older versions of the library won’t work.
For backwards-compatibility sake, the library has introduced the properties Message.reply_to_msg_id and
Message.to_id that behave like they did before (Telegram has renamed and changed how these fields work).
Breaking Changes
• Message.from_id is now a Peer, not int! If you want the marked sender ID (much like old behaviour),
replace all uses of .from_id with .sender_id. This will mostly work, but of course in old and new versions
you have to account for the fact that this sender may no longer be a user.
• You can no longer assign to Message.reply_to_msg_id and Message.to_id because these are now
properties that offer a “view” to the real value from a different field.
• Answering inline queries with a photo or document will now send the photo or document used in the result-
ing message by default. Not sending the media was technically a bug, but some people may be relying on this
old behaviour. You can use the old behaviour with include_media=False.
Additions
• New raise_last_call_error parameter in the client constructor to raise the same error produced by the
last failing call, rather than a generic ValueError.
• New formatting_entities parameter in client.send_message(), and client.send_file()
to bypass the parse mode and manually specify the formatting entities.
• New client.get_permissions() method to query a participant’s permissions in a group or channel.
This request is slightly expensive in small group chats because it has to fetch the entire chat to check just a user,
so use of a cache is advised.
• Message.click() now works on normal polls!
• New local_addr parameter in the client constructor to use a specific local network address when connecting
to Telegram.
• client.inline_query() now lets you specify the chat where the query is being made from, which some
bots need to provide certain functionality.
• You can now get comments in a channel post with the reply_to parameter in client.
iter_messages(). Comments are messages that “reply to” a specific channel message, hence the name
(which is consistent with how Telegram’s API calls it).
Enhancements
• Old usernames are evicted from cache, so getting entities by cached username should now be more reliable.
• Slightly less noisy logs.
• Stability regarding transport-level errors (transport flood, authorization key not found) should be improved. In
particular, you should no longer be getting unnecessarily logged out.
• Reconnection should no longer occur if the client gets logged out (for example, another client revokes the
session).
Bug fixes
• In some cases, there were issues when using events.Album together with events.Raw.
• For some channels, one of their channel photos would not show up in client.iter_profile_photos().
• In some cases, a request that failed to be sent would be forgotten, causing the original caller to be “locked”
forever for a response that would never arrive. Failing requests should now consistently be automatically re-
sent.
• The library should more reliably handle certain updates with “empty” data.
• Sending documents in inline queries should now work fine.
• Manually using client.sign_up should now work correctly, instead of claiming “code invalid”.
Special mention to some of the other changes in the 1.16.x series:
• The thumb for download_media now supports both str and VideoSize.
• Thumbnails are sorted, so -1 is always the largest.
The last release added support to force_file on any media, including things that were not possible before like
.webp files. However, the force_document toggle commonly used for photos was applied “twice” (one told the
library to send it as a document, and then to send that document as file), which prevented Telegram for analyzing the
images. Long story short, sending files to the stickers bot stopped working, but that’s been fixed now, and sending
photos as documents include the size attribute again as long as Telegram adds it.
Enhancements
• When trying to client.start() to another account if you were previously logged in, the library will now
warn you because this is probably not intended. To avoid the warning, make sure you’re logging in to the right
account or logout from the other first.
• Sending a copy of messages with polls will now work when possible.
• The library now automatically retries on inter-dc call errors (which occur when Telegram has internal issues).
Bug Fixes
The newest Telegram update has a new method to also retrieve megagroup statistics, which can now be used with
client.get_stats(). This way you’ll be able to access the raw data about your channel or megagroup statistics.
The maximum file size limit has also been increased to 2GB on the server, so you can send even larger files.
Breaking Changes
• Besides the obvious layer change, the loop argument is now ignored. It has been deprecated since Python 3.8
and will be removed in Python 3.10, and also caused some annoying warning messages when using certain parts
of the library. If you were (incorrectly) relying on using a different loop from the one that was set, things may
break.
Enhancements
• client.upload_file() now works better when streaming files (anything that has a .read()), instead
of reading it all into memory when possible.
Published at 2020/07/04
The library now has a friendly method to perform QR-login, as detailed in https://core.telegram.org/api/qr-login. It
won’t generate QR images, but it provides a way for you to easily do so with any other library of your choice.
Additions
• New client.qr_login().
• message.click now lets you click on buttons requesting phone or location.
Enhancements
Bug fixes
Published at 2020/05/26
Some nice things that were missing, along with the usual bug-fixes.
Additions
Bug fixes
Enhancements
Published at 2020/04/25
Bug fixes
Published at 2020/04/20
Once again nothing major, but a few bug fixes and primarily the new layer deserves a new minor release.
Bug fixes
Published at 2020/02/20
It has been a while since the last release, and a few bug fixes have been made since then. This release includes them
and updates the scheme layer.
Note that most of the bug-fixes are available in the v1.10.10 patch.
Bug fixes
Enhancements
Published at 2019/09/08
You can now schedule messages to be sent (or edited, or forwarded. . . ) at a later time, which can also work as
reminders for yourself when used in your own chat!
# Remind yourself to walk the dog in 10 minutes (after you play with Telethon's
˓→update)
Additions
• New Button.auth friendly button you can use to ask users to login to your bot.
• Telethon’s repository now contains *.nix expressions that you can use.
• New client.kick_participant() method to truly kick (not ban) participants.
• New schedule parameter in client.send_message(), client.edit_message(), client.
forward_messages() and client.send_file().
Bug fixes
Enhancements
Published at 2019/07/06
With the layer 103, Telethon is now able to send and receive animated stickers! These use the 'application/
x-tgsticker' mime-type and for now, you can access its raw data, which is a gzipped JSON.
Additions
Bug fixes
original_fwd = message.forward.original_fwd.original_fwd.original_fwd.original_
˓→fwd.original_fwd.original_fwd
Enhancements
Published at 2019/05/30
The documentation has been completely reworked from the ground up, with awesome new quick references such as
Client Reference to help you quickly find what you need!
Raw methods also warn you when a friendly variant is available, so that you don’t accidentally make your life harder
than it has to be.
In addition, all methods in the client now are fully annotated with type hints! More work needs to be done, but this
should already help a lot when using Telethon from any IDEs.
You may have noticed that the patch versions between v1.7.2 to v1.7.7 have not been documented. This is
because patch versions should only contain bug fixes, no new features or breaking changes. This hasn’t been the case
in the past, but from now on, the library will try to adhere more strictly to the Semantic Versioning principles.
If you ever want to look at those bug fixes, please use the appropriated git command, such as git shortlog
v1.7.1...v1.7.4, but in general, they probably just fixed your issue.
With that out of the way, let’s look at the full change set:
Breaking Changes
• The layer changed, so take note if you use the raw API, as it’s usual.
• The way photos are downloaded changed during the layer update of the previous version, and fixing that bug as a
breaking change in itself. client.download_media() now offers a different way to deal with thumbnails.
Additions
• New Message.file property! Now you can trivially access message.file.id to get the file ID of some
media, or even print(message.file.name).
• Archiving dialogs with Dialog.archive() or client.edit_folder() is now possible.
• New cleaned-up method to stream downloads with client.iter_download(), which offers a lot of flex-
ibility, such as arbitrary offsets for efficient seeking.
• Dialog.delete() has existed for a while, and now client.delete_dialog() exists too so you can
easily leave chats or delete dialogs without fetching all dialogs.
• Some people or chats have a lot of profile photos. You can now iterate over all of them with the new client.
iter_profile_photos() method.
• You can now annoy everyone with the new Message.pin(notify=True)! The client has its own variant
too, called client.pin_message().
Bug fixes
• Database is now upgraded if the version was lower, not different. From now on, this should help with upgrades
and downgrades slightly.
• Fixed saving pts and session-related stuff.
• Disconnection should not raise any errors.
• Invite links of the form tg://join?invite= now work.
• client.iter_participants(search=...) now works on private chats again.
• Iterating over messages in reverse with a date as offset wouldn’t work.
• The conversation would behave weirdly when a timeout occurred.
Enhancements
• telethon now re-export all the goodies that you commonly need when using the library, so e.g. from
telethon import Button will now work.
• telethon.sync now re-exports everything from telethon, so that you can trivially import from just one
place everything that you need.
• More attempts at reducing CPU usage after automatically fetching missing entities on events. This isn’t a big
deal, even if it sounds like one.
• Hexadecimal invite links are now supported. You didn’t need them, but they will now work.
Internal Changes
Published at 2019/04/24
Telegram changed the way thumbnails (which includes photos) are downloaded, so you can no longer use a PhotoSize
alone to download a particular thumbnail size (this is a breaking change).
Instead, you will have to specify the new thumb parameter in client.download_media() to download a par-
ticular thumbnail size. This addition enables you to easily download thumbnails from documents, something you
couldn’t do easily before.
Published at 2019/04/22
If you have been using Telethon for a while, you probably know how annoying the “Could not find the input entity
for. . . ” error can be. In this new version, the library will try harder to find the input entity for you!
That is, instead of doing:
@client.on(events.NewMessage)
async def handler(event):
await client.download_profile_photo(await event.get_input_sender())
# ...... needs await, it's a method ^^^^^ ^^
A lot of people use IDs thinking this is the right way of doing it. Ideally, you would always use input_*, not
sender or sender_id (and the same applies to chats). But, with this change, IDs will work just the same as
input_* inside events.
This feature still needs some more testing, so please do open an issue if you find strange behaviour.
Breaking Changes
• The layer changed, and a lot of things did too. If you are using raw API, you should be careful with this. In
addition, some attributes weren’t of type datetime when they should be, which has been fixed.
• Due to the layer change, you can no longer download photos with just their PhotoSize. Version 1.7.1 introduces
a new way to download thumbnails to work around this issue.
• client.disconnect() is now asynchronous again. This means you need to await it. You don’t need to
worry about this if you were using with client or client.run_until_disconnected. This should
prevent the “pending task was destroyed” errors.
Additions
• New in-memory cache for input entities. This should mean a lot less of disk look-ups.
• New client.action method to easily indicate that you are doing some chat action:
async with client.action(chat, 'typing'):
await asyncio.sleep(2) # type for 2 seconds
await client.send_message(chat, 'Hello world! I type slow ^^')
You can also easily use this for sending files, playing games, etc.
New bugs
Bug fixes
Enhancements
Published at 2019/02/27
First things first, sorry for updating the layer in the previous patch version. That should only be done between major
versions ideally, but due to how Telegram works, it’s done between minor versions. However raw API has and will
always be considered “unsafe”, this meaning that you should always use the convenience client methods instead. These
methods don’t cover the full API yet, so pull requests are welcome.
Breaking Changes
• The layer update, of course. This didn’t really need a mention here.
• You can no longer pass a batch_size when iterating over messages. No other method exposed this parameter,
and it was only meant for testing purposes. Instead, it’s now a private constant.
• client.iter_* methods no longer have a _total parameter which was supposed to be private anyway.
Instead, they return a new generator object which has a .total attribute:
it = client.iter_messages(chat)
for i, message in enumerate(it, start=1):
percentage = i / it.total
print('{:.2%} {}'.format(percentage, message.text))
Additions
• You can now pass phone and phone_code_hash in client.sign_up, although you probably don’t need
that.
• Thanks to the overhaul of all client.iter_* methods, you can now do:
Bug fixes
• Fix telethon.utils.resolve_bot_file_id, which wasn’t working after the layer update (so you
couldn’t send some files by bot file IDs).
• Fix sending albums as bot file IDs (due to image detection improvements).
• Fix takeout() failing when they need to download media from other DCs.
• Fix repeatedly calling conversation.get_response() when many messages arrived at once (i.e. when
several of them were forwarded).
• Fixed connecting with ConnectionTcpObfuscated.
• Fix client.get_peer_id('me').
• Fix warning of “missing sqlite3” when in reality it just had wrong tables.
• Fix a strange error when using too many IDs in client.delete_messages().
• Fix client.send_file with the result of client.upload_file.
• When answering inline results, their order was not being preserved.
• Fix events.ChatAction detecting user leaves as if they were kicked.
Enhancements
Internal changes
Published at 2019/01/14
There isn’t an entry for v1.5.4 because it contained only one hot-fix regarding loggers. This update is slightly bigger
so it deserves mention.
Additions
Bug fixes
• Dealing with mimetypes should cause less issues in systems like Windows.
• Potentially fix alternative session storages that had issues with dates.
Enhancements
Published at 2019/01/14
Several bug fixes and some quality of life enhancements.
Breaking Changes
• message.edit now respects the previous message buttons or link preview being hidden. If you want to
toggle them you need to explicitly set them. This is generally the desired behaviour, but may cause some bots
to have buttons when they shouldn’t.
Additions
• You can now “hide_via” when clicking on results from client.inline_query to @bing and @gif.
• You can now further configure the logger Telethon uses to suit your needs.
Bug fixes
Enhancements
Published at 2019/01/05
You can now easily start takeout sessions (also known as data export sessions) through client.takeout(). Some
of the requests will have lower flood limits when done through the takeout session.
Bug fixes
Published at 2019/01/03
The library already had a way to easily convert the objects the API returned into dictionaries through object.
to_dict(), but some of the fields are dates or bytes which JSON can’t serialize directly.
For convenience, a new object.to_json() has been added which will by default format both of those problematic
types into something sensible.
Additions
Bug fixes
Enhancements
• You can now configure the reply markup when using Button as a bot.
• More properties for Message to make accessing media convenient.
• Downloading to file=bytes will now return a bytes object with the downloaded media.
Published at 2018/12/25
This version doesn’t really bring many new features, but rather focuses on updating the code base to support the latest
available Telegram layer, 91. This layer brings polls, and you can create and manage them through Telethon!
Breaking Changes
• The layer change from 82 to 91 changed a lot of things in the raw API, so be aware that if you rely on
raw API calls, you may need to update your code, in particular if you work with files. They have a new
file_reference parameter that you must provide.
Additions
Bug fixes
• Markdown and HTML parsing now behave correctly with leading whitespace.
• HTTP connection should now work correctly again.
• Using caption=None would raise an error instead of setting no caption.
• KeyError is now handled properly when forwarding messages.
• button.click() now works as expected for KeyboardButtonGame.
Enhancements
• Some improvements to the search in the full API and generated examples.
• Using entities with access_hash = 0 will now work in more cases.
Internal changes
Published at 2018/12/04
While this may seem like a minor thing, it’s a big usability improvement.
Anyone who wants to update the documentation for known errors, or whether some methods can be used as a bot, user
or both, can now be easily edited. Everyone is encouraged to help document this better!
Bug fixes
Enhancements
• Accessing events.ChatAction properties such as input users may now work in more cases.
Internal changes
• Error descriptions and information about methods is now loaded from a CSV file instead of being part of several
messy JSON files.
Published at 2018/11/24
This version also includes the v1.4.1 hot-fix, which was a single quick fix and didn’t really deserve an entry in the
changelog.
Bug fixes
Enhancements
Published at 2018/11/03
Yet again, a lot of work has been put into reworking the low level connection classes. This means asyncio.
open_connection is now used correctly and the errors it can produce are handled properly. The separation be-
tween packing, encrypting and network is now abstracted away properly, so reasoning about the code is easier, making
it more maintainable.
As a user, you shouldn’t worry about this, other than being aware that quite a few changes were made in the insides of
the library and you should report any issues that you encounter with this version if any.
Breaking Changes
• The threaded version of the library will no longer be maintained, primarily because it never was properly main-
tained anyway. If you have old code, stick with old versions of the library, such as 0.19.1.6.
• Timeouts no longer accept timedelta. Simply use seconds.
• The callback parameter from telethon.tl.custom.button.Button.inline() was removed,
since it had always been a bad idea. Adding the callback there meant a lot of extra work for every message sent,
and only registering it after the first message was sent! Instead, use telethon.events.callbackquery.
CallbackQuery.
Additions
Bug fixes
• Fixed Conversation.wait_event().
• Fixed replying with photos/documents on inline results.
• client.is_user_authorized() now works correctly after client.log_out().
• dialog.is_group now works for ChatForbidden.
• Not using async with when needed is now a proper error.
• events.CallbackQuery with string regex was not working properly.
• client.get_entity('me') now works again.
• Empty codes when signing in are no longer valid.
• Fixed file cache for in-memory sessions.
Enhancements
Published at 2018/09/22
If you have worked with Flask templates, you will love this update, since it gives you the same features but even more
conveniently:
# handlers/welcome.py
from telethon import events
@events.register(events.NewMessage('(?i)hello'))
async def handler(event):
client = event.client
await event.respond('Hi!')
await client.send_message('me', 'Sent hello to someone')
This will register the handler callback to handle new message events. Note that you didn’t add this to any client
yet, and this is the key point: you don’t need a client to define handlers! You can add it later:
# main.py
from telethon import TelegramClient
import handlers.welcome
This should help you to split your big code base into a more modular design.
Breaking Changes
• .sender is the .chat when the message is sent in a broadcast channel. This makes sense, because the sender
of the message was the channel itself, but you now must take into consideration that it may be either a User or
Channel instead of being None.
Additions
• New MultiError class when invoking many requests at once through client([requests]).
• New custom func= on all events. These will receive the entire event, and a good usage example is
func=lambda e: e.is_private.
• New .web_preview field on messages. The .photo and .document will also return the media in the web
preview if any, for convenience.
• Callback queries now have a .chat in most circumstances.
Bug fixes
• Running code with python3 -O would remove critical code from asserts.
• Fix some rare ghost disconnections after reconnecting.
• Fix strange behavior for send_message(chat, Message, reply_to=foo).
• The loop= argument was being pretty much ignored.
• Fix MemorySession file caching.
• The logic for getting entities from their username is now correct.
• Fixes for sending stickers from .webp files in Windows, again.
• Fix disconnection without being logged in.
• Retrieving media from messages would fail.
• Getting some messages by ID on private chats.
Enhancements
• iter_participants will now use its search= as a symbol set when aggressive=True, so you can
do client.get_participants(group, aggressive=True, search='').
• The StringSession supports custom encoding.
• Callbacks for telethon.client.auth.AuthMethods.start can be async.
Internal changes
• Cherry-picked a commit to use asyncio.open_connection in the lowest level of the library. Do open
issues if this causes trouble, but it should otherwise improve performance and reliability.
• Building and resolving events overhaul.
Published at 2018/08/14
This is a big release! Quite a few things have been added to the library, such as the new Conversation. This makes
it trivial to get tokens from @BotFather:
In addition to that, you can now easily load and export session files without creating any on-disk file thanks to the
StringSession:
Additions
• Bot-API-style file_id can now be used to send files and download media. You can also access telethon.
utils.resolve_bot_file_id and telethon.utils.pack_bot_file_id to resolve and create
these file IDs yourself. Note that each user has its own ID for each file so you can’t use a bot’s file_id with
your user, except stickers.
• New telethon.utils.get_peer, useful when you expect a Peer.
Bug fixes
Enhancements
• Updated documentation.
• Invite links will now use cache, so using them as entities is cheaper.
• You can reuse message buttons to send new messages with those buttons.
• .to_dict() will now work even on invalid TLObject’s.
Published at 2018/07/23
The custom.Message class has been rewritten in a cleaner way and overall feels less hacky in the library. This
should perform better than the previous way in which it was patched.
The release is primarily intended to test this big change, but also fixes Python 3.5.2 compatibility which was broken
due to a trailing comma.
Bug fixes
• Using functools.partial on event handlers broke updates if they had uncaught exceptions.
• A bug under some session files where the sender would export authorization for the same data center, which is
unsupported.
• Some logical bugs in the custom message class.
Published at 2018/07/21
Two new event handlers to ease creating normal bots with the library, namely events.InlineQuery and
events.CallbackQuery for handling @InlineBot queries or reacting to a button click. For this second
option, there is an even better way:
bot.send_message(chat, 'Hello!',
buttons=Button.inline('Click me', callback))
You can directly pass the callback when creating the button.
This is fine for small bots but it will add the callback every time you send a message, so you probably should do this
instead once you are done testing:
And yes, you can create more complex button layouts with lists:
@bot.on(events.CallbackQuery)
async def handler(event):
global phone
if event.data == b'<':
phone = phone[:-1]
else:
phone += event.data.decode('utf-8')
markup = bot.build_reply_markup([
[Button.inline('1'), Button.inline('2'), Button.inline('3')],
[Button.inline('4'), Button.inline('5'), Button.inline('6')],
[Button.inline('7'), Button.inline('8'), Button.inline('9')],
[Button.inline('+'), Button.inline('0'), Button.inline('<')],
])
bot.send_message(chat, 'Enter a phone', buttons=markup)
(Yes, there are better ways to do this). Now for the rest of things:
Additions
• New custom.Button class to help you create inline (or normal) reply keyboards. You must sign in as a bot
to use the buttons= parameters.
• New events usable if you sign in as a bot: events.InlineQuery and events.CallbackQuery.
• New silent parameter when sending messages, usable in broadcast channels.
• Documentation now has an entire section dedicate to how to use the client’s friendly methods at (removed broken
link).
Bug fixes
• Empty except are no longer used which means sending a keyboard interrupt should now work properly.
• The pts of incoming updates could be None.
• UTC timezone information is properly set for read datetime.
• Some infinite recursion bugs in the custom message class.
• Updates was being dispatched to raw handlers when it shouldn’t.
• Using proxies and HTTPS connection mode may now work properly.
• Less flood waits when downloading media from different data centers, and the library will now detect them even
before sending requests.
Enhancements
Internal changes
• When downloading media, the right sender is directly used without previously triggering migrate errors.
• Code reusing for getting the chat and the sender, which easily enables this feature for new types.
Published at 2018/07/09
This release implements the HTTP connection mode to the library, which means certain proxies that only allow HTTP
connections should now work properly. You can use it doing the following, like any other mode:
Additions
Bug fixes
Enhancements
Internal changes
• Outgoing TLMessage are now pre-packed so if there’s an error when serializing the raw requests, the library
will no longer swallow it. This also means re-sending packets doesn’t need to re-pack their bytes.
Published at 2018/07/04
Mostly bug fixes, but now there is a new parameter on client.iter_messages to support reversing the order in
which messages are returned.
Additions
Bug fixes
• Signing in required a named code= parameter, but usage without a name was really widespread so it has been
reverted.
Published at 2018/06/28
Updated some asserts and parallel downloads, as well as some fixes for sync.
Published at 2018/06/27
And as usual, every major release has a few bugs that make the library unusable! This quick update should fix those,
namely:
Bug fixes
• client.start() was completely broken due to a last-time change requiring named arguments everywhere.
• Since the rewrite, if your system clock was wrong, the connection would get stuck in an infinite “bad message”
loop of responses from Telegram.
• Accessing the buttons of a custom message wouldn’t work in channels, which lead to fix a completely different
bug regarding starting bots.
• Disconnecting could complain if the magic telethon.sync was imported.
• Successful automatic reconnections now ask Telegram to send updates to us once again as soon as the library is
ready to listen for them.
Published at 2018/06/27
Important: If you come from Telethon pre-1.0 you really want to read Compatibility and Convenience to port your
scripts to the new version.
The library has been around for well over a year. A lot of improvements have been made, a lot of user complaints have
been fixed, and a lot of user desires have been implemented. It’s time to consider the public API as stable, and remove
some of the old methods that were around until now for compatibility reasons. But there’s one more surprise!
There is a new magic telethon.sync module to let you use all the methods in the TelegramClient (and the types
returned from its functions) in a synchronous way, while using asyncio behind the scenes! This means you’re now
able to do both of the following:
import asyncio
asyncio.get_event_loop().run_until_complete(main())
Both ways can coexist (you need to await if the loop is running).
You can also use the magic sync module in your own classes, and call sync.syncify(cls) to convert all their
async def into magic variants.
Breaking Changes
Additions
Convenience at its maximum! You can even chain the .start() method since it returns the instance of the
client:
Bug fixes
• There were some @property async def left, and some await property.
• “User joined” event was being treated as “User was invited”.
• SQLite’s cursor should not be closed properly after usage.
• await the updates task upon disconnection.
• Some bug in Python 3.5.2’s asyncio causing 100% CPU load if you forgot to call client.
disconnect(). The method is called for you on object destruction, but you still should disconnect manually
or use a with block.
• Some fixes regarding disconnecting on client deletion and properly saving the authorization key.
• Passing a class to message.get_entities_text now works properly.
• Iterating messages from a specific user in private messages now works.
Enhancements
Published at 2018/06/24
This version is a major overhaul of the library internals. The core has been rewritten, cleaned up and refactored to fix
some oddities that have been growing inside the library.
This means that the code is easier to understand and reason about, including the code flow such as conditions, excep-
tions, where to reconnect, how the library should behave, and separating different retry types such as disconnections
or call fails, but it also means that some things will necessarily break in this version.
All requests that touch the network are now methods and need to have their await (or be ran until their completion).
Also, the library finally has the simple logo it deserved: a carefully hand-written .svg file representing a T following
Python’s colours.
Breaking Changes
• If you relied on internals like the MtProtoSender and the TelegramBareClient, both are gone. They
are now MTProtoSender and TelegramBaseClient and they behave differently.
• Underscores have been renamed from filenames. This means telethon.errors.rpc_error_list
won’t work, but you should have been using telethon.errors all this time instead.
• client.connect no longer returns True on success. Instead, you should except the possible
ConnectionError and act accordingly. This makes it easier to not ignore the error.
• You can no longer set retries=n when calling a request manually. The limit works differently now, and it’s
done on a per-client basis.
• Accessing .sender, .chat and similar may not work in events anymore, since previously they could ac-
cess the network. The new rule is that properties are not allowed to make API calls. You should use .
get_sender(), .get_chat() instead while using events. You can safely access properties if you get
messages through client.get_messages() or other methods in the client.
• The above point means reply_message is now .get_reply_message(), and fwd_from_entity
is now get_fwd_sender(). Also forward was gone in the previous version, and you should be using
fwd_from instead.
Additions
• Telegram’s Terms Of Service are now accepted when creating a new account. This can possibly help avoid bans.
This has no effect for accounts that were created before.
• The method reference now shows which methods can be used if you sign in with a bot_token.
• There’s a new client.disconnected future which you can wait on. When a disconnection occurs, you
will now, instead letting it happen in the background.
• More configurable retries parameters, such as auto-reconnection, retries when connecting, and retries when
sending a request.
• You can filter events.NewMessage by sender ID, and also whether they are forwards or not.
• New ignore_migrated parameter for client.iter_dialogs.
Bug fixes
Enhancements
• You can now delete over 100 messages at once with client.delete_messages.
• Signing in now accounts for AuthRestartError itself, and also handles
PasswordHashInvalidError.
• __all__ is now defined, so from telethon import * imports sane defaults (client, events and utils).
This is however discouraged and should be used only in quick scripts.
• pathlib.Path is now supported for downloading and uploading media.
• Messages you send to yourself are now considered outgoing, unless they are forwarded.
• The documentation has been updated with a brand new asyncio crash course to encourage you use it. You
can still use the threaded version if you want though.
• .name property is now properly supported when sending and downloading files.
• Custom parse_mode, which can now be set per-client, support MessageEntityMentionName so you can return
those now.
• The session file is saved less often, which could result in a noticeable speed-up when working with a lot of
incoming updates.
Internal changes
• The flow for sending a request is as follows: the TelegramClient creates a MTProtoSender with a
Connection, and the sender starts send and receive loops. Sending a request means enqueueing it in the
sender, which will eventually pack and encrypt it with its ConnectionState instead of using the entire
Session instance. When the data is packed, it will be sent over the Connection and ultimately over the
TcpClient.
• Reconnection occurs at the MTProtoSender level, and receiving responses follows a similar process, but now
asyncio.Future is used for the results which are no longer part of all TLObject, instead are part of the
TLMessage which simplifies things.
• Objects can no longer be content_related and instead subclass TLRequest, making the separation of
concerns easier.
• The TelegramClient has been split into several mixin classes to avoid having a 3,000-lines-long file with
all the methods.
• More special cases in the MTProtoSender have been cleaned up, and also some attributes from the Session
which didn’t really belong there since they weren’t being saved.
• The telethon_generator/ can now convert .tl files into .json, mostly as a proof of concept, but it
might be useful for other people.
Published at 2018/06/03
Refer to its documentation to see all you can do, again, click telethon.tl.custom.message.Message to go
to its page.
Breaking Changes
Additions
Bug fixes
• Some IDs start with 1000 and these would be wrongly treated as channels.
• Some short usernames like @vote were being ignored.
• telethon.telegram_client.TelegramClient.iter_messages’s from_user was failing if
no filter had been set.
• telethon.telegram_client.TelegramClient.iter_messages’s min_id/max_id was be-
ing ignored by Telegram. This is now worked around.
• telethon.telegram_client.TelegramClient.catch_up would fail with empty states.
• telethon.events.newmessage.NewMessage supports incoming=False to indicate
outgoing=True.
Enhancements
• You can now send multiple requests at once while preserving the order:
Internal changes
Published at 2018/05/07
This update prepares the library for catching up with updates with the new telethon.telegram_client.
TelegramClient.catch_up method. This feature needs more testing, but for now it will let you “catch up”
on some old updates that occurred while the library was offline, and brings some new features and bug fixes.
Additions
Bug fixes
Enhancements
• Retry automatically on RpcCallFailError. This error happened a lot when iterating over many messages,
and retrying often fixes it.
• Faster telethon.telegram_client.TelegramClient.iter_messages by sleeping only as
much as needed.
• telethon.telegram_client.TelegramClient.edit_message now supports omitting the entity
if you pass a Message.
• telethon.events.raw.Raw can now be filtered by type.
Internal changes
Published at 2018/04/15
Now you can use Python’s pickle module to serialize RPCError and any other TLObject thanks to @veg-
eta1k95! A fix that was fairly simple, but still might be useful for many people.
As a side note, the documentation at https://tl.telethon.dev now lists known RPCError for all requests, so you know
what to expect. This required a major rewrite, but it was well worth it!
Breaking changes
Additions
• New telethon.events.messageread.MessageRead event, to find out when and who read which
messages as soon as it happens.
• Now you can access .chat_id on all events and .sender_id on some.
Bug fixes
Enhancements
Internal changes
• The library won’t call .get_dialogs() on entity not found. Instead, it will raise ValueError() so
you can properly except it.
• Several new examples and updated documentation.
• py:obj is the default Sphinx’s role which simplifies .rst files.
• setup.py now makes use of python_requires.
• Events now live in separate files.
• Other minor changes.
Published at 2018/03/27
Just a few bug fixes before they become too many.
Additions
• Getting an entity by its positive ID should be enough, regardless of their type (whether it’s an User, a Chat or
a Channel). Although wrapping them inside a Peer is still recommended, it’s not necessary.
• New client.edit_2fa function to change your Two Factor Authentication settings.
• .stringify() and string representation for custom Dialog/Draft.
Bug fixes
Published at 2018/03/17
All the .get_ methods in the TelegramClient now have a .iter_ counterpart, so you can do operations while
retrieving items from them. For instance, you can client.iter_dialogs() and break once you find what
you’re looking for instead fetching them all at once.
Another big thing, you can get entities by just their positive ID. This may cause some collisions (although it’s very
unlikely), and you can (should) still be explicit about the type you want. However, it’s a lot more convenient and less
confusing.
Breaking changes
• The library only offers the default SQLiteSession again. See Session Files for more on how to use a different
storage from now on.
Additions
• Events now override __str__ and implement .stringify(), just like every other TLObject does.
• events.ChatAction now has respond(), reply() and delete() for the message that triggered it.
• client.iter_participants() (and its client.get_participants() counterpart) now expose
the filter argument, and the returned users also expose the .participant they are.
• You can now use client.remove_event_handler() and client.list_event_handlers()
similar how you could with normal updates.
• New properties on events.NewMessage, like .video_note and .gif to access only specific types of
documents.
• The Draft class now exposes .text and .raw_text, as well as a new Draft.send() to send it.
Bug fixes
Enhancements
• Less RPC are made when accessing the .sender and .chat of some events (mostly those that occur in a
channel).
• You can send albums larger than 10 items (they will be sliced for you), as well as mixing normal files with
photos.
• TLObject now have Python type hints.
Internal changes
Published at 2018/03/04
The Session’s have been revisited thanks to the work of @tulir and they now use an ABC so you can easily
implement your own!
The default will still be a SQLiteSession, but you might want to use the new AlchemySessionContainer if
you need. Refer to the section of the documentation on Session Files for more.
Breaking changes
Additions
Bug fixes
Internal changes
Published at 2018/02/24
Some new things and patches that already deserved their own release.
Additions
Bug fixes
Internal changes
Published at 2018/02/18
More bug fixes and a few others addition to make events easier to use.
Additions
Bug fixes
• bot_token wouldn’t work on .start(), and changes to password (now it will ask you for it if you don’t
provide it, as docstring hinted).
• .edit_message() was ignoring the formatting (e.g. markdown).
• Added missing case to the NewMessage event for normal groups.
• Accessing the .text of the NewMessage event was failing due to a bug with the markdown unparser.
Internal changes
• libssl is no longer an optional dependency. Use cryptg instead, which you can find on https://pypi.org/
project/cryptg/.
Published at 2018/02/15
Primarily bug fixing and a few welcomed additions.
Additions
Bug fixes
• Periodically send GetStateRequest automatically to keep the server sending updates even if you’re not
invoking any request yourself.
• HTML parsing was failing due to not handling surrogates properly.
• .sign_up was not accepting int codes.
• Whitelisting more than one chat on events wasn’t working.
Internal changes
Published at 2018/02/09
Of course there was more work to be done regarding updates, and it’s here! The library comes with a new events
module (which you will often import as from telethon import TelegramClient, events). This are
pretty much all the additions that come with this version change, but they are a nice addition. Refer to (removed
broken link) to get started with events.
Published at 2018/02/03
The library trusts the server with updates again. The library will not check for duplicates anymore, and when the
server kicks us, it will run GetStateRequest so the server starts sending updates again (something it wouldn’t do
unless you invoked something, it seems). But this update also brings a few more changes!
Additions
Enhancements
Bug fixes
Published at 2018/01/19
The TLObject’s (instances returned by the API and Request’s) have now acquired a new .resolve() method.
While this should be used by the library alone (when invoking a request), it means that you can now use Peer types
or even usernames where a InputPeer is required. The object now has access to the client, so that it can fetch
the right type if needed, or access the session database. Furthermore, you can reuse requests that need “autocast” (e.g.
you put User but InputPeer was needed), since .resolve() is called when invoking. Before, it was only done
on object construction.
Additions
Enhancements
Bug fixes
Internal changes
Published at 2018/01/11
The library is now using MtProto 2.0! This shouldn’t really affect you as an end user, but at least it means the library
will be ready by the time MtProto 1.0 is deprecated.
Additions
Bug fixes
• The library uses again only a single connection. Less updates are be dropped now, and the performance is even
better than using temporary connections.
• without rowid will only be used on the *.session if supported.
• Phone code hash is associated with phone, so you can change your mind when calling .sign_in().
Internal changes
• File cache now relies on the hash of the file uploaded instead its path, and is now persistent in the *.session
file. Report any bugs on this!
• Clearer error when invoking without being connected.
• Markdown parser doesn’t work on bytes anymore (which makes it cleaner).
Published at 2017/12/28
In the beginning, session files used to be pickle. This proved to be bad as soon as one wanted to add more fields. For
this reason, they were migrated to use JSON instead. But this proved to be bad as soon as one wanted to save things
like entities (usernames, their ID and hash), so now it properly uses sqlite3, which has been well tested, to save the
session files! Calling .get_input_entity using a username no longer will need to fetch it first, so it’s really 0
calls again. Calling .get_entity will always fetch the most up to date version.
Furthermore, nearly everything has been documented, thus preparing the library for Read the Docs (although there are
a few things missing I’d like to polish first), and the logging are now better placed.
Breaking changes
• .get_dialogs() now returns a single list instead a tuple consisting of a custom class that should make
everything easier to work with.
• .get_message_history() also returns a single list instead a tuple, with the Message instances modified
to make them more convenient.
Both lists have a .total attribute so you can still know how many dialogs/messages are in total.
Additions
Bug fixes
• Empty strings weren’t working when they were a flag parameter (e.g., setting no last name).
• Fix invalid assertion regarding flag parameters as well.
• Avoid joining the background thread on disconnect, as it would be None due to a race condition.
• Correctly handle None dates when downloading media.
• .download_profile_photo was failing for some channels.
• .download_media wasn’t handling Photo.
Internal changes
• date was being serialized as local date, but that was wrong.
• date was being represented as a float instead of an int.
• .tl parser wasn’t stripping inline comments.
• Removed some redundant checks on update_state.py.
• Use a synchronized queue instead a hand crafted version.
• Use signed integers consistently (e.g. salt).
• Always read the corresponding TLObject from API responses, except for some special cases still.
• A few more except low level to correctly wrap errors.
• More accurate exception types.
• invokeWithLayer(initConnection(X)) now wraps every first request after .connect().
As always, report if you have issues with some of the changes!
Published at 2017/11/16
It’s here, it has come! The library now supports IPv6! Just pass use_ipv6=True when creating a
TelegramClient. Note that I could not test this feature because my machine doesn’t have IPv6 setup. If you
know IPv6 works in your machine but the library doesn’t, please refer to #425.
Additions
• IPv6 support.
• New method to extract the text surrounded by MessageEntity’s, in the extensions.markdown module.
Enhancements
Bug fixes
Published at 2017/11/04
This update brings a few general enhancements that are enough to deserve a new release, with a new feature: beta
markdown-like parsing for .send_message()!
Additions
Bug fixes
• The list of known peers could end “corrupted” and have users with access_hash=None, resulting in
struct error for it not being an integer. You shouldn’t encounter this issue anymore.
• The warning for “added update handler but no workers set” wasn’t actually working.
• .get_input_peer was ignoring a case for InputPeerSelf.
• There used to be an exception when logging exceptions (whoops) on update handlers.
• “Downloading contacts” would produce strange output if they had semicolons (;) in their name.
• Fix some cyclic imports and installing dependencies from the git repository.
• Code generation was using f-strings, which are only supported on Python 3.6.
Internal changes
• The auth_key generation has been moved from .connect() to .invoke(). There were some issues
were .connect() failed and the auth_key was None so this will ensure to have a valid auth_key when
needed, even if BrokenAuthKeyError is raised.
• Support for higher limits on .get_history() and .get_dialogs().
• Much faster integer factorization when generating the required auth_key. Thanks @delivrance for making
me notice this, and for the pull request.
Published at 2017/10/20
Hopefully a very ungrateful bug has been removed. When you used to invoke some request through update handlers,
it could potentially enter an infinite loop. This has been mitigated and it’s now safe to invoke things again! A lot of
updates were being dropped (all those gzipped), and this has been fixed too.
More bug fixes include a correct parsing of certain TLObjects thanks to @stek29, and some wrong calls that would
cause the library to crash thanks to @andr-04, and the ReadThread not re-starting if you were already authorized.
Internally, the .to_bytes() function has been replaced with __bytes__ so now you can do
bytes(tlobject).
Published at 2017/10/14
This release primarly focuses on a few bug fixes and enhancements. Although more stuff may have broken along the
way.
Enhancements
Bug fixes
• .get_input_entity was failing for IDs and other cases, also making more requests than it should.
• Use basename instead abspath when sending a file. You can now also override the attributes.
• EntityDatabase.__delitem__ wasn’t working.
• .send_message() was failing with channels.
• .get_dialogs(limit=None) should now return all the dialogs correctly.
• Temporary fix for abusive duplicated updates.
Internal changes
Published at 2017/10/05
The main feature of this release is that Telethon now has a custom database for all the entities you encounter, instead
depending on @lru_cache on the .get_entity() method.
The EntityDatabase will, by default, cache all the users, chats and channels you find in memory for as long as
the program is running. The session will, by default, save all key-value pairs of the entity identifiers and their hashes
(since Telegram may send an ID that it thinks you already know about, we need to save this information).
You can prevent the EntityDatabase from saving users by setting client.session.entities.
enabled = False, and prevent the Session from saving input entities at all by setting client.session.
save_entities = False. You can also clear the cache for a certain user through client.session.
entities.clear_cache(entity=None), which will clear all if no entity is given.
Additions
Enhancements
Bug fixes
• .get_dialogs() doesn’t fail on Windows anymore, and returns the right amount of dialogs.
• GeneralProxyError should be passed to the main thread again, so that you can handle it.
Published at 2017/10/01
After hundreds of lines changed on a major refactor, it’s finally here. It’s the Updates Overhaul Update; let’s get
right into it!
Breaking changes
• .create_new_connection() is gone for good. No need to deal with this manually since new connections
are now handled on demand by the library itself.
Enhancements
• You can invoke requests from update handlers. And any other thread. A new temporary will be made, so
that you can be sending even several requests at the same time!
• Several worker threads for your updates! By default, None will spawn. I recommend you to work with
update_workers=4 to get started, these will be polling constantly for updates.
• You can also change the number of workers at any given time.
• The library can now run in a single thread again, if you don’t need to spawn any at all. Simply set
spawn_read_thread=False when creating the TelegramClient!
• You can specify limit=None on .get_dialogs() to get all of them[1].
• Updates are expanded, so you don’t need to check if the update has .updates or an inner .update any-
more.
• All InputPeer entities are saved in the session file, but you can disable this by setting
save_entities=False.
• New .get_input_entity method, which makes use of the above feature. You should use this when a
request needs a InputPeer, rather than the whole entity (although both work).
• Assert that either all or None dependent-flag parameters are set before sending the request.
• Phone numbers can have dashes, spaces, or parenthesis. They’ll be removed before making the request.
• You can override the phone and its hash on .sign_in(), if you’re creating a new TelegramClient on
two different places.
Bug fixes
• .log_out() was consuming all retries. It should work just fine now.
• The session would fail to load if the auth_key had been removed manually.
• Updates.check_error was popping wrong side, although it’s been completely removed.
• ServerError’s will be ignored, and the request will immediately be retried.
Internal changes
• TelegramClient is now only an abstraction over the TelegramBareClient, which can only do ba-
sic things, such as invoking requests, working with files, etc. If you don’t need any of the abstractions the
TelegramClient, you can now use the TelegramBareClient in a much more comfortable way.
• MtProtoSender is not thread-safe, but it doesn’t need to be since a new connection will be spawned when
needed.
• New connections used to be cached and then reused. Now only their sessions are saved, as temporary connec-
tions are spawned only when needed.
• Added more RPC errors to the list.
[1]: Broken due to a condition which should had been the opposite (sigh), fixed 4 commits ahead on https://github.
com/LonamiWebs/Telethon/commit/62ea77cbeac7c42bfac85aa8766a1b5b35e3a76c.
That’s pretty much it, although there’s more work to be done to make the overall experience of working with updates
even better. Stay tuned!
Published at 2017/09/29
Bug fixes
• Important, related to the serialization. Every object or request that had to serialize a True/False type was
always being serialized as false!
• Another bug that didn’t allow you to leave as None flag parameters that needed a list has been fixed.
Internal changes
• Other internal changes include a somewhat more readable .to_bytes() function and pre-computing the
flag instead using bit shifting. The TLObject.constructor_id has been renamed to TLObject.
CONSTRUCTOR_ID, and .subclass_of_id is also uppercase now.
Published at 2017/09/28
Version v0.14 had started working on the new .to_bytes() method to dump the BinaryWriter and its usage
on the .on_send() when serializing TLObjects, and this release finally removes it. The speed up when serializing
things to bytes should now be over twice as fast wherever it’s needed.
Bug fixes
• This version is again compatible with Python 3.x versions below 3.5 (there was a method call that was Python
3.5 and above).
Internal changes
• Using proper classes (including the generated code) for generating authorization keys and to write out
TLMessage’s.
Published at 2017/09/27
New major release, since I’ve decided that these two features are big enough:
Additions
• Requests larger than 512 bytes will be compressed through gzip, and if the result is smaller, this will be
uploaded instead.
• You can now send multiple requests at once, they’re simply *var_args on the .invoke(). Note that the
server doesn’t guarantee the order in which they’ll be executed!
Internally, another important change. The .on_send function on the TLObjects is gone, and now there’s a new .
to_bytes(). From my tests, this has always been over twice as fast serializing objects, although more replacements
need to be done, so please report any issues.
Enhancements
• Implemented .get_input_media helper methods. Now you can even use another message as input media!
Bug fixes
Published at 2017/09/23
Before getting any further, here’s a quick fix-up with things that should have been on v0.13.5 but were missed.
Specifically, the timeout when receiving a request will now work properly.
Some other additions are a tiny fix when handling updates, which was ignoring some of them, nicer __str__ and
.stringify() methods for the TLObject’s, and not stopping the ReadThread if you try invoking something
there (now it simply returns None).
Published at 2017/09/23
Yet another update to fix some bugs and increase the stability of the library, or, at least, that was the attempt!
This release should really improve the experience with the background thread that the library starts to read things
from the network as soon as it can, but I can’t spot every use case, so please report any bug (and as always, minimal
reproducible use cases will help a lot).
Bug fixes
Enhancements
• Type hinting for all the generated Request’s and TLObjects! IDEs like PyCharm will benefit from this.
• ProxyConnectionError should properly be passed to the main thread for you to handle.
• The background thread will only be started after you’re authorized on Telegram (i.e. logged in), and several
other attempts at polishing the experience with this thread.
• The Connection instance is only created once now, and reused later.
• Calling .connect() should have a better behavior now (like actually trying to connect even if we seemingly
were connected already).
• .reconnect() behavior has been changed to also be more consistent by making the assumption that we’ll
only reconnect if the server has disconnected us, and is now private.
Internal changes
• TLObject.__repr__ doesn’t show the original TL definition anymore, it was a lot of clutter. If you have
any complaints open an issue and we can discuss it.
• Internally, the '+' from the phone number is now stripped, since it shouldn’t be included.
• Spotted a new place where BrokenAuthKeyError would be raised, and it now is raised there.
Published at 2017/09/18
Additions
Bug fixes
• Now you should be able to sign in even if you have process_updates=True and no previous session.
• Some errors and methods are documented a bit clearer.
• .send_message() could randomly fail, as the returned type was not expected.
• TimeoutError is now ignored, since the request will be retried up to 5 times by default.
• “-404” errors (BrokenAuthKeyError’s) are now detected when first connecting to a new data center.
• BufferError is handled more gracefully, in the same way as InvalidCheckSumError’s.
• Attempt at fixing some “NoneType has no attribute. . . ” errors (with the .sender).
Internal changes
Published at 2017/09/14
Bug fixes
• Reconnection used to fail because it tried invoking things from the ReadThread.
• Inferring random ids for ForwardMessagesRequest wasn’t working.
• Downloading media from CDNs failed due to having forgotten to remove a single line.
• TcpClient.close() now has a ‘‘threading.Lock‘‘, so NoneType has no close() should not hap-
pen.
• New workaround for msg seqno too low/high. Also, both Session.id/seq are not saved any-
more.
Enhancements
• Request will be retried up to 5 times by default rather than failing on the first attempt.
• InvalidChecksumError’s are now ignored by the library.
• TelegramClient.get_entity() is now public, and uses the @lru_cache() decorator.
• New method to ‘‘.send_voice_note()‘‘’s.
• Methods to send message and media now support a ‘‘reply_to‘‘ parameter.
• .send_message() now returns the full message which was just sent.
Published at 2017/09/08
This update brings a new way to work with updates, and it’s begging for your feedback, or better names or ways to
do what you can do now.
Please refer to the wiki/Usage Modes for an in-depth description on how to work with updates now. Notice that you
cannot invoke requests from within handlers anymore, only the v.0.13.1 patch allowed you to do so.
Bug fixes
Published at 2017/09/04
Warning: This update brings some big changes to the update system, so please read it if you work with them!
A silly “bug” which hadn’t been spotted has now been fixed. Now you can invoke other requests from within your
update callbacks. However this is not advised. You should post these updates to some other thread, and let that thread
do the job instead. Invoking a request from within a callback will mean that, while this request is being invoked, no
other things will be read.
Internally, the generated code now resides under a lot less files, simply for the sake of avoiding so many unnecessary
files. The generated code is not meant to be read by anyone, simply to do its job.
Unused attributes have been removed from the TLObject class too, and .sign_up() returns the user that just
logged in in a similar way to .sign_in() now.
Published at 2017/09/04
The purpose of this release is to denote a big change, now you can connect to Telegram through different **connection
modes**. Also, a second thread will always be started when you connect a TelegramClient, despite whether
you’ll be handling updates or ignoring them, whose sole purpose is to constantly read from the network.
The reason for this change is as simple as “reading and writing shouldn’t be related”. Even when you’re simply
ignoring updates, this way, once you send a request you will only need to read the result for the request. Whatever
Telegram sent before has already been read and outside the buffer.
Additions
Enhancements
• The low-level socket doesn’t use a handcrafted timeout anymore, which should benefit by avoiding the arbitrary
sleep(0.1) that there used to be.
• TelegramClient.sign_in will call .send_code_request if no code was provided.
Deprecation
• .sign_up does not take a phone argument anymore. Change this or you will be using phone as code, and
it will fail! The definition looks like def sign_up(self, code, first_name, last_name='').
• The old JsonSession finally replaces the original Session (which used pickle). If you were overriding
any of these, you should only worry about overriding Session now.
Published at 2017/08/28
Since the Content Distributed Network (CDN) is not handled by Telegram itself, the owners may tamper these files.
Telegram sends their sha256 sum for clients to implement this additional verification step, which now the library has.
If any CDN has altered the file you’re trying to download, CdnFileTamperedError will be raised to let you know.
Besides this. TLObject.stringify() was showing bytes as lists (now fixed) and RPC errors are reported by
default:
In an attempt to help everyone who works with the Telegram API, Telethon will by default report all
Remote Procedure Call errors to PWRTelegram, a public database anyone can query, made by Daniil. All
the information sent is a GET request with the error code, error message and method used.
Note: If you still would like to opt out, simply set client.session.report_errors = False to disable
this feature. However Daniil would really thank you if you helped him (and everyone) by keeping it on!
Published at 2017/08/24
The biggest news for this update are that downloading media from CDN’s (you’ll often encounter this when working
with popular channels) now works.
Bug fixes
• The method used to download documents crashed because two lines were swapped.
• Determining the right path when downloading any file was very weird, now it’s been enhanced.
• The .sign_in() method didn’t support integer values for the code! Now it does again.
Some important internal changes are that the old way to deal with RSA public keys now uses a different module
instead the old strange hand-crafted version.
Hope the new, super simple README.rst encourages people to use Telethon and make it better with either sugges-
tions, or pull request. Pull requests are super appreciated, but showing some support by leaving a star also feels nice
.
Published at 2017/08/22
This update is overall an attempt to make Telethon a bit more user friendly, along with some other stability enhance-
ments, although it brings quite a few changes.
Breaking changes
Additions
• libssl will also be used if available on your system (likely on Linux based systems). This speed boost should
also apply to uploading and downloading files.
• You can use a phone number or an username for methods like .send_message(), .send_file(), and
all the other quick-access methods provided by the TelegramClient.
Bug fixes
• Crashing when migrating to a new layer and receiving old updates should not happen now.
• InputPeerChannel is now casted to InputChannel automtically too.
• .get_new_msg_id() should now be thread-safe. No promises.
• Logging out on macOS caused a crash, which should be gone now.
• More checks to ensure that the connection is flagged correctly as either connected or not.
Note: Downloading files from CDN’s will not work yet (something new that comes with layer 70).
That’s it, any new idea or suggestion about how to make the project even more friendly is highly appreciated.
Note: Did you know that you can pretty print any result Telegram returns (called TLObject’s) by using their
.stringify() function? Great for debugging!
Published at 2017/07/11
Quick fix-up of a bug which hadn’t been encountered until now. Auto-cast by using get_input_* now works.
Published at 2017/07/10
For some reason, Telegram doesn’t have enough with the InputPeer. There also exist InputChannel and InputUser!
You don’t have to worry about those anymore, it’s handled internally now.
Besides this, every Telegram object now features a new default .__str__ look, and also a .stringify() method to
pretty format them, if you ever need to inspect them.
The library now uses the DEBUG level everywhere, so no more warnings or information messages if you had logging
enabled.
The no_webpage parameter from .send_message has been renamed to link_preview for clarity, so now it
does the opposite (but has a clearer intention).
Published at 2017/07/05
A very quick follow-up release to fix a tiny bug with .send_message(), no new features.
Published at 2017/07/04
There is a new preferred way to invoke requests, which you’re encouraged to use:
# New!
result = client(SomeRequest())
# Old.
result = client.invoke(SomeRequest())
Existing code will continue working, since the old .invoke() has not been deprecated.
When you .create_new_connection(), it will also handle FileMigrateError’s for you, so you don’t
need to worry about those anymore.
Bugs fixes
• Fixed some errors when installing Telethon via pip (for those using either source distributions or a Python
version 3.5).
• ConnectionResetError didn’t flag sockets as closed, but now it does.
On a more technical side, msg_id’s are now more accurate.
Published at 2017/06/24
Receiving new updates shouldn’t miss any anymore, also, periodic pings are back again so it should work on the long
run.
On a different order of things, .connect() also features a timeout. Notice that the timeout= is not passed as a
parameter anymore, and is instead specified when creating the TelegramClient.
Bug fixes
Published at 2017/06/16
This update brings a lot of changes, so it would be nice if you could read the whole change log!
Breaking changes
• Every Telegram error has now its own class, so it’s easier to fine-tune your except’s.
• Markdown parsing is not part of Telethon itself anymore, although there are plans to support it again through a
some external module.
• The .list_sessions() has been moved to the Session class instead.
• The InteractiveTelegramClient is not shipped with pip anymore.
Additions
• A new, more lightweight class has been added. The TelegramBareClient is now the base of the normal
TelegramClient, and has the most basic features.
• New method to .create_new_connection(), which can be ran in parallel with the original connection.
This will return the previously mentioned TelegramBareClient already connected.
• Any file object can now be used to download a file (for instance, a BytesIO() instead a file name).
• Vales like random_id are now automatically inferred, so you can save yourself from the hassle of writing
generate_random_long() everywhere. Same applies to .get_input_peer(), unless you really need
the extra performance provided by skipping one if if called manually.
• Every type now features a new .to_dict() method.
Bug fixes
• Received errors are acknowledged to the server, so they don’t happen over and over.
• Downloading media on different data centers is now up to x2 faster, since there used to be an
InvalidDCError for each file part tried to be downloaded.
• Lost messages are now properly skipped.
• New way to handle the result of requests. The old ValueError “The previously sent request must be resent.
However, no request was previously sent (possibly called from a different thread).” should not happen anymore.
Internal changes
Published at 2017/06/07
This version is primarily for people to migrate their .session files, which are pickled, to the new JSON format.
Although slightly slower, and a bit more vulnerable since it’s plain text, it’s a lot more resistant to upgrades.
Warning: You must upgrade to this version before any higher one if you’ve used Telethon v0.10. If you happen
to upgrade to an higher version, that’s okay, but you will have to manually delete the *.session file, and logout
from that session from an official client.
Additions
Enhancements
Published at 2017/06/03
Working with different data centers finally works! On a different order of things, reconnection is now performed
automatically every time Telegram decides to kick us off their servers, so now Telethon can really run forever and
ever! In theory.
Enhancements
Published at 2017/05/23
Telethon used to crash a lot when logging in for the very first time. The reason for this was that the reconnection (or
dead connections) were not handled properly. Now they are, so you should be able to login directly, without needing
to delete the *.session file anymore. Notice that downloading from a different DC is still a WIP.
Enhancements
Published at 2017/05/19
Additions
Bug fixes
Internal changes
Published at 2017/04/14
Additions
Bug fixes
Published at 2017/02/19
If you’re one of those who runs Telethon for a long time (more than 30 minutes), this update by @strayge will be
great for you. It sends periodic pings to the Telegram servers so you don’t get disconnected and you can still send and
receive updates!
Published at 2017/01/31
If you’re one of those who love security the most, these are good news. You can now use two factor authentication
with Telethon too! As internal changes, the coding style has been improved, and you can easily use custom session
objects, and various little bugs have been fixed.
Published at 2016/11/13
This release has no new major features. However, it contains some small changes that make using Telethon a little bit
easier. Now those who have installed Telethon via pip can also take advantage of changes, such as less bugs, creating
empty instances of TLObjects, specifying a timeout and more!
Published at 2016/09/18
Published at 2016/09/12
Yes, really cool! I promise. Even though this is meant to be a library, that doesn’t mean it can’t have a
good interactive client for you to try the library out. This is why now you can do many, many things with the
InteractiveTelegramClient:
• List dialogs (chats) and pick any you wish.
• Send any message you like, text, photos or even documents.
• List the latest messages in the chat.
• Download any message’s media (photos, documents or even contacts!).
• Receive message updates as you talk (i.e., someone sent you a message).
It actually is a usable-enough client for your day by day. You could even add libnotify and pop, you’re done! A
great cli-client with desktop notifications.
Also, being able to download and upload media implies that you can do the same with the library itself. Did I need to
mention that? Oh, and now, with even less bugs! I hope.
Published at 2016/09/11
Telegram is more than an application to send and receive messages. You can also send and receive media. Now, this
implementation also gives you the power to upload and download media from any message that contains it! Nothing
can now stop you from filling up all your disk space with all the photos! If you want to, of course.
Published at 2016/09/10
This version handles updates in a different thread (if you wish to do so). This means that both the low level
TcpClient and the not-so-low-level MtProtoSender are now multi-thread safe, so you can use them with more
than a single thread without worrying!
This also implies that you won’t need to send a request to receive an update (is someone typing? did they send me a
message? has someone gone offline?). They will all be received instantly.
Some other cool examples of things that you can do: when someone tells you “Hello”, you can automatically reply
with another “Hello” without even needing to type it by yourself :)
However, be careful with spamming!! Do not use the program for that!
Published at 2016/09/06
There probably are some bugs left, which haven’t yet been found. However, the majority of code works and the
application is already usable! Not only that, but also uses the latest scheme as of now and handles way better the
errors. This tag is being used to mark this release as stable enough.
This project has an issues section for you to file issues whenever you encounter any when working with the library.
Said section is not for issues on your program but rather issues with Telethon itself.
If you have not made the effort to 1. read through the docs and 2. look for the method you need, you will end up on
the Wall of Shame, i.e. all issues labeled “RTFM”:
rtfm Literally “Read The F–king Manual”; a term showing the frustration of being bothered with ques-
tions so trivial that the asker could have quickly figured out the answer on their own with minimal effort,
usually by reading readily-available documents. People who say”RTFM!” might be considered rude, but
the true rude ones are the annoying people who take absolutely no self-responibility and expect to have
all the answers handed to them personally.
“Damn, that’s the twelveth time that somebody posted this question to the messageboard today! RTFM,
already!”
by Bill M. July 27, 2004
If you have indeed read the docs, and have tried looking for the method, and yet you didn’t find what you need, that’s
fine. Telegram’s API can have some obscure names at times, and for this reason, there is a “question” label with
questions that are okay to ask. Just state what you’ve tried so that we know you’ve made an effort, or you’ll go to the
Wall of Shame.
Of course, if the issue you’re going to open is not even a question but a real issue with the library (thankfully, most of
the issues have been that!), you won’t end up here. Don’t worry.
Telethon is an asyncio library. Compatibility is an important concern, and while it can’t always be kept and mistakes
happens, the Changelog (Version History) is there to tell you when these important changes happen.
Contents
2.33.1 Compatibility
Some decisions when developing will inevitable be proven wrong in the future. One of these decisions was using
threads. Now that Python 3.4 is reaching EOL and using asyncio is usable as of Python 3.5 it makes sense for a
library like Telethon to make a good use of it.
If you have old code, just use old versions of the library! There is nothing wrong with that other than not getting new
updates or fixes, but using a fixed version with pip install telethon==0.19.1.6 is easy enough to do.
You might want to consider using Virtual Environments in your projects.
There’s no point in maintaining a synchronous version because the whole point is that people don’t have time to
upgrade, and there has been several changes and clean-ups. Using an older version is the right way to go.
Sometimes, other small decisions are made. These all will be reflected in the Changelog (Version History) which you
should read when upgrading.
If you want to jump the asyncio boat, here are some of the things you will need to start migrating really old code:
# 1. Import the client from telethon.sync
from telethon.sync import TelegramClient
# ...for this:
with client:
... # REST OF YOUR CODE
In addition, all the update handlers must be async def, and you need to await method calls that rely on network
requests, such as getting the chat or sender. If you don’t use updates, you’re done!
2.33.2 Convenience
Note: The entire documentation assumes you have done one of the following:
from telethon import TelegramClient, sync
# or
from telethon.sync import TelegramClient
For quick scripts that don’t need updates, it’s a lot more convenient to forget about asyncio and just work with
sequential code. This can prove to be a powerful hybrid for running under the Python REPL too.
from telethon.sync import TelegramClient
# ^~~~~ note this part; it will manage the asyncio loop for you
client.run_until_disconnected()
Some methods, such as with, start, disconnect and run_until_disconnected work both in syn-
chronous and asynchronous contexts by default for convenience, and to avoid the little overhead it has when using
methods like sending a message, getting messages, etc. This keeps the best of both worlds as a sane default.
Note: As a rule of thumb, if you’re inside an async def and you need the client, you need to await calls to the
API. If you call other functions that also need API calls, make them async def and await them too. Otherwise,
there is no need to do so with this mode.
2.33.3 Speed
When you’re ready to micro-optimize your application, or if you simply don’t need to call any non-basic methods
from a synchronous context, just get rid of telethon.sync and work inside an async def:
import asyncio
from telethon import TelegramClient, events
@client.on(events.NewMessage(pattern='(?i)hi|hello'))
async def handler(event):
await event.reply('hey')
await client.run_until_disconnected()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
So that you don’t have to write it yourself every time. That’s the overhead you pay if you import it, and what you save
if you don’t.
2.33.4 Learning
You know the library uses asyncio everywhere, and you want to learn how to do things right. Even though asyncio
is its own topic, the documentation wants you to learn how to use Telethon correctly, and for that, you need to use
asyncio correctly too. For this reason, there is a section called Mastering asyncio that will introduce you to the
asyncio world, with links to more resources for learning how to use it. Feel free to check that section out once you
have read the rest.
2.34 TelegramClient
The TelegramClient aggregates several mixin classes to provide all the common functionality in a nice, Pythonic
interface. Each mixin has its own methods, which you all can use.
In short, to create a client you must run:
with client:
client.loop.run_until_complete(main())
You don’t need to import these AuthMethods, MessageMethods, etc. Together they are the TelegramClient
and you can access all of their methods.
See Client Reference for a short summary.
raise_last_call_error (bool, optional): When API calls fail in a way that causes Telethon to retry auto-
matically, should the RPC error of the last attempt be raised instead of a generic ValueError. This is
mostly useful for detecting when Telegram has internal issues.
device_model (str, optional): “Device model” to be sent when creating the initial connection. Defaults
to ‘PC (n)bit’ derived from platform.uname().machine, or its direct value if unknown.
system_version (str, optional): “System version” to be sent when creating the initial connection. De-
faults to platform.uname().release stripped of everything ahead of -.
app_version (str, optional): “App version” to be sent when creating the initial connection. Defaults to
telethon.version.__version__.
lang_code (str, optional): “Language code” to be sent when creating the initial connection. Defaults to
'en'.
system_lang_code (str, optional): “System lang code” to be sent when creating the initial connection.
Defaults to lang_code.
loop (asyncio.AbstractEventLoop, optional): Asyncio event loop to use. Defaults to
asyncio.get_event_loop(). This argument is ignored.
base_logger (str | logging.Logger, optional): Base logger name or instance to use. If a str is
given, it’ll be passed to logging.getLogger(). If a logging.Logger is given, it’ll be used
directly. If something else or nothing is given, the default logger will be used.
receive_updates (bool, optional): Whether the client will receive updates or not. By default, updates
will be received from Telegram as they occur.
Turning this off means that Telegram will not send updates at all so event handlers, conversations, and
QR login will not work. However, certain scripts don’t need updates, so this will reduce the amount
of bandwidth used.
__call__(request, ordered=False)
Invokes (sends) one or more MTProtoRequests and returns (receives) their result.
Args:
request (TLObject | list): The request or requests to be invoked.
ordered (bool, optional): Whether the requests (if more than one was given) should be executed
sequentially on the server. They run in arbitrary order by default.
flood_sleep_threshold (int | None, optional): The flood sleep threshold to use for this request.
This overrides the default value stored in client.flood_sleep_threshold
Returns: The result of the request (often a TLObject) or a list of results if more than one request was
given.
__version__ = '1.26.0'
__weakref__
list of weak references to the object (if defined)
connect() → None
Connects to Telegram.
Note: Connect means connect and nothing else, and only one low-level request is made to notify Telegram
about which layer we will be using.
Before Telegram sends you updates, you need to make a high-level request, like client.get_me(), as
described in https://core.telegram.org/api/updates.
Example
try:
await client.connect()
except OSError:
print('Failed to connect')
disconnect()
Disconnects from Telegram.
If the event loop is already running, this method returns a coroutine that you should await on your own
code; otherwise the loop is ran until said coroutine completes.
Event handlers which are currently running will be cancelled before this function returns (in order to
properly clean-up their tasks). In particular, this means that using disconnect in a handler will cause
code after the disconnect to never run. If this is needed, consider spawning a separate task to do the
remaining work.
Example
disconnected
Property with a Future that resolves upon disconnection.
Example
flood_sleep_threshold
is_connected() → bool
Returns True if the user has connected.
This method is not asynchronous (don’t use await on it).
Example
while client.is_connected():
await asyncio.sleep(1)
loop
Property with the asyncio event loop used by this client.
Example
# Do some work
...
await client.end_takeout(success=False)
takeout(finalize: bool = True, *, contacts: bool = None, users: bool = None, chats: bool = None,
megagroups: bool = None, channels: bool = None, files: bool = None, max_file_size: bool =
None) → TelegramClient
Returns a TelegramClient which calls methods behind a takeout session.
It does so by creating a proxy object over the current client through which making requests will use
InvokeWithTakeoutRequest to wrap them. In other words, returns the current client modified so that
requests are done as a takeout:
Some of the calls made through the takeout session will have lower flood limits. This is useful if you want
to export the data from conversations or mass-download media, since the rate limits will be lower. Only
some requests will be affected, and you will need to adjust the wait_time of methods like client.
iter_messages.
By default, all parameters are None, and you need to enable those you plan to use by setting them to either
True or False.
You should except errors.TakeoutInitDelayError as e, since this exception will raise de-
pending on the condition of the session. You can then access e.seconds to know how long you should
wait for before calling the method again.
There’s also a success property available in the takeout proxy object, so from the with body you can
set the boolean result that will be sent back to Telegram. But if it’s left None as by default, then the action
is based on the finalize parameter. If it’s True then the takeout will be finished, and if no exception
occurred during it, then True will be considered as a result. Otherwise, the takeout will not be finished
and its ID will be preserved for future usage as client.session.takeout_id.
Arguments
finalize (bool): Whether the takeout session should be finalized upon exit or not.
contacts (bool): Set to True if you plan on downloading contacts.
users (bool): Set to True if you plan on downloading information from users and their private
conversations with you.
chats (bool): Set to True if you plan on downloading information from small group chats, such as
messages and media.
megagroups (bool): Set to True if you plan on downloading information from megagroups (chan-
nels), such as messages and media.
channels (bool): Set to True if you plan on downloading information from broadcast channels,
such as messages and media.
files (bool): Set to True if you plan on downloading media and you don’t only wish to export
messages.
max_file_size (int): The maximum file size, in bytes, that you plan to download for each message
with media.
Example
try:
async with client.takeout() as takeout:
await client.get_messages('me') # normal call
await takeout.get_messages('me') # wrapped through takeout (less
˓→limits)
except errors.TakeoutInitDelayError as e:
print('Must wait', e.seconds, 'before takeout')
class telethon.client.auth.AuthMethods
Bases: object
__aenter__()
__aexit__(*args)
__enter__()
Helps to cut boilerplate on async context managers that offer synchronous variants.
__exit__(*args)
__weakref__
list of weak references to the object (if defined)
edit_2fa(current_password: str = None, new_password: str = None, *, hint: str = ”, email: str =
None, email_code_callback: Callable[[int], str] = None) → bool
Changes the 2FA settings of the logged in user.
Review carefully the parameter explanations before using this method.
Note that this method may be incredibly slow depending on the prime numbers that must be used during
the process to make sure that everything is safe.
Has no effect if both current and new password are omitted.
Arguments
log_out() → bool
Logs out Telegram and deletes the current *.session file.
The client is unusable after logging out and a new instance should be created.
Returns True if the operation was successful.
Example
def display_url_as_qr(url):
pass # do whatever to show url as a qr to the user
# You should except that error and call `sign_in` with the password if
˓→this happens.
sign_in(phone: str = None, code: Union[str, int] = None, *, password: str = None, bot_token: str =
None, phone_code_hash: str = None) → typing.Union[types.User, types.auth.SentCode]
Logs in to Telegram to an existing user or bot account.
You should only use this if you are not authorized yet.
This method will send the code if it’s not provided.
Note: In most cases, you should simply use start() and not this method.
Arguments
phone (str | int): The phone to send the code to if no code was provided, or to override the phone
that was previously used with these requests.
code (str | int): The code that Telegram sent. Note that if you have sent this code through the
application itself it will immediately expire. If you want to send the code, obfuscate it somehow.
If you’re not doing any of this you can ignore this note.
password (str): 2FA password, should be used if a previous call raised
SessionPasswordNeededError.
bot_token (str): Used to sign in as a bot. Not all requests will be available. This should be the hash
the @BotFather gave you.
phone_code_hash (str, optional): The hash returned by send_code_request. This can be left
as None to use the last hash known for the phone to be used.
Returns The signed in user, or the information about send_code_request().
Example
sign_up(code: Union[str, int], first_name: str, last_name: str = ”, *, phone: str = None,
phone_code_hash: str = None) → types.User
Signs up to Telegram as a new user account.
Use this if you don’t have an account yet.
You must call send_code_request first.
By using this method you’re agreeing to Telegram’s Terms of Service. This is required and your
account will be banned otherwise. See https://telegram.org/tos and https://core.telegram.org/api/terms.
Arguments
code (str | int): The code sent by Telegram
first_name (str): The first name to be used by the new account.
last_name (str, optional) Optional last name.
phone (str | int, optional): The phone to sign up. This will be the last phone used by default (you
normally don’t need to set this).
phone_code_hash (str, optional): The hash returned by send_code_request. This can be left
as None to use the last hash known for the phone to be used.
Returns The new created User.
Example
password (str, callable, optional): The password for 2 Factor Authentication (2FA). This is
only required if it is enabled in your account. The argument may be a coroutine.
bot_token (str): Bot Token obtained by @BotFather to log in as a bot. Cannot be specified with
phone (only one of either allowed).
force_sms (bool, optional): Whether to force sending the code request as SMS. This only makes
sense when signing in with a phone.
code_callback (callable, optional): A callable that will be used to retrieve the Telegram login
code. Defaults to input(). The argument may be a coroutine.
first_name (str, optional): The first name to be used if signing up. This has no effect if the account
already exists and you sign in.
last_name (str, optional): Similar to the first name, but for the last. Optional.
max_attempts (int, optional): How many times the code/password callback should be retried or
switching between signing in and signing up.
Returns This TelegramClient, so initialization can be chained with .start().
Example
class telethon.client.bots.BotMethods
Bases: object
__weakref__
list of weak references to the object (if defined)
inline_query(bot: hints.EntityLike, query: str, *, entity: hints.EntityLike = None, offset: str = None,
geo_point: types.GeoPoint = None) → telethon.tl.custom.inlineresults.InlineResults
Makes an inline query to the specified bot (@vote New Poll).
Arguments
bot (entity): The bot entity to which the inline query should be made.
query (str): The query that should be made to the bot.
entity (entity, optional): The entity where the inline query is being made from. Certain bots use
this to display different results depending on where it’s used, such as private chats, groups or
channels.
If specified, it will also be the default entity where the message will be sent after clicked. Other-
wise, the “empty peer” will be used, which some bots may not handle correctly.
offset (str, optional): The string offset to use for the bot.
geo_point (GeoPoint, optional) The geo point location information to send to the bot for localised
results. Available under some bots.
Returns A list of custom.InlineResult.
Example
class telethon.client.buttons.ButtonMethods
Bases: object
__weakref__
list of weak references to the object (if defined)
markup = client.build_reply_markup(Button.inline('hi'))
# later
await client.send_message(chat, 'click me', buttons=markup)
class telethon.client.chats.ChatMethods
Bases: object
__weakref__
list of weak references to the object (if defined)
action(entity: hints.EntityLike, action: typing.Union[str, types.TypeSendMessageAction], *, delay:
float = 4, auto_cancel: bool = True) → typing.Union[_ChatAction, typing.Coroutine]
Returns a context-manager object to represent a “chat action”.
Chat actions indicate things like “user is typing”, “user is uploading a photo”, etc.
If the action is 'cancel', you should just await the result, since it makes no sense to use a context-
manager for it.
See the example below for intended usage.
Arguments
entity (entity): The entity where the action should be showed in.
action (str | SendMessageAction): The action to show. You can either pass a instance of SendMes-
sageAction or better, a string used while:
• 'typing': typing a text message.
• 'contact': choosing a contact.
• 'game': playing a game.
• 'location': choosing a geo location.
• 'sticker': choosing a sticker.
• 'record-audio': recording a voice note. You may use 'record-voice' as alias.
• 'record-round': recording a round video.
• 'record-video': recording a normal video.
• 'audio': sending an audio file (voice note or song). You may use 'voice' and 'song'
as aliases.
invite_users (bool, optional): Whether the user will be able to invite users. Needs some testing.
pin_messages (bool, optional): Whether the user will be able to pin messages.
add_admins (bool, optional): Whether the user will be able to add admins.
manage_call (bool, optional): Whether the user will be able to manage group calls.
anonymous (bool, optional): Whether the user will remain anonymous when sending messages.
The sender of the anonymous messages becomes the group itself.
Note: Users may be able to identify the anonymous admin by its custom title, so additional care
is needed when using both anonymous and custom titles. For example, if multiple anonymous
admins share the same title, users won’t be able to distinguish them.
is_admin (bool, optional): Whether the user will be an admin in the chat. This will only work in
small group chats. Whether the user will be an admin in the chat. This is the only permission
available in small group chats, and when used in megagroups, all non-explicitly set permissions
will have this value.
Essentially, only passing is_admin=True will grant all permissions, but you can still disable
those you need.
title (str, optional): The custom title (also known as “rank”) to show for this admin. This text will
be shown instead of the “admin” badge. This will only work in channels and megagroups.
When left unspecified or empty, the default localized “admin” badge will be shown.
Returns The resulting Updates object.
Example
Arguments
entity (entity): The channel or megagroup where the restriction should happen.
user (entity, optional): If specified, the permission will be changed for the specific user. If left as
None, the default chat permissions will be updated.
until_date (DateLike, optional): When the user will be unbanned.
If the due date or duration is longer than 366 days or shorter than 30 seconds, the ban will be
forever. Defaults to 0 (ban forever).
view_messages (bool, optional): Whether the user is able to view messages or not. Forbidding
someone from viewing messages equals to banning them. This will only work if user is set.
send_messages (bool, optional): Whether the user is able to send messages or not.
send_media (bool, optional): Whether the user is able to send media or not.
send_stickers (bool, optional): Whether the user is able to send stickers or not.
send_gifs (bool, optional): Whether the user is able to send animated gifs or not.
send_games (bool, optional): Whether the user is able to send games or not.
send_inline (bool, optional): Whether the user is able to use inline bots or not.
embed_link_previews (bool, optional): Whether the user is able to enable the link preview in the
messages they send. Note that the user will still be able to send messages with links if this
permission is removed, but these links won’t display a link preview.
send_polls (bool, optional): Whether the user is able to send polls or not.
change_info (bool, optional): Whether the user is able to change info or not.
invite_users (bool, optional): Whether the user is able to invite other users or not.
pin_messages (bool, optional): Whether the user is able to pin messages or not.
Returns The resulting Updates object.
Example
Example
Note: This request has to fetch the entire chat for small group chats, which can get somewhat expensive,
so use of a cache is advised.
Arguments
entity (entity): The channel or chat the user is participant of.
user (entity, optional): Target user.
Returns A ParticipantPermissions instance. Refer to its documentation to see what properties
are available.
Example
print(stats.stringify())
admins (entity | list): If present, the events will be filtered by these admins (or single admin)
and only those caused by them will be returned.
join (bool): If True, events for when a user joined will be returned.
leave (bool): If True, events for when a user leaves will be returned.
invite (bool): If True, events for when a user joins through an invite link will be returned.
restrict (bool): If True, events with partial restrictions will be returned. This is what the API calls
“ban”.
unrestrict (bool): If True, events removing restrictions will be returned. This is what the API calls
“unban”.
ban (bool): If True, events applying or removing all restrictions will be returned. This is what the
API calls “kick” (restricting all permissions removed is a ban, which kicks the user).
unban (bool): If True, events removing all restrictions will be returned. This is what the API calls
“unkick”.
promote (bool): If True, events with admin promotions will be returned.
demote (bool): If True, events with admin demotions will be returned.
info (bool): If True, events changing the group info will be returned.
settings (bool): If True, events changing the group settings will be returned.
pinned (bool): If True, events of new pinned messages will be returned.
edit (bool): If True, events of message edits will be returned.
delete (bool): If True, events of message deletions will be returned.
group_call (bool): If True, events related to group calls will be returned.
Yields Instances of AdminLogEvent.
Example
Note: The filter ChannelParticipantsBanned will return restricted users. If you want banned
users you should use ChannelParticipantsKicked instead.
# Search by name
async for user in client.iter_participants(chat, search='name'):
print(user.username)
# Filter by admins
from telethon.tl.types import ChannelParticipantsAdmins
async for user in client.iter_participants(chat,
˓→filter=ChannelParticipantsAdmins):
print(user.first_name)
Note: Attempting to kick someone who was banned will remove their restrictions (and thus unbanning
them), since kicking is just ban + unban.
Arguments
entity (entity): The channel or chat where the user should be kicked from.
user (entity, optional): The user to kick.
Returns Returns the service Message produced about a user being kicked, if any.
Example
# Kick some user from some chat, and deleting the service message
msg = await client.kick_participant(chat, user)
await msg.delete()
# Leaving chat
await client.kick_participant(chat, 'me')
class telethon.client.dialogs.DialogMethods
Bases: object
__weakref__
list of weak references to the object (if defined)
conversation(entity: hints.EntityLike, *, timeout: float = 60, total_timeout: float = None,
max_messages: int = 100, exclusive: bool = True, replies_are_responses: bool =
True) → telethon.tl.custom.conversation.Conversation
Creates a Conversation with the given entity.
Note: This Conversation API has certain shortcomings, such as lacking persistence, poor interaction with
other event handlers, and overcomplicated usage for anything beyond the simplest case.
If you plan to interact with a bot without handlers, this works fine, but when running a bot yourself, you
may instead prefer to follow the advice from https://stackoverflow.com/a/62246569/.
This is not the same as just sending a message to create a “dialog” with them, but rather a way to easily
send messages and await for responses or other reactions. Refer to its documentation for more.
Arguments
entity (entity): The entity with which a new conversation should be opened.
timeout (int | float, optional): The default timeout (in seconds) per action to be used. You may
also override this timeout on a per-method basis. By default each action can take up to 60 seconds
(the value of this timeout).
total_timeout (int | float, optional): The total timeout (in seconds) to use for the whole conver-
sation. This takes priority over per-action timeouts. After these many seconds pass, subsequent
actions will result in asyncio.TimeoutError.
max_messages (int, optional): The maximum amount of messages this conversation will remem-
ber. After these many messages arrive in the specified chat, subsequent actions will result in
ValueError.
exclusive (bool, optional): By default, conversations are exclusive within a single chat. That means
that while a conversation is open in a chat, you can’t open another one in the same chat, unless
you disable this flag.
If you try opening an exclusive conversation for a chat where it’s already open, it will raise
AlreadyInConversationError.
replies_are_responses (bool, optional): Whether replies should be treated as responses or not.
If the setting is enabled, calls to conv.get_response and a subsequent call to conv.
get_reply will return different messages, otherwise they may return the same message.
Consider the following scenario with one outgoing message, 1, and two incoming messages, the
second one replying:
Hello! <1
2> (reply to 1) Hi!
3> (reply to 1) How are you?
With the setting enabled, msg2 will be 'Hi!' and msg3 be 'How are you?' since replies
are also responses, and a response was already returned.
With the setting disabled, both msg2 and msg3 will be 'Hi!' since one is a response and also
a reply.
Returns A Conversation.
Example
# <usr> Hello!
hello = conv.get_response()
# <usr> ?
name = conv.get_response().raw_text
# <usr> Human
name = conv.get_response().raw_text
class telethon.client.downloads.DownloadMethods
Bases: object
__weakref__
list of weak references to the object (if defined)
Note: Generally, you should instead use download_media. This method is intended to be a bit more
low-level.
Arguments
input_location (InputFileLocation): The file location from which the file will be downloaded. See
telethon.utils.get_input_location source for a complete list of supported types.
file (str | file, optional): The output file path, directory, or stream-like object. If the path exists
and is a file, it will be overwritten.
If the file path is None or bytes, then the result will be saved in memory and returned as bytes.
part_size_kb (int, optional): Chunk size when downloading files. The larger, the less requests will
be made (up to 512KB maximum).
file_size (int, optional): The file size that is about to be downloaded, if known. Only used if
progress_callback is specified.
progress_callback (callable, optional): A callback function accepting two parameters:
(downloaded bytes, total). Note that the total is the provided file_size.
dc_id (int, optional): The data center the library should connect to in order to download the file.
You shouldn’t worry about this.
key (‘bytes’, optional): In case of an encrypted upload (secret chats) a key is supplied
iv (‘bytes’, optional): In case of an encrypted upload (secret chats) an iv is supplied
Example
thumb (int | PhotoSize, optional): Which thumbnail size from the document or photo to down-
load, instead of downloading the document or photo itself.
If it’s specified but the file does not have a thumbnail, this method will return None.
The parameter should be an integer index between 0 and len(sizes). 0 will download the
smallest thumbnail, and len(sizes) - 1 will download the largest thumbnail. You can also
use negative indices, which work the same as they do in Python’s list.
You can also pass the PhotoSize instance to use. Alternatively, the thumb size type str may be
used.
In short, use thumb=0 if you want the smallest thumbnail and thumb=-1 if you want the largest
thumbnail.
Note: The largest thumbnail may be a video instead of a photo, as they are available since layer
116 and are bigger than any of the photos.
Returns None if no media was provided, or if it was Empty. On success the file path is returned since it
may differ from the one given.
Example
Note: This method expects the full entity (which has the data to download the photo), not an
input variant.
It’s possible that sometimes you can’t fetch the entity from its input (since you can get errors
like ChannelPrivateError) but you already have it through another call, like getting a for-
warded message from it.
file (str | file, optional): The output file path, directory, or stream-like object. If the path exists
and is a file, it will be overwritten. If file is the type bytes, it will be downloaded in-memory as
a bytestring (e.g. file=bytes).
download_big (bool, optional): Whether to use the big version of the available photos.
Returns None if no photo was provided, or if it was Empty. On success the file path is returned since it
may differ from the one given.
Example
iter_download(file: hints.FileLike, *, offset: int = 0, stride: int = None, limit: int = None,
chunk_size: int = None, request_size: int = 524288, file_size: int = None, dc_id:
int = None)
Iterates over a file download, yielding chunks of the file.
This method can be used to stream files in a more convenient way, since it offers more control (pausing,
resuming, etc.)
Note: Using a value for offset or stride which is not a multiple of the minimum allowed
request_size, or if chunk_size is different from request_size, the library will need to do
a bit more work to fetch the data in the way you intend it to.
You normally shouldn’t worry about this.
Arguments
file (hints.FileLike): The file of which contents you want to iterate over.
offset (int, optional): The offset in bytes into the file from where the download should start. For
example, if a file is 1024KB long and you just want the last 512KB, you would use offset=512
* 1024.
stride (int, optional): The stride of each chunk (how much the offset should advance between read-
ing each chunk). This parameter should only be used for more advanced use cases.
It must be bigger than or equal to the chunk_size.
limit (int, optional): The limit for how many chunks will be yielded at most.
chunk_size (int, optional): The maximum size of the chunks that will be yielded. Note that the
last chunk may be less than this value. By default, it equals to request_size.
request_size (int, optional): How many bytes will be requested to Telegram when more data is
required. By default, as many bytes as possible are requested. If you would like to request data in
smaller sizes, adjust this parameter.
Note that values outside the valid range will be clamped, and the final value will also be a multiple
of the minimum allowed size.
file_size (int, optional): If the file size is known beforehand, you should set this parameter to said
value. Depending on the type of the input file passed, this may be set automatically.
dc_id (int, optional): The data center the library should connect to in order to download the file.
You shouldn’t worry about this.
Yields
bytes objects representing the chunks of the file if the right conditions are met, or
memoryview objects instead.
Example
class telethon.client.messageparse.MessageParseMethods
Bases: object
__weakref__
list of weak references to the object (if defined)
parse_mode
This property is the default parse mode used when sending messages. Defaults to telethon.
extensions.markdown. It will always be either None or an object with parse and unparse
methods.
When setting a different value it should be one of:
• Object with parse and unparse methods.
• A callable to act as the parse method.
• A str indicating the parse_mode. For Markdown 'md' or 'markdown' may be used. For
HTML, 'htm' or 'html' may be used.
The parse method should be a function accepting a single parameter, the text to parse, and returning a
tuple consisting of (parsed message str, [MessageEntity instances]).
The unparse method should be the inverse of parse such that assert text ==
unparse(*parse(text)).
See MessageEntity for allowed message entities.
Example
class telethon.client.messages.MessageMethods
Bases: object
__weakref__
list of weak references to the object (if defined)
Warning: This method does not validate that the message IDs belong to the chat that you passed! It’s
possible for the method to delete messages from different private chats and small group chats at once,
so make sure to pass the right IDs.
Arguments
entity (entity): From who the message will be deleted. This can actually be None for normal
chats, but must be present for channels and megagroups.
message_ids (list | int | Message): The IDs (or ID) or messages to be deleted.
revoke (bool, optional): Whether the message should be deleted for everyone or not. By default it
has the opposite behaviour of official clients, and it will delete the message for everyone.
Since 24 March 2019, you can also revoke messages of any age (i.e. messages sent long in the
past) the other person sent in private conversations (and of course your messages too).
Disabling this has no effect on channels or megagroups, since it will unconditionally delete the
message for everyone.
Returns A list of AffectedMessages, each item being the result for the delete calls of the messages in
chunks of 100 each.
Example
entity (entity | Message): From which chat to edit the message. This can also be the message to
be edited, and the entity will be inferred from it, so the next parameter will be assumed to be the
message text.
You may also pass a InputBotInlineMessageID, which is the only way to edit messages that were
sent after the user selects an inline query result.
message (int | Message | str): The ID of the message (or Message itself) to be edited. If the
entity was a Message, then this message will be treated as the new text.
text (str, optional): The new text of the message. Does nothing if the entity was a Message.
parse_mode (object, optional): See the TelegramClient.parse_mode property for al-
lowed values. Markdown parsing will be used by default.
attributes (list, optional): Optional attributes that override the inferred ones, like DocumentAt-
tributeFilename and so on.
formatting_entities (list, optional): A list of message formatting entities. When provided, the
parse_mode is ignored.
link_preview (bool, optional): Should the link preview be shown?
file (str | bytes | file | media, optional): The file object that should replace the existing media
in the message.
thumb (str | bytes | file, optional): Optional JPEG thumbnail (for documents). Telegram will
ignore this parameter unless you pass a .jpg file! The file must also be small in dimensions
and in disk size. Successful thumbnails were files below 20kB and 320x320px. Width/height and
dimensions/size ratios may be important. For Telegram to accept a thumbnail, you must provide
the dimensions of the underlying media through attributes= with DocumentAttributesVideo
or by installing the optional hachoir dependency.
force_document (bool, optional): Whether to send the given file as a document or not.
buttons (list, custom.Button, KeyboardButton): The matrix (list of lists), row list or button
to be shown after sending the message. This parameter will only work if you have signed in as a
bot. You can also pass your own ReplyMarkup here.
supports_streaming (bool, optional): Whether the sent video supports streaming or not. Note that
Telegram only recognizes as streamable some formats like MP4, and others like AVI or MKV will
not work. You should convert these to MP4 before sending if you want them to be streamable.
Unsupported formats will result in VideoContentTypeError.
schedule (hints.DateLike, optional): If set, the message won’t be edited immediately, and in-
stead it will be scheduled to be automatically edited at a later time.
Note that this parameter will have no effect if you are trying to edit a message that was sent via
inline bots.
Returns The edited Message, unless entity was a InputBotInlineMessageID in which case this
method returns a boolean.
Raises MessageAuthorRequiredError if you’re not the author of the message but tried editing it
anyway.
MessageNotModifiedError if the contents of the message were not modified at all.
MessageIdInvalidError if the ID of the message is invalid (the ID itself may be correct, but
the message with that ID cannot be edited). For example, when trying to edit messages with a reply
markup (or clear markup) this error will be raised.
Example
# a single one
await client.forward_messages(chat, message)
# or
await client.forward_messages(chat, message_id, from_chat)
# or
await message.forward_to(chat)
# multiple
await client.forward_messages(chat, messages)
# or
(continues on next page)
# Forwarding as a copy
await client.send_message(chat, message)
# Get 0 photos and print the total to show how many photos there are
from telethon.tl.types import InputMessagesFilterPhotos
photos = await client.get_messages(chat, 0,
˓→filter=InputMessagesFilterPhotos)
print(photos.total)
Note: Telegram’s flood wait limit for GetHistoryRequest seems to be around 30 seconds per 10 requests,
therefore a sleep of 1 second is the default for this limit (or above).
Arguments
entity (entity): The entity from whom to retrieve the message history.
It may be None to perform a global search, or to get messages by their ID from no particular
chat. Note that some of the offsets will not work if this is the case.
Note that if you want to perform a global search, you must set a non-empty search string, a
filter. or from_user.
limit (int | None, optional): Number of messages to be retrieved. Due to limitations with the API
retrieving more than 3000 messages will take longer than half a minute (or even more based on
previous calls).
The limit may also be None, which would eventually return the whole history.
offset_date (datetime): Offset date (messages previous to this date will be retrieved). Exclusive.
offset_id (int): Offset message ID (only messages previous to the given ID will be retrieved). Ex-
clusive.
max_id (int): All the messages with a higher (newer) ID or equal to this will be excluded.
min_id (int): All the messages with a lower (older) ID or equal to this will be excluded.
add_offset (int): Additional message offset (all of the specified offsets + this offset = older mes-
sages).
search (str): The string to be used as a search query.
filter (MessagesFilter | type): The filter to use when returning messages. For instance, InputMes-
sagesFilterPhotos would yield only messages containing photos.
from_user (entity): Only messages from this entity will be returned.
wait_time (int): Wait time (in seconds) between different GetHistoryRequest. Use this parameter
to avoid hitting the FloodWaitError as needed. If left to None, it will default to 1 second
only if the limit is higher than 3000.
If the ids parameter is used, this time will default to 10 seconds only if the amount of IDs is
higher than 300.
ids (int, list): A single integer ID (or several IDs) for the message that should be returned. This
parameter takes precedence over the rest (which will be ignored if this is set). This can for
instance be used to get the message with ID 123 from a channel. Note that if the message doesn’t
exist, None will appear in its place, so that zipping the list of IDs with the messages can match
one-to-one.
Note: At the time of writing, Telegram will not return MessageEmpty for InputMessageReplyTo
IDs that failed (i.e. the message is not replying to any, or is replying to a deleted message). This
means that it is not possible to match messages one-by-one, so be careful if you use non-integers
in this parameter.
reverse (bool, optional): If set to True, the messages will be returned in reverse order (from old-
est to newest, instead of the default newest to oldest). This also means that the meaning of
offset_id and offset_date parameters is reversed, although they will still be exclusive.
min_id becomes equivalent to offset_id instead of being max_id as well since messages
are returned in ascending order.
You cannot use this if both entity and ids are None.
reply_to (int, optional): If set to a message ID, the messages that reply to this ID will be returned.
This feature is also known as comments in posts of broadcast channels, or viewing threads in
groups.
This feature can only be used in broadcast channels and their linked megagroups. Using it in a
chat or private conversation will result in telethon.errors.PeerIdInvalidError to
occur.
When using this parameter, the filter and search parameters have no effect, since Tele-
gram’s API doesn’t support searching messages in replies.
Note: This feature is used to get replies to a message in the discussion group. If the same
broadcast channel sends a message and replies to it itself, that reply will not be included in the
results.
scheduled (bool, optional): If set to True, messages which are scheduled will be returned. All
other parameter will be ignored for this, except entity.
Yields Instances of Message.
Example
# Filter by sender
async for message in client.iter_messages(chat, from_user='me'):
print(message.text)
print(message.photo)
message (int | Message): The message or the message ID to pin. If it’s None, all messages will
be unpinned instead.
notify (bool, optional): Whether the pin should notify people or not.
pm_oneside (bool, optional): Whether the message should be pinned for everyone or not. By de-
fault it has the opposite behaviour of official clients, and it will pin the message for both sides, in
private chats.
Example
attributes (list, optional): Optional attributes that override the inferred ones, like DocumentAt-
tributeFilename and so on.
parse_mode (object, optional): See the TelegramClient.parse_mode property for al-
lowed values. Markdown parsing will be used by default.
formatting_entities (list, optional): A list of message formatting entities. When provided, the
parse_mode is ignored.
link_preview (bool, optional): Should the link preview be shown?
file (file, optional): Sends a message with a file attached (e.g. a photo, video, audio or document).
The message may be empty.
thumb (str | bytes | file, optional): Optional JPEG thumbnail (for documents). Telegram will
ignore this parameter unless you pass a .jpg file! The file must also be small in dimensions
and in disk size. Successful thumbnails were files below 20kB and 320x320px. Width/height and
dimensions/size ratios may be important. For Telegram to accept a thumbnail, you must provide
the dimensions of the underlying media through attributes= with DocumentAttributesVideo
or by installing the optional hachoir dependency.
force_document (bool, optional): Whether to send the given file as a document or not.
clear_draft (bool, optional): Whether the existing draft should be cleared or not.
buttons (list, custom.Button, KeyboardButton): The matrix (list of lists), row list or button
to be shown after sending the message. This parameter will only work if you have signed in as a
bot. You can also pass your own ReplyMarkup here.
All the following limits apply together:
• There can be 100 buttons at most (any more are ignored).
• There can be 8 buttons per row at most (more are ignored).
• The maximum callback data per button is 64 bytes.
• The maximum data that can be embedded in total is just over 4KB, shared between inline
callback data and text.
silent (bool, optional): Whether the message should notify people in a broadcast channel or not.
Defaults to False, which means it will notify them. Set it to True to alter this behaviour.
background (bool, optional): Whether the message should be send in background.
supports_streaming (bool, optional): Whether the sent video supports streaming or not. Note that
Telegram only recognizes as streamable some formats like MP4, and others like AVI or MKV will
not work. You should convert these to MP4 before sending if you want them to be streamable.
Unsupported formats will result in VideoContentTypeError.
schedule (hints.DateLike, optional): If set, the message won’t send immediately, and instead
it will be scheduled to be automatically sent at a later time.
comment_to (int | Message, optional): Similar to reply_to, but replies in the linked group of
a broadcast channel instead (effectively leaving a “comment to” the specified message).
This parameter takes precedence over reply_to. If there is no linked chat, telethon.
errors.sgIdInvalidError is raised.
Returns The sent custom.Message.
Example
# code and pre tags also work, but those break the documentation :)
await client.send_message('me', '<a href="tg://user?id=me">Mentions</a>')
@client.on(events.CallbackQuery)
async def callback(event):
await event.edit('Thank you for clicking {}!'.format(event.data))
# Reply keyboard
await client.send_message(chat, 'Welcome', buttons=[
Button.text('Thanks!', resize=True, single_use=True),
Button.request_phone('Send phone'),
Button.request_location('Send location')
])
client.add_event_handler(handler, events.NewMessage)
catch_up()
“Catches up” on the missed updates while the client was offline. You should call this method after regis-
tering the event handlers so that the updates it loads can by processed by your script.
This can also be used to forcibly fetch new updates if there are any.
Example
await client.catch_up()
@client.on(events.NewMessage(pattern='hello'))
async def on_greeting(event):
'''Greets someone'''
await event.reply('Hi')
(continues on next page)
on(event: telethon.events.common.EventBuilder)
Decorator used to add_event_handler more conveniently.
Arguments
event (_EventBuilder | type): The event builder class or instance to be used, for instance
events.NewMessage.
Example
@client.on(events.Raw)
@client.on(events.NewMessage)
async def handler(event):
...
run_until_disconnected()
Runs the event loop until the library is disconnected.
It also notifies Telegram that we want to receive updates as described in https://core.telegram.org/api/
updates. If an unexpected error occurs during update handling, the client will disconnect and said error
will be raised.
Manual disconnections can be made by calling disconnect() or sending a KeyboardInterrupt
(e.g. by pressing Ctrl+C on the console window running the script).
If a disconnection error occurs (i.e. the library fails to reconnect automatically), said error will be raised
through here, so you have a chance to except it on your own code.
If the loop is already running, this method returns a coroutine that you should await on your own code.
Note: If you want to handle KeyboardInterrupt in your code, simply run the event loop in your
code too in any way, such as loop.run_forever() or await client.disconnected (e.g.
loop.run_until_complete(client.disconnected)).
Example
set_receive_updates(receive_updates)
Change the value of receive_updates.
This is an async method, because in order for Telegram to start sending updates again, a request must be
made.
class telethon.client.uploads.UploadMethods
Bases: object
__weakref__
list of weak references to the object (if defined)
send_file(entity: hints.EntityLike, file: typing.Union[hints.FileLike, typ-
ing.Sequence[hints.FileLike]], *, caption: Union[str, Sequence[str]] = None,
force_document: bool = False, file_size: int = None, clear_draft: bool = False,
progress_callback: hints.ProgressCallback = None, reply_to: hints.MessageIDLike
= None, attributes: typing.Sequence[types.TypeDocumentAttribute] = None, thumb:
hints.FileLike = None, allow_cache: bool = True, parse_mode: str = (), for-
matting_entities: Optional[List[Union[telethon.tl.types.MessageEntityUnknown,
telethon.tl.types.MessageEntityMention, telethon.tl.types.MessageEntityHashtag,
telethon.tl.types.MessageEntityBotCommand, telethon.tl.types.MessageEntityUrl,
telethon.tl.types.MessageEntityEmail, telethon.tl.types.MessageEntityBold,
telethon.tl.types.MessageEntityItalic, telethon.tl.types.MessageEntityCode,
telethon.tl.types.MessageEntityPre, telethon.tl.types.MessageEntityTextUrl,
telethon.tl.types.MessageEntityMentionName, telethon.tl.types.InputMessageEntityMentionName,
telethon.tl.types.MessageEntityPhone, telethon.tl.types.MessageEntityCashtag,
telethon.tl.types.MessageEntityUnderline, telethon.tl.types.MessageEntityStrike,
telethon.tl.types.MessageEntityBlockquote, telethon.tl.types.MessageEntityBankCard,
telethon.tl.types.MessageEntitySpoiler, telethon.tl.types.MessageEntityCustomEmoji]]]
= None, voice_note: bool = False, video_note: bool = False, buttons: Op-
tional[hints.MarkupLike] = None, silent: bool = None, background: bool = None,
supports_streaming: bool = False, schedule: hints.DateLike = None, comment_to:
typing.Union[int, types.Message] = None, ttl: int = None, **kwargs) → types.Message
Sends message with the given file to the specified entity.
Note: If the hachoir3 package (hachoir module) is installed, it will be used to determine metadata
from audio and video files.
If the pillow package is installed and you are sending a photo, it will be resized to fit within the maximum
dimensions allowed by Telegram to avoid errors.PhotoInvalidDimensionsError. This cannot
be done if you are sending InputFile, however.
Arguments
entity (entity): Who will receive the file.
file (str | bytes | file | media): The file to send, which can be one of:
• A local file path to an in-disk file. The file name will be the path’s base name.
• A bytes byte array with the file’s data to send (for example, by using text.
encode('utf-8')). A default file name will be used.
• A bytes io.IOBase stream over the file to send (for example, by using open(file,
'rb')). Its .name property will be used for the file name, or a default if it doesn’t have
one.
• An external URL to a file over the internet. This will send the file as “external” media, and
Telegram is the one that will fetch the media and send it.
• A Bot API-like file_id. You can convert previously sent media to file IDs for later reusing
with telethon.utils.pack_bot_file_id.
• A handle to an existing file (for example, if you sent a message with media before, you can use
its message.media as a file here).
• A handle to an uploaded file (from upload_file).
• A InputMedia instance. For example, if you want to send a dice use InputMediaDice, or if you
want to send a contact use InputMediaContact.
To send an album, you should provide a list in this parameter.
If a list or similar is provided, the files in it will be sent as an album in the order in which they
appear, sliced in chunks of 10 if more than 10 are given.
caption (str, optional): Optional caption for the sent media message. When sending an album, the
caption may be a list of strings, which will be assigned to the files pairwise.
force_document (bool, optional): If left to False and the file is a path that ends with the extension
of an image file or a video file, it will be sent as such. Otherwise always as a document.
file_size (int, optional): The size of the file to be uploaded if it needs to be uploaded, which will be
determined automatically if not specified.
If the file size can’t be determined beforehand, the entire file will be read in-memory to find out
how large it is.
clear_draft (bool, optional): Whether the existing draft should be cleared or not.
progress_callback (callable, optional): A callback function accepting two parameters: (sent
bytes, total).
reply_to (int | Message): Same as reply_to from send_message.
attributes (list, optional): Optional attributes that override the inferred ones, like DocumentAt-
tributeFilename and so on.
thumb (str | bytes | file, optional): Optional JPEG thumbnail (for documents). Telegram will
ignore this parameter unless you pass a .jpg file!
The file must also be small in dimensions and in disk size. Successful thumbnails were files be-
low 20kB and 320x320px. Width/height and dimensions/size ratios may be important. For Tele-
gram to accept a thumbnail, you must provide the dimensions of the underlying media through
attributes= with DocumentAttributesVideo or by installing the optional hachoir depen-
dency.
allow_cache (bool, optional): This parameter currently does nothing, but is kept for backward-
compatibility (and it may get its use back in the future).
# Custom thumbnails
await client.send_file(chat, '/my/documents/doc.txt', thumb='photo.jpg')
# Only documents
(continues on next page)
# Albums
await client.send_file(chat, [
'/my/photos/holiday1.jpg',
'/my/photos/holiday2.jpg',
'/my/drawings/portrait.png'
])
# Contacts
await client.send_file(chat, types.InputMediaContact(
phone_number='+34 123 456 789',
first_name='Example',
last_name='',
vcard=''
))
upload_file(file: hints.FileLike, *, part_size_kb: float = None, file_size: int = None, file_name: str =
None, use_cache: type = None, key: bytes = None, iv: bytes = None, progress_callback:
hints.ProgressCallback = None) → types.TypeInputFile
Uploads a file to Telegram’s servers, without sending it.
This method returns a handle (an instance of InputFile or InputFileBig, as required) which can be later
used before it expires (they are usable during less than a day).
Uploading a file will simply return a “handle” to the file stored remotely in the Telegram servers, which
can be later used on. This will not upload the file to your own chat or any chat at all.
Arguments
file (str | bytes | file): The path of the file, byte array, or stream that will be sent. Note that if a
byte array or a stream is given, a filename or its type won’t be inferred, and it will be sent as an
“unnamed application/octet-stream”.
part_size_kb (int, optional): Chunk size when uploading files. The larger, the less requests will
be made (up to 512KB maximum).
file_size (int, optional): The size of the file to be uploaded, which will be determined automatically
if not specified.
If the file size can’t be determined beforehand, the entire file will be read in-memory to find out
how large it is.
file_name (str, optional): The file name which will be used on the resulting InputFile. If not spec-
ified, the name will be taken from the file and if this is not a str, it will be "unnamed".
use_cache (type, optional): This parameter currently does nothing, but is kept for backward-
compatibility (and it may get its use back in the future).
key (‘bytes’, optional): In case of an encrypted upload (secret chats) a key is supplied
iv (‘bytes’, optional): In case of an encrypted upload (secret chats) an iv is supplied
progress_callback (callable, optional): A callback function accepting two parameters: (sent
bytes, total).
Returns InputFileBig if the file size is larger than 10MB, InputSizedFile (subclass of InputFile)
otherwise.
Example
class telethon.client.users.UserMethods
Bases: object
__call__(request, ordered=False, flood_sleep_threshold=None)
Call self as a function.
__weakref__
list of weak references to the object (if defined)
get_entity(entity: hints.EntitiesLike) → hints.Entity
Turns the given entity into a valid Telegram User, Chat or Channel. You can also pass a list or iterable of
entities, and they will be efficiently fetched from the network.
Arguments
entity (str | int | Peer | InputPeer): If a username is given, the username will be resolved mak-
ing an API call every time. Resolving usernames is an expensive operation and will start hitting
flood waits around 50 usernames in a short period of time.
If you want to get the entity for a cached username, you should first
get_input_entity(username) which will use the cache), and then use get_entity
with the result of the previous call.
Similar limits apply to invite links, and you should use their ID instead.
Using phone numbers (from people in your contact list), exact names, integer IDs or Peer rely on
a get_input_entity first, which in turn needs the entity to be in cache, unless a InputPeer
was passed.
Unsupported types will raise TypeError.
me = await client.get_entity('me')
print(utils.get_display_name(me))
# Note that you could have used the username directly, but it's
# good to use get_input_entity if you will reuse it a lot.
async for message in client.iter_messages('username'):
...
# Note that for this to work the phone number must be in your contacts
some_id = await client.get_peer_id('+34123456789')
Example
me = await client.get_me()
print(me.username)
print(await client.get_peer_id('me'))
is_bot() → bool
Return True if the signed-in user is a bot, False otherwise.
Example
if await client.is_bot():
print('Beep')
else:
print('Hello')
is_user_authorized() → bool
Returns True if the user is authorized (logged in).
Example
Every event (builder) subclasses common.EventBuilder, so all the methods in it can be used from any event
builder/event instance.
class telethon.events.common.EventBuilder(chats=None, *, blacklist_chats=False,
func=None)
Bases: abc.ABC
The common event builder, with builtin support to filter per chat.
Args:
chats (entity, optional): May be one or more entities (username/peer/etc.), preferably IDs. By default,
only matching chats will be handled.
blacklist_chats (bool, optional): Whether to treat the chats as a blacklist instead of as a whitelist (de-
fault). This means that every chat will be handled except those specified in chats which will be
ignored if blacklist_chats=True.
func (callable, optional): A callable (async or not) function that should accept the event as input
parameter, and return a value indicating whether the event should be dispatched or not (any truthy
value will do, it does not need to be a bool). It works like a custom filter:
@client.on(events.NewMessage(func=lambda e: e.is_private))
async def handler(event):
pass # code here
__weakref__
list of weak references to the object (if defined)
classmethod build(update, others=None, self_id=None)
Builds an event for the given update if possible, or returns None.
others are the rest of updates that came in the same container as the current update.
self_id should be the current user’s ID, since it is required for some events which lack this information
but still need it.
filter(event)
Returns a truthy value if the event passed the filter and should be used, or falsy otherwise. The return value
may need to be awaited.
The events must have been resolved before this can be called.
resolve(client)
Helper method to allow event builders to be resolved before usage
class telethon.events.common.EventCommon(chat_peer=None, msg_id=None, broad-
cast=None)
Bases: telethon.tl.custom.chatgetter.ChatGetter, abc.ABC
Intermediate class with common things to all events.
Remember that this class implements ChatGetter which means you have access to all chat properties and
methods.
In addition, you can access the original_update field which contains the original Update.
__str__()
Return str(self).
client
The telethon.TelegramClient that created this event.
stringify()
to_dict()
telethon.events.common.name_inner_event(cls)
Decorator to rename cls.Event ‘Event’ as ‘cls.Event’
class telethon.events.newmessage.NewMessage(chats=None, *, blacklist_chats=False,
func=None, incoming=None, out-
going=None, from_users=None, for-
wards=None, pattern=None)
Bases: telethon.events.common.EventBuilder
Occurs whenever a new text message or a message with media arrives.
Args:
incoming (bool, optional): If set to True, only incoming messages will be handled. Mutually exclu-
sive with outgoing (can only set one of either).
outgoing (bool, optional): If set to True, only outgoing messages will be handled. Mutually exclusive
with incoming (can only set one of either).
from_users (entity, optional): Unlike chats, this parameter filters the senders of the message. That
is, only messages sent by these users will be handled. Use chats if you want private messages
with this/these users. from_users lets you filter by messages sent by one or more users across the
desired chats (doesn’t need a list).
forwards (bool, optional): Whether forwarded messages should be handled or not. By default, both
forwarded and normal messages are included. If it’s True only forwards will be handled. If it’s
False only messages that are not forwards will be handled.
pattern (str, callable, Pattern, optional): If set, only messages matching this pattern will be han-
dled. You can specify a regex-like string which will be matched against the message, a callable func-
tion that returns True if a message is acceptable, or a compiled regex pattern.
Example
import asyncio
from telethon import events
@client.on(events.NewMessage(pattern='(?i)hello.+'))
async def handler(event):
# Respond whenever someone says "Hello" and something else
await event.reply('Hey!')
@client.on(events.NewMessage(outgoing=True, pattern='!ping'))
async def handler(event):
# Say "!pong" whenever you send "!ping", then delete both messages
m = await event.respond('!pong')
await asyncio.sleep(5)
await client.delete_messages(event.chat_id, [event.id, m.id])
class Event(message)
Bases: telethon.events.common.EventCommon
Represents the event of a new message. This event can be treated to all effects as a Message, so please
refer to its documentation to know what you can do with this event.
Members:
message (Message): This is the only difference with the received Message, and will return the
telethon.tl.custom.message.Message itself, not the text.
See Message for the rest of available members and methods.
pattern_match (obj): The resulting object from calling the passed pattern function. Here’s an
example using a string (defaults to regex match):
__getattr__(item)
__setattr__(name, value)
Implement setattr(self, name, value).
classmethod build(update, others=None, self_id=None)
Builds an event for the given update if possible, or returns None.
others are the rest of updates that came in the same container as the current update.
self_id should be the current user’s ID, since it is required for some events which lack this information
but still need it.
filter(event)
Returns a truthy value if the event passed the filter and should be used, or falsy otherwise. The return value
may need to be awaited.
The events must have been resolved before this can be called.
class telethon.events.chataction.ChatAction(chats=None, *, blacklist_chats=False,
func=None)
Bases: telethon.events.common.EventBuilder
Occurs on certain chat actions:
• Whenever a new chat is created.
• Whenever a chat’s title or photo is changed or removed.
• Whenever a new message is pinned.
• Whenever a user scores in a game.
• Whenever a user joins or is added to the group.
• Whenever a user is removed or leaves a group if it has less than 50 members or the removed user was a
bot.
Note that “chat” refers to “small group, megagroup and broadcast channel”, whereas “group” refers to “small
group and megagroup” only.
Example
@client.on(events.ChatAction)
async def handler(event):
# Welcome every new user
if event.user_joined:
await event.reply('Welcome to the group!')
get_pinned_messages()
If new_pin is True, this returns a list of Message objects that were pinned.
get_user()
Returns user but will make an API call if necessary.
get_users()
Returns users but will make an API call if necessary.
input_user
Input version of the self.user property.
input_users
Input version of the self.users property.
kicked_by
The user who kicked users, if applicable (None otherwise).
reply(*args, **kwargs)
Replies to the chat action message (as a reply). Shorthand for telethon.client.messages.
MessageMethods.send_message with both entity and reply_to already set.
Has the same effect as respond if there is no message.
respond(*args, **kwargs)
Responds to the chat action message (not as a reply). Shorthand for telethon.client.
messages.MessageMethods.send_message with entity already set.
user
The first user that takes part in this action. For example, who joined.
Might be None if the information can’t be retrieved or there is no user taking part.
user_id
Returns the marked signed ID of the first user, if any.
user_ids
Returns the marked signed ID of the users, if any.
users
A list of users that take part in this action. For example, who joined.
Might be empty if the information can’t be retrieved or there are no users taking part.
classmethod build(update, others=None, self_id=None)
Builds an event for the given update if possible, or returns None.
others are the rest of updates that came in the same container as the current update.
self_id should be the current user’s ID, since it is required for some events which lack this information
but still need it.
class telethon.events.userupdate.UserUpdate(chats=None, *, blacklist_chats=False,
func=None)
Bases: telethon.events.common.EventBuilder
Occurs whenever a user goes online, starts typing, etc.
Example
@client.on(events.UserUpdate)
async def handler(event):
(continues on next page)
round
True if what’s being recorded/uploaded is a round video.
sticker
True if what’s being uploaded is a sticker.
typing
True if the action is typing a message.
until
The datetime.datetime until when the user should appear online.
uploading
True if the action is uploading something.
user
Alias for sender.
user_id
Alias for sender_id.
video
True if what’s being recorded/uploaded is an video.
within_months
True if the user was seen within 30 days.
within_weeks
True if the user was seen within 7 days.
classmethod build(update, others=None, self_id=None)
Builds an event for the given update if possible, or returns None.
others are the rest of updates that came in the same container as the current update.
self_id should be the current user’s ID, since it is required for some events which lack this information
but still need it.
class telethon.events.messageedited.MessageEdited(chats=None, *, black-
list_chats=False, func=None,
incoming=None, outgoing=None,
from_users=None, forwards=None,
pattern=None)
Bases: telethon.events.newmessage.NewMessage
Occurs whenever a message is edited. Just like NewMessage, you should treat this event as a Message.
Warning: On channels, Message.out will be True if you sent the message originally, not if you edited
it! This can be dangerous if you run outgoing commands on edits.
Some examples follow:
• You send a message “A”, out is True.
• You edit “A” to “B”, out is True.
• Someone else edits “B” to “C”, out is True (be careful!).
• Someone sends “X”, out is False.
• Someone edits “X” to “Y”, out is False.
• You edit “Y” to “Z”, out is False.
Since there are useful cases where you need the right out value, the library cannot do anything automatically
to help you. Instead, consider using from_users='me' (it won’t work in broadcast channels at all since
the sender is the channel and not you).
Example
@client.on(events.MessageEdited)
async def handler(event):
# Log the date of new edits
print('Message', event.id, 'changed at', event.date)
class Event(message)
Bases: telethon.events.newmessage.Event
classmethod build(update, others=None, self_id=None)
Builds an event for the given update if possible, or returns None.
others are the rest of updates that came in the same container as the current update.
self_id should be the current user’s ID, since it is required for some events which lack this information
but still need it.
class telethon.events.messagedeleted.MessageDeleted(chats=None, *, black-
list_chats=False, func=None)
Bases: telethon.events.common.EventBuilder
Occurs whenever a message is deleted. Note that this event isn’t 100% reliable, since Telegram doesn’t always
notify the clients that a message was deleted.
Important: Telegram does not send information about where a message was deleted if it occurs in private
conversations with other users or in small group chats, because message IDs are unique and you can identify the
chat with the message ID alone if you saved it previously.
Telethon does not save information of where messages occur, so it cannot know in which chat a message was
deleted (this will only work in channels, where the channel ID is present).
This means that the chats= parameter will not work reliably, unless you intend on working with channels and
super-groups only.
Example
@client.on(events.MessageDeleted)
async def handler(event):
# Log all deleted message IDs
for msg_id in event.deleted_ids:
print('Message', msg_id, 'was deleted in', event.chat_id)
others are the rest of updates that came in the same container as the current update.
self_id should be the current user’s ID, since it is required for some events which lack this information
but still need it.
class telethon.events.messageread.MessageRead(chats=None, *, blacklist_chats=False,
func=None, inbox=False)
Bases: telethon.events.common.EventBuilder
Occurs whenever one or more messages are read in a chat.
Args:
inbox (bool, optional): If this argument is True, then when you read someone else’s messages the
event will be fired. By default (False) only when messages you sent are read by someone else will
fire it.
Example
@client.on(events.MessageRead)
async def handler(event):
# Log when someone reads your messages
print('Someone has read all your messages until', event.max_id)
@client.on(events.MessageRead(inbox=True))
async def handler(event):
# Log when you read message in a chat (from your "inbox")
print('You have read messages until', event.max_id)
message_ids
The IDs of the messages which contents’ were read.
Use is_read() if you need to check whether a message was read instead checking if it’s in here.
classmethod build(update, others=None, self_id=None)
Builds an event for the given update if possible, or returns None.
others are the rest of updates that came in the same container as the current update.
self_id should be the current user’s ID, since it is required for some events which lack this information
but still need it.
filter(event)
Returns a truthy value if the event passed the filter and should be used, or falsy otherwise. The return value
may need to be awaited.
The events must have been resolved before this can be called.
class telethon.events.callbackquery.CallbackQuery(chats=None, *, black-
list_chats=False, func=None,
data=None, pattern=None)
Bases: telethon.events.common.EventBuilder
Occurs whenever you sign in as a bot and a user clicks one of the inline buttons on your messages.
Note that the chats parameter will not work with normal IDs or peers if the clicked inline button comes from a
“via bot” message. The chats parameter also supports checking against the chat_instance which should
be used for inline callbacks.
Args:
data (bytes, str, callable, optional): If set, the inline button payload data must match this data. A
UTF-8 string can also be given, a regex or a callable. For instance, to check against 'data_1' and
'data_2' you can use re.compile(b'data_').
pattern (bytes, str, callable, Pattern, optional): If set, only buttons with payload matching
this pattern will be handled. You can specify a regex-like string which will be matched against the
payload data, a callable function that returns True if a the payload data is acceptable, or a compiled
regex pattern.
Example
# Handle all callback queries and check data inside the handler
@client.on(events.CallbackQuery)
async def handler(event):
if event.data == b'yes':
await event.answer('Correct answer!')
Note: This method won’t respect the previous message unlike Message.edit, since the message
object is normally not present.
get_message()
Returns the message to which the clicked inline button belongs.
id
Returns the query ID. The user clicking the inline button is the one who generated this random ID.
message_id
Returns the message ID to which the clicked inline button belongs.
reply(*args, **kwargs)
Replies to the message (as a reply). Shorthand for telethon.client.messages.
MessageMethods.send_message with both entity and reply_to already set.
This method also creates a task to answer the callback.
This method will likely fail if via_inline is True.
respond(*args, **kwargs)
Responds to the message (not as a reply). Shorthand for telethon.client.messages.
MessageMethods.send_message with entity already set.
This method also creates a task to answer the callback.
This method will likely fail if via_inline is True.
via_inline
Whether this callback was generated from an inline button sent via an inline query or not. If the bot
sent the message itself with buttons, and one of those is clicked, this will be False. If a user sent the
message coming from an inline query to the bot, and one of those is clicked, this will be True.
If it’s True, it’s likely that the bot is not in the chat, so methods like respond or delete won’t
work (but edit will always work).
classmethod build(update, others=None, self_id=None)
Builds an event for the given update if possible, or returns None.
others are the rest of updates that came in the same container as the current update.
self_id should be the current user’s ID, since it is required for some events which lack this information
but still need it.
filter(event)
Returns a truthy value if the event passed the filter and should be used, or falsy otherwise. The return value
may need to be awaited.
The events must have been resolved before this can be called.
class telethon.events.inlinequery.InlineQuery(users=None, *, blacklist_users=False,
func=None, pattern=None)
Bases: telethon.events.common.EventBuilder
Occurs whenever you sign in as a bot and a user sends an inline query such as @bot query.
Args:
users (entity, optional): May be one or more entities (username/peer/etc.), preferably IDs. By default,
only inline queries from these users will be handled.
blacklist_users (bool, optional): Whether to treat the users as a blacklist instead of as a whitelist (de-
fault). This means that every chat will be handled except those specified in users which will be
ignored if blacklist_users=True.
pattern (str, callable, Pattern, optional): If set, only queries matching this pattern will be han-
dled. You can specify a regex-like string which will be matched against the message, a callable
function that returns True if a message is acceptable, or a compiled regex pattern.
Example
@client.on(events.InlineQuery)
async def handler(event):
builder = event.builder
class Event(query)
Bases: telethon.events.common.EventCommon, telethon.tl.custom.
sendergetter.SenderGetter
Represents the event of a new callback query.
Members:
query (UpdateBotInlineQuery): The original UpdateBotInlineQuery.
Make sure to access the text property of the query if you want the text rather than the actual
query object.
pattern_match (obj, optional): The resulting object from calling the passed pattern function,
which is re.compile(...).match by default.
answer(results=None, cache_time=0, *, gallery=False, next_offset=None, private=False,
switch_pm=None, switch_pm_param=”)
Answers the inline query with the given results.
See the documentation for builder to know what kind of answers can be given.
Args:
results (list, optional): A list of InputBotInlineResult to use. You should use builder to
create these:
builder = inline.builder
r1 = builder.article('Be nice', text='Have a nice day')
r2 = builder.article('Be bad', text="I don't like you")
await inline.answer([r1, r2])
@bot.on(events.InlineQuery)
async def handler(event):
builder = event.builder
rev_text = event.text[::-1]
await event.answer([
builder.article('Reverse text', text=rev_text),
builder.photo('/path/to/photo.jpg')
])
builder
Returns a new InlineBuilder instance.
geo
If the user location is requested when using inline mode and the user’s device is able to send it, this
will return the GeoPoint with the position of the user.
id
Returns the unique identifier for the query ID.
offset
The string the user’s client used as an offset for the query. This will either be empty or equal to offsets
passed to answer.
text
Returns the text the user used to make the inline query.
classmethod build(update, others=None, self_id=None)
Builds an event for the given update if possible, or returns None.
others are the rest of updates that came in the same container as the current update.
self_id should be the current user’s ID, since it is required for some events which lack this information
but still need it.
filter(event)
Returns a truthy value if the event passed the filter and should be used, or falsy otherwise. The return value
may need to be awaited.
The events must have been resolved before this can be called.
class telethon.events.album.Album(chats=None, *, blacklist_chats=False, func=None)
Bases: telethon.events.common.EventBuilder
Occurs whenever you receive an album. This event only exists to ease dealing with an unknown amount of
messages that belong to the same album.
Example
@client.on(events.Album)
async def handler(event):
# Counting how many photos or videos the album has
print('Got an album with', len(event), 'items')
class Event(messages)
Bases: telethon.events.common.EventCommon, telethon.tl.custom.
sendergetter.SenderGetter
Represents the event of a new album.
Members:
messages (Sequence[Message]): The list of messages belonging to the same album.
__getitem__(n)
Access the n’th message in the album.
Equivalent to event.messages[n].
__iter__()
Iterate over the messages in the album.
Equivalent to iter(self.messages).
__len__()
Return the amount of messages in the album.
Equivalent to len(self.messages).
delete(*args, **kwargs)
Deletes the entire album. You’re responsible for checking whether you have the permission
to do so, or to except the error otherwise. Shorthand for telethon.client.messages.
MessageMethods.delete_messages with entity and message_ids already set.
edit(*args, **kwargs)
Edits the first caption or the message, or the first messages’ caption if no caption is set, iff it’s out-
going. Shorthand for telethon.client.messages.MessageMethods.edit_message
with both entity and message already set.
Returns None if the message was incoming, or the edited Message otherwise.
Note: This is different from client.edit_message and will respect the previous state of the
message. For example, if the message didn’t have a link preview, the edit won’t add one by default,
and you should force it by setting it to True if you want it.
This is generally the most desired and convenient behaviour, and will work for link previews and
message buttons.
forward
The Forward information for the first message in the album if it was forwarded.
forward_to(*args, **kwargs)
Forwards the entire album. Shorthand for telethon.client.messages.
MessageMethods.forward_messages with both messages and from_peer already
set.
get_reply_message()
The Message that this album is replying to, or None.
extend(messages)
class telethon.events.raw.Raw(types=None, *, func=None)
Bases: telethon.events.common.EventBuilder
Raw events are not actual events. Instead, they are the raw Update object that Telegram sends. You normally
shouldn’t need these.
Args:
types (list | tuple | type, optional): The type or types that the Update instance must be. Equivalent
to if not isinstance(update, types): return.
Example
@client.on(events.Raw)
async def handler(update):
# Print all incoming updates
print(update.stringify())
__weakref__
list of weak references to the object (if defined)
telethon.events.is_handler(callback)
Returns True if the given callback is an event handler (i.e. you used register on it).
telethon.events.list(callback)
Returns a list containing the registered event builders inside the specified callback handler.
telethon.events.register(event=None)
Decorator method to register event handlers. This is the client-less add_event_handler() variant.
Note that this method only registers callbacks as handlers, and does not attach them to any client. This is
useful for external modules that don’t have access to the client, but still want to define themselves as a handler.
Example:
>>> from telethon import events
>>> @events.register(events.NewMessage)
... async def handler(event):
... ...
...
>>> # (somewhere else)
...
>>> from telethon import TelegramClient
>>> client = TelegramClient(...)
>>> client.add_event_handler(handler)
The telethon.tl.custom package contains custom classes that the library uses in order to make working with
Telegram easier. Only those that you are supposed to use will be documented here. You can use undocumented ones
at your own risk.
More often than not, you don’t need to import these (unless you want type hinting), nor do you need to manually create
instances of these classes. They are returned by client methods.
Contents
• Custom package
– AdminLogEvent
– Button
– ChatGetter
– Conversation
– Dialog
– Draft
– File
– Forward
– InlineBuilder
– InlineResult
– InlineResults
– Message
– MessageButton
– ParticipantPermissions
– QRLogin
– SenderGetter
2.36.1 AdminLogEvent
changed_title
Whether the channel’s title was changed or not.
If True, old and new will be present as str.
changed_user_volume
Whether a participant’s volume in a call has been changed.
If True, new will be the updated GroupCallParticipant.
changed_username
Whether the channel’s username was changed or not.
If True, old and new will be present as str.
date
The date when this event occurred.
deleted_exported_invite
Whether the exported chat invite has been deleted.
If True, old will be the deleted ExportedChatInvite.
deleted_message
Whether a message in this channel was deleted or not.
If True, old will be present as Message.
discarded_group_call
Whether a group call was started or not.
If True, old will be present as InputGroupCall.
edited_exported_invite
Whether the exported chat invite has been edited.
If True, old and new will be the old and new ExportedChatInvite, respectively.
id
The ID of this event.
joined
Whether user joined through the channel’s public username or not.
joined_by_invite
Whether a new participant has joined with the use of an invite link.
If True, old will be pre-existing (old) ExportedChatInvite used to join.
joined_invite
Whether a new user joined through an invite link to the channel or not.
If True, new will be present as ChannelParticipant.
left
Whether user left the channel or not.
new
The new value present in the event.
old
The old value from the event.
revoked_exported_invite
Whether the exported chat invite has been revoked.
2.36.2 Button
Note: This class is used to define reply markups, e.g. when sending a message or replying to events. When
you access Message.buttons they are actually MessageButton, so you might want to refer to that class
instead.
Helper class to allow defining reply_markup when sending a message with inline or keyboard buttons.
You should make use of the defined class methods to create button instances instead making them yourself (i.e.
don’t do Button(...) but instead use methods line Button.inline(...) etc.
You can use inline, switch_inline, url, auth, buy and game together to create inline buttons (under
the message).
You can use text, request_location, request_phone and request_poll together to create a
reply markup (replaces the user keyboard). You can also configure the aspect of the reply with these. The latest
message with a reply markup will be the one shown to the user (messages contain the buttons, not the chat
itself).
You cannot mix the two type of buttons together, and it will error if you try to do so.
The text for all buttons may be at most 142 characters. If more characters are given, Telegram will cut the text
to 128 characters and add the ellipsis (. . . ) character as the 129.
__weakref__
list of weak references to the object (if defined)
static auth(text, url=None, *, bot=None, write_access=False, fwd_text=None)
Creates a new inline button to authorize the user at the given URL.
You should set the url to be on the same domain as the one configured for the desired bot via @BotFather
using the /setdomain command.
For more information about letting the user login via Telegram to a certain domain, see https://core.
telegram.org/widgets/login.
If no url is specified, it will default to text.
Args:
bot (hints.EntityLike): The bot that requires this authorization. By default, this is the bot that
is currently logged in (itself), although you may pass a different input peer.
Note: For now, you cannot use ID or username for this argument. If you want to
use a different bot than the one currently logged in, you must manually use client.
get_input_entity().
write_access (bool): Whether write access is required or not. This is False by default (read-only
access).
fwd_text (str): The new text to show in the button if the message is forwarded. By default, the
button text will be the same.
When the user clicks this button, a confirmation box will be shown to the user asking whether they want
to login to the specified domain.
static buy(text)
Creates a new inline button to buy a product.
This can only be used when sending files of type InputMediaInvoice, and must be the first button.
If the button is not specified, Telegram will automatically add the button to the message. See the Payments
API documentation for more information.
static clear(selective=None)
Clears all keyboard buttons after sending a message with this markup. When used, no other
button should be present or it will be ignored.
selective is as documented in text.
static force_reply(single_use=None, selective=None, placeholder=None)
Forces a reply to the message with this markup. If used, no other button should be present or it will be
ignored.
single_use and selective are as documented in text.
Args:
placeholder (str): text to show the user at typing place of message.
If the placeholder is too long, Telegram applications will crop the text (for example, to 64 char-
acters and adding an ellipsis (. . . ) character as the 65th).
static game(text)
Creates a new inline button to start playing a game.
This should be used when sending files of type InputMediaGame, and must be the first button.
See the Games documentation for more information on using games.
static inline(text, data=None)
Creates a new inline button with some payload data in it.
If data is omitted, the given text will be used as data. In any case data should be either bytes or
str.
Note that the given data must be less or equal to 64 bytes. If more than 64 bytes are passed as data,
ValueError is raised. If you need to store more than 64 bytes, consider saving the real data in a
database and a reference to that data inside the button.
When the user clicks this button, events.CallbackQuery will trigger with the same data that the
button contained, so that you can determine which button was pressed.
classmethod request_location(text, *, resize=None, single_use=None, selective=None)
Creates a new keyboard button to request the user’s location on click.
resize, single_use and selective are documented in text.
When the user clicks this button, a confirmation box will be shown to the user asking whether they want
to share their location with the bot, and if confirmed a message with geo media will be sent.
classmethod request_phone(text, *, resize=None, single_use=None, selective=None)
Creates a new keyboard button to request the user’s phone on click.
resize, single_use and selective are documented in text.
When the user clicks this button, a confirmation box will be shown to the user asking whether they want
to share their phone with the bot, and if confirmed a message with contact media will be sent.
classmethod request_poll(text, *, force_quiz=False, resize=None, single_use=None, selec-
tive=None)
Creates a new keyboard button to request the user to create a poll.
If force_quiz is False, the user will be allowed to choose whether they want their poll to be a quiz
or not. Otherwise, the user will be forced to create a quiz when creating the poll.
If a poll is a quiz, there will be only one answer that is valid, and the votes cannot be retracted. Otherwise,
users can vote and retract the vote, and the pol might be multiple choice.
resize, single_use and selective are documented in text.
When the user clicks this button, a screen letting the user create a poll will be shown, and if they do create
one, the poll will be sent.
static switch_inline(text, query=”, same_peer=False)
Creates a new inline button to switch to inline query.
If query is given, it will be the default text to be used when making the inline query.
If same_peer is True the inline query will directly be set under the currently opened chat. Other-
wise, the user will have to select a different dialog to make the query.
When the user clicks this button, after a chat is selected, their input field will be filled with the username
of your bot followed by the query text, ready to make inline queries.
classmethod text(text, *, resize=None, single_use=None, selective=None)
Creates a new keyboard button with the given text.
Args:
resize (bool): If present, the entire keyboard will be reconfigured to be resized and be smaller if
there are not many buttons.
single_use (bool): If present, the entire keyboard will be reconfigured to be usable only once before
it hides itself.
selective (bool): If present, the entire keyboard will be reconfigured to be “selective”. The keyboard
will be shown only to specific users. It will target users that are @mentioned in the text of the
message or to the sender of the message you reply to.
When the user clicks this button, a text message with the same text as the button will be sent, and can be
handled with events.NewMessage. You cannot distinguish between a button press and the user typing
and sending exactly the same text on their own.
static url(text, url=None)
Creates a new inline button to open the desired URL on click.
If no url is given, the text will be used as said URL instead.
You cannot detect that the user clicked this button directly.
When the user clicks this button, a confirmation box will be shown to the user asking whether they want
to open the displayed URL unless the domain is trusted, and once confirmed the URL will open in their
device.
2.36.3 ChatGetter
input_chat
This InputPeer is the input version of the chat where the message was sent. Similarly to input_sender,
this doesn’t have things like username or similar, but still useful in some cases.
Note that this might not be available if the library doesn’t have enough information available.
is_channel
True if the message was sent on a megagroup or channel.
is_group
True if the message was sent on a group or megagroup.
Returns None if there isn’t enough information (e.g. on events.MessageDeleted).
is_private
True if the message was sent as a private message.
Returns None if there isn’t enough information (e.g. on events.MessageDeleted).
2.36.4 Conversation
get_response(message=None, *, timeout=None)
Gets the next message that responds to a previous one. This is the method you need most of the time, along
with get_edit.
Args:
message (Message | int, optional): The message (or the message ID) for which a response is ex-
pected. By default this is the last sent message.
timeout (int | float, optional): If present, this timeout (in seconds) will override the per-
action timeout defined for the conversation.
async with client.conversation(...) as conv:
await conv.send_message('Hey, what is your name?')
mark_read(message=None)
Marks as read the latest received message if message is None. Otherwise, marks as read until the
given message (or message ID).
This is equivalent to calling client.send_read_acknowledge.
send_file(*args, **kwargs)
Sends a file in the context of this conversation. Shorthand for telethon.client.uploads.
UploadMethods.send_file with entity already set.
send_message(*args, **kwargs)
Sends a message in the context of this conversation. Shorthand for telethon.client.messages.
MessageMethods.send_message with entity already set.
wait_event(event, *, timeout=None)
Waits for a custom event to occur. Timeouts still apply.
Note: Only use this if there isn’t another method available! For example, don’t use wait_event for
new messages, since get_response already exists, etc.
Unless you’re certain that your code will run fast enough, generally you should get a “handle” of this
special coroutine before acting. In this example you will see how to wait for a user to join a group with
proper use of wait_event:
from telethon import TelegramClient, events
client = TelegramClient(...)
group_id = ...
This way your event can be registered before acting, since the response may arrive before your event was
registered. It depends on your use case since this also means the event can arrive before you send a previous
action.
wait_read(message=None, *, timeout=None)
Awaits for the sent message to be marked as read. Note that receiving a response doesn’t imply the message
was read, and this action will also trigger even without a response.
2.36.5 Dialog
draft (Draft): The draft object in this dialog. It will not be None, so you can call draft.
set_message(...).
is_user (bool): True if the entity is a User.
is_group (bool): True if the entity is a Chat or a Channel megagroup.
is_channel (bool): True if the entity is a Channel.
__str__()
Return str(self).
__weakref__
list of weak references to the object (if defined)
archive(folder=1)
Archives (or un-archives) this dialog.
Args:
folder (int, optional): The folder to which the dialog should be archived to.
If you want to “un-archive” it, use folder=0.
Returns: The Updates object that the request produces.
Example:
# Archiving
dialog.archive()
# Un-archiving
dialog.archive(0)
delete(revoke=False)
Deletes the dialog from your dialog list. If you own the channel this won’t destroy it, only delete it from
the list.
Shorthand for telethon.client.dialogs.DialogMethods.delete_dialog with entity
already set.
send_message(*args, **kwargs)
Sends a message to this dialog. This is just a wrapper around client.send_message(dialog.
input_entity, *args, **kwargs).
stringify()
to_dict()
2.36.6 Draft
__str__()
Return str(self).
__weakref__
list of weak references to the object (if defined)
delete()
Deletes this draft, and returns True on success.
entity
The entity that belongs to this dialog (user, chat or channel).
get_entity()
Returns entity but will make an API call if necessary.
get_input_entity()
Returns input_entity but will make an API call if necessary.
input_entity
Input version of the entity.
is_empty
Convenience bool to determine if the draft is empty or not.
raw_text
The raw (text without formatting) contained in the draft. It will be empty if there is no text (thus draft not
set).
send(clear=True, parse_mode=())
Sends the contents of this draft to the dialog. This is just a wrapper around send_message(dialog.
input_entity, *args, **kwargs).
set_message(text=None, reply_to=0, parse_mode=(), link_preview=None)
Changes the draft message on the Telegram servers. The changes are reflected in this object.
Parameters
• text (str) – New text of the draft. Preserved if left as None.
• reply_to (int) – Message ID to reply to. Preserved if left as 0, erased if set to None.
• link_preview (bool) – Whether to attach a web page preview. Preserved if left as
None.
• parse_mode (str) – The parse mode to be used for the text.
Return bool True on success.
stringify()
text
The markdown text contained in the draft. It will be empty if there is no text (and hence no draft is set).
to_dict()
2.36.7 File
class telethon.tl.custom.file.File(media)
Bases: object
Convenience class over media like photos or documents, which supports accessing the attributes in a more
convenient way.
If any of the attributes are not present in the current media, the properties will be None.
Note: This file ID may not work under user accounts, but should still be usable by bot accounts.
You can, however, still use it to identify a file in for example a database.
mime_type
The mime-type of this file.
name
The file name of this document.
performer
The performer of the song.
size
The size in bytes of this file.
For photos, this is the heaviest thumbnail, as it often repressents the largest dimensions.
sticker_set
The InputStickerSet to which the sticker file belongs.
title
The title of the song.
width
The width in pixels of this media if it’s a photo or a video.
2.36.8 Forward
Attributes:
original_fwd (MessageFwdHeader): The original MessageFwdHeader instance.
Any other attribute: Attributes not described here are the same as those available in the original
MessageFwdHeader.
2.36.9 InlineBuilder
class telethon.tl.custom.inlinebuilder.InlineBuilder(client)
Bases: object
Helper class to allow defining InlineQuery results.
Common arguments to all methods are explained here to avoid repetition:
text (str, optional): If present, the user will send a text message with this text upon being clicked.
link_preview (bool, optional): Whether to show a link preview in the sent text message or not.
geo (InputGeoPoint, GeoPoint, InputMediaVenue, MessageMediaVenue, optional): If present,
it may either be a geo point or a venue.
period (int, optional): The period in seconds to be used for geo points.
contact (InputMediaContact, MessageMediaContact, optional): If present, it must be the con-
tact information to send.
game (bool, optional): May be True to indicate that the game will be sent.
buttons (list, custom.Button, KeyboardButton, optional): Same as buttons for
client.send_message().
parse_mode (str, optional): Same as parse_mode for client.send_message().
id (str, optional): The string ID to use for this result. If not present, it will be the SHA256 hex-
adecimal digest of converting the created InputBotInlineResult with empty ID to bytes(), so
that the ID will be deterministic for the same input.
Note: If two inputs are exactly the same, their IDs will be the same too. If you send two
articles with the same ID, it will raise ResultIdDuplicateError. Consider giving them
an explicit ID if you need to send two results that are the same.
__weakref__
list of weak references to the object (if defined)
article(title, description=None, *, url=None, thumb=None, content=None, id=None, text=None,
parse_mode=(), link_preview=True, geo=None, period=60, contact=None, game=False, but-
tons=None)
Creates new inline result of article type.
Args:
title (str): The title to be shown for this result.
description (str, optional): Further explanation of what this result means.
url (str, optional): The URL to be shown for this result.
thumb (InputWebDocument, optional): The thumbnail to be shown for this result. For now it has
to be a InputWebDocument if present.
content (InputWebDocument, optional): The content to be shown for this result. For now it has to
be a InputWebDocument if present.
Example:
results = [
# Option with title and description sending a message.
builder.article(
title='First option',
description='This is the first option',
text='Text sent after clicking this option',
),
# Option with title URL to be opened when clicked.
builder.article(
title='Second option',
url='https://example.com',
text='Text sent if the user clicks the option and not the URL',
),
# Sending a message with buttons.
# You can use a list or a list of lists to include more buttons.
builder.article(
title='Third option',
text='Text sent with buttons below',
buttons=Button.url('https://example.com'),
),
]
results = [
# Sending just the photo when the user selects it.
builder.photo('/path/to/photo.jpg'),
2.36.10 InlineResult
document
Returns either the WebDocument content for normal results or the Document for media results.
download_media(*args, **kwargs)
Downloads the media in this result (if there is a document, the document will be downloaded; otherwise,
the photo will if present).
This is a wrapper around client.download_media.
message
The always-present BotInlineMessage that will be sent if click is called on this result.
photo
Returns either the WebDocument thumbnail for normal results or the Photo for media results.
title
The title for this inline result. It may be None.
type
The always-present type of this result. It will be one of: 'article', 'photo', 'gif',
'mpeg4_gif', 'video', 'audio', 'voice', 'document', 'location', 'venue',
'contact', 'game'.
You can access all of these constants through InlineResult, such as InlineResult.ARTICLE,
InlineResult.VIDEO_GIF, etc.
url
The URL present in this inline results. If you want to “click” this URL to open it in your browser, you
should use Python’s webbrowser.open(url) for such task.
2.36.11 InlineResults
__str__()
Return str(self).
__weakref__
list of weak references to the object (if defined)
results_valid()
Returns True if the cache time has not expired yet and the results can still be considered valid.
2.36.12 Message
sendergetter.SenderGetter, telethon.tl.tlobject.TLObject
This custom class aggregates both Message and MessageService to ease accessing their members.
Remember that this class implements ChatGetter and SenderGetter which means you have access to all
their sender and chat properties and methods.
Members:
out (bool): Whether the message is outgoing (i.e. you sent it from another session) or incoming (i.e.
someone else sent it).
Note that messages in your own chat are always incoming, but this member will be True if you send
a message to your own chat. Messages you forward to your chat are not considered outgoing, just like
official clients display them.
mentioned (bool): Whether you were mentioned in this message or not. Note that replies to your own
messages also count as mentions.
media_unread (bool): Whether you have read the media in this message or not, e.g. listened to the
voice note media.
silent (bool): Whether the message should notify people with sound or not. Previously used in channels,
but since 9 August 2019, it can also be used in private chats.
post (bool): Whether this message is a post in a broadcast channel or not.
from_scheduled (bool): Whether this message was originated from a previously-scheduled message or
not.
legacy (bool): Whether this is a legacy message or not.
edit_hide (bool): Whether the edited mark of this message is edited should be hidden (e.g. in GUI
clients) or shown.
pinned (bool): Whether this message is currently pinned or not.
noforwards (bool): Whether this message can be forwarded or not.
id (int): The ID of this message. This field is always present. Any other member is optional and may be
None.
from_id (Peer): The peer who sent this message, which is either PeerUser, PeerChat or PeerChannel.
This value will be None for anonymous messages.
peer_id (Peer): The peer to which this message was sent, which is either PeerUser, PeerChat or Peer-
Channel. This will always be present except for empty messages.
fwd_from (MessageFwdHeader): The original forward header if this message is a forward. You should
probably use the forward property instead.
via_bot_id (int): The ID of the bot used to send this message through its inline mode (e.g. “via @like”).
reply_to (MessageReplyHeader): The original reply header if this message is replying to another.
date (datetime): The UTC+0 datetime object indicating when this message was sent. This will
always be present except for empty messages.
message (str): The string text of the message for Message instances, which will be None for other
types of messages.
media (MessageMedia): The media sent with this message if any (such as photos, videos, documents,
gifs, stickers, etc.).
You may want to access the photo, document etc. properties instead.
If the media was not present or it was MessageMediaEmpty, this member will instead be None for
convenience.
reply_markup (ReplyMarkup): The reply markup for this message (which was sent either via a bot or
by a bot). You probably want to access buttons instead.
entities (List[MessageEntity]): The list of markup entities in this message, such as bold, italics, code,
hyperlinks, etc.
views (int): The number of views this message from a broadcast channel has. This is also present in
forwards.
forwards (int): The number of times this message has been forwarded.
replies (int): The number of times another message has replied to this message.
edit_date (datetime): The date when this message was last edited.
post_author (str): The display name of the message sender to show in messages sent to broadcast chan-
nels.
grouped_id (int): If this message belongs to a group of messages (photo albums or video albums), all
of them will have the same value here.
reactions (MessageReactions) Reactions to this message.
restriction_reason (List[RestrictionReason]) An optional list of reasons why this message was re-
stricted. If the list is None, this message has not been restricted.
ttl_period (int): The Time To Live period configured for this message. The message should be erased
from wherever it’s stored (memory, a local database, etc.) when datetime.now() > message.
date + timedelta(seconds=message.ttl_period).
action (MessageAction): The message action object of the message for MessageService instances, which
will be None for other types of messages.
action_entities
Returns a list of entities that took part in this action.
Possible cases for this are MessageActionChatAddUser, types.MessageActionChatCreate, MessageAc-
tionChatDeleteUser, MessageActionChatJoinedByLink MessageActionChatMigrateTo and MessageAc-
tionChannelMigrateFrom.
If the action is neither of those, the result will be None. If some entities could not be retrieved, the list
may contain some None items in it.
audio
The Document media in this message, if it’s an audio file.
button_count
Returns the total button count (sum of all buttons rows).
buttons
Returns a list of lists of MessageButton, if any.
Otherwise, it returns None.
click(i=None, j=None, *, text=None, filter=None, data=None, share_phone=None, share_geo=None,
password=None)
Calls SendVote with the specified poll option or button.click on the specified button.
Does nothing if the message is not a poll or has no buttons.
Args:
i (int | list): Clicks the i’th button or poll option (starting from the index 0). For multiple-choice
polls, a list with the indices should be used. Will raise IndexError if out of bounds. Ex-
ample:
j (int): Clicks the button at position (i, j), these being the indices for the (row, column) respectively.
Example:
# Click by text
await message.click(text='')
# Click by data
await message.click(data=b'payload')
client
Returns the TelegramClient that patched this message. This will only be present if you use the
friendly methods, it won’t be there if you invoke raw API methods manually, in which case you should
only access members, not properties.
contact
The MessageMediaContact in this message, if it’s a contact.
delete(*args, **kwargs)
Deletes the message. You’re responsible for checking whether you have the permission to do so, or
to except the error otherwise. Shorthand for telethon.client.messages.MessageMethods.
delete_messages with entity and message_ids already set.
If you need to delete more than one message at once, don’t use this delete method. Use a telethon.
client.telegramclient.TelegramClient instance directly.
dice
The MessageMediaDice in this message, if it’s a dice roll.
document
The Document media in this message, if any.
download_media(*args, **kwargs)
Downloads the media contained in the message, if any. Shorthand for telethon.client.
downloads.DownloadMethods.download_media with the message already set.
edit(*args, **kwargs)
Edits the message iff it’s outgoing. Shorthand for telethon.client.messages.
MessageMethods.edit_message with both entity and message already set.
Returns None if the message was incoming, or the edited Message otherwise.
Note: This is different from client.edit_message and will respect the previous state of the mes-
sage. For example, if the message didn’t have a link preview, the edit won’t add one by default, and you
should force it by setting it to True if you want it.
This is generally the most desired and convenient behaviour, and will work for link previews and message
buttons.
file
Returns a File wrapping the photo or document in this message. If the media type is different (polls,
games, none, etc.), this property will be None.
This instance lets you easily access other properties, such as file.id, file.name, etc., without having
to manually inspect the document.attributes.
forward
The Forward information if this message is a forwarded message.
forward_to(*args, **kwargs)
Forwards the message. Shorthand for telethon.client.messages.MessageMethods.
forward_messages with both messages and from_peer already set.
If you need to forward more than one message at once, don’t use this forward_to method. Use a
telethon.client.telegramclient.TelegramClient instance directly.
game
The Game media in this message, if it’s a game.
geo
The GeoPoint media in this message, if it has a location.
get_buttons()
Returns buttons when that property fails (this is rarely needed).
get_entities_text(cls=None)
Returns a list of (markup entity, inner text) (like bold or italics).
The markup entity is a MessageEntity that represents bold, italics, etc., and the inner text is the str inside
that markup entity.
For example:
Args:
cls (type): Returns entities matching this type only. For example, the following will print the text
for all code entities:
get_reply_message()
The Message that this message is replying to, or None.
The result will be cached after its first use.
gif
The Document media in this message, if it’s a “gif”.
“Gif” files by Telegram are normally .mp4 video files without sound, the so called “animated” media.
However, it may be the actual gif format if the file is too large.
invoice
The MessageMediaInvoice in this message, if it’s an invoice.
is_reply
True if the message is a reply to some other message.
Remember that you can access the ID of the message this one is replying to through reply_to.
reply_to_msg_id, and the Message object with get_reply_message().
mark_read()
Marks the message as read. Shorthand for client.send_read_acknowledge() with both
entity and message already set.
photo
The Photo media in this message, if any.
This will also return the photo for MessageService if its action is MessageActionChatEditPhoto, or if the
message has a web preview with a photo.
pin(*, notify=False, pm_oneside=False)
Pins the message. Shorthand for telethon.client.messages.MessageMethods.
pin_message with both entity and message already set.
poll
The MessageMediaPoll in this message, if it’s a poll.
raw_text
The raw message text, ignoring any formatting. Will be None for MessageService.
Setting a value to this field will erase the entities, unlike changing the message member.
reply(*args, **kwargs)
Replies to the message (as a reply). Shorthand for telethon.client.messages.
MessageMethods.send_message with both entity and reply_to already set.
reply_to_msg_id
Returns the message ID this message is replying to, if any. This is equivalent to accessing .reply_to.
reply_to_msg_id.
respond(*args, **kwargs)
Responds to the message (not as a reply). Shorthand for telethon.client.messages.
MessageMethods.send_message with entity already set.
sticker
The Document media in this message, if it’s a sticker.
text
The message text, formatted using the client’s default parse mode. Will be None for MessageService.
to_id
Returns the peer to which this message was sent to. This used to exist to infer the .peer_id.
unpin()
Unpins the message. Shorthand for telethon.client.messages.MessageMethods.
unpin_message with both entity and message already set.
venue
The MessageMediaVenue in this message, if it’s a venue.
via_bot
The bot User if the message was sent via said bot.
This will only be present if via_bot_id is not None and the entity is known.
via_input_bot
Returns the input variant of via_bot.
video
The Document media in this message, if it’s a video.
video_note
The Document media in this message, if it’s a video note.
voice
The Document media in this message, if it’s a voice note.
web_preview
The WebPage media in this message, if any.
2.36.13 MessageButton
Note: Message.buttons are instances of this type. If you want to define a reply markup for e.g. sending
messages, refer to Button instead.
Custom class that encapsulates a message button providing an abstraction to easily access some commonly
needed features (such as clicking the button itself).
Attributes:
button (KeyboardButton): The original KeyboardButton object.
__weakref__
list of weak references to the object (if defined)
click(share_phone=None, share_geo=None, *, password=None)
Emulates the behaviour of clicking this button.
If it’s a normal KeyboardButton with text, a message will be sent, and the sent Message returned.
If it’s an inline KeyboardButtonCallback with text and data, it will be “clicked” and the BotCallbackAn-
swer returned.
If it’s an inline KeyboardButtonSwitchInline button, the StartBotRequest will be invoked and the resulting
updates returned.
If it’s a KeyboardButtonUrl, the URL of the button will be passed to webbrowser.open and return
True on success.
If it’s a KeyboardButtonRequestPhone, you must indicate that you want to share_phone=True in
order to share it. Sharing it is not a default because it is a privacy concern and could happen accidentally.
You may also use share_phone=phone to share a specific number, in which case either str or Input-
MediaContact should be used.
If it’s a KeyboardButtonRequestGeoLocation, you must pass a tuple in share_geo=(longitude,
latitude). Note that Telegram seems to have some heuristics to determine impossible locations, so
changing this value a lot quickly may not work as expected. You may also pass a InputGeoPoint if you
find the order confusing.
client
Returns the telethon.client.telegramclient.TelegramClient instance that created this
instance.
data
The bytes data for KeyboardButtonCallback objects.
inline_query
The query str for KeyboardButtonSwitchInline objects.
text
The text string of the button.
url
The url str for KeyboardButtonUrl objects.
2.36.14 ParticipantPermissions
class telethon.tl.custom.participantpermissions.ParticipantPermissions(participant,
chat:
bool)
Bases: object
Participant permissions information.
The properties in this objects are boolean values indicating whether the user has the permission or not.
Example
permissions = ...
if permissions.is_banned:
"this user is banned"
elif permissions.is_admin:
"this user is an administrator"
__weakref__
list of weak references to the object (if defined)
add_admins
Whether the administrator can add new administrators with the same or less permissions than them.
anonymous
Whether the administrator will remain anonymous when sending messages.
ban_users
Whether the administrator can ban other users or not.
change_info
Whether the administrator can change the information about the chat, such as title or description.
delete_messages
Whether the administrator can delete messages from other participants.
edit_messages
Whether the administrator can edit messages.
has_default_permissions
Whether the user is a normal user of the chat (not administrator, but not banned either, and has no restric-
tions applied).
has_left
Whether the user left the chat.
invite_users
Whether the administrator can add new users to the chat.
is_admin
Whether the user is an administrator of the chat or not. The creator also counts as begin an administrator,
since they have all permissions.
is_banned
Whether the user is banned in the chat.
is_creator
Whether the user is the creator of the chat or not.
manage_call
Whether the user will be able to manage group calls.
pin_messages
Whether the administrator can pin messages or not.
post_messages
Whether the administrator can post messages in the broadcast channel.
2.36.15 QRLogin
2.36.16 SenderGetter
2.37 Utilities
class telethon.utils.AsyncClassWrapper(wrapped)
Bases: object
__getattr__(item)
__weakref__
list of weak references to the object (if defined)
telethon.utils.chunks(iterable, size=100)
Turns the given iterable into chunks of the specified size, which is 100 by default since that’s what Telegram
uses the most.
telethon.utils.decode_waveform(waveform)
Inverse operation of encode_waveform.
telethon.utils.encode_waveform(waveform)
Encodes the input bytes into a 5-bit byte-string to be used as a voice note’s waveform. See
decode_waveform for the reverse operation.
Example
chat = ...
file = 'my.ogg'
)]
telethon.utils.get_appropriated_part_size(file_size)
Gets the appropriated part size when uploading or downloading files, given an initial file size.
telethon.utils.get_attributes(file, *, attributes=None, mime_type=None,
force_document=False, voice_note=False, video_note=False,
supports_streaming=False, thumb=None)
Get a list of attributes for the given file and the mime type as a tuple ([attribute], mime_type).
telethon.utils.get_display_name(entity)
Gets the display name for the given User, Chat or Channel. Returns an empty string otherwise.
telethon.utils.get_extension(media)
Gets the corresponding extension for any Telegram media.
telethon.utils.get_inner_text(text, entities)
Gets the inner text that’s surrounded by the given entities. For instance: text = ‘hey!’, entity = MessageEntity-
Bold(2, 2) -> ‘y!’.
Parameters
• text – the original text.
• entities – the entity or entities that must be matched.
Important: This method does not validate for invalid general-purpose access hashes, unlike
get_input_peer. Consider using instead: get_input_channel(get_input_peer(channel)).
telethon.utils.get_input_chat_photo(photo)
Similar to get_input_peer(), but for chat photos
telethon.utils.get_input_dialog(dialog)
Similar to get_input_peer(), but for dialogs
telethon.utils.get_input_document(document)
Similar to get_input_peer(), but for documents
telethon.utils.get_input_geo(geo)
Similar to get_input_peer(), but for geo points
telethon.utils.get_input_group_call(call)
Similar to get_input_peer(), but for input calls.
telethon.utils.get_input_location(location)
Similar to get_input_peer(), but for input messages.
Note that this returns a tuple (dc_id, location), the dc_id being present if known.
telethon.utils.get_input_media(media, *, is_photo=False, attributes=None,
force_document=False, voice_note=False, video_note=False,
supports_streaming=False, ttl=None)
Similar to get_input_peer(), but for media.
If the media is InputFile and is_photo is known to be True, it will be treated as an InputMediaUploaded-
Photo. Else, the rest of parameters will indicate how to treat it.
telethon.utils.get_input_message(message)
Similar to get_input_peer(), but for input messages.
telethon.utils.get_input_peer(entity, allow_self=True, check_hash=True)
Gets the input peer for the given “entity” (user, chat or channel).
A TypeError is raised if the given entity isn’t a supported type or if check_hash is True but the entity’s
access_hash is None or the entity contains min information. In this case, the hash cannot be used for
general purposes, and thus is not returned to avoid any issues which can derive from invalid access hashes.
Note that check_hash is ignored if an input peer is already passed since in that case we assume the user
knows what they’re doing. This is key to getting entities by explicitly passing hash = 0.
telethon.utils.get_input_photo(photo)
Similar to get_input_peer(), but for photos
telethon.utils.get_input_user(entity)
Similar to get_input_peer(), but for InputUser’s alone.
Important: This method does not validate for invalid general-purpose access hashes, unlike
get_input_peer. Consider using instead: get_input_channel(get_input_peer(channel)).
telethon.utils.get_message_id(message)
Similar to get_input_peer(), but for message IDs.
telethon.utils.get_peer(peer)
telethon.utils.get_peer_id(peer, add_mark=True)
Convert the given peer into its marked ID by default.
This “mark” comes from the “bot api” format, and with it the peer type can be identified back. User ID is left
unmodified, chat ID is negated, and channel ID is “prefixed” with -100:
• user_id
• -chat_id
• -100channel_id
The original ID and the peer type class can be returned with a call to resolve_id(marked_id)().
telethon.utils.is_audio(file)
Returns True if the file has an audio mime type.
telethon.utils.is_gif(file)
Returns True if the file extension looks like a gif file to Telegram.
telethon.utils.is_image(file)
Returns True if the file extension looks like an image file to Telegram.
telethon.utils.is_list_like(obj)
Returns True if the given object looks like a list.
Checking if hasattr(obj, '__iter__') and ignoring str/bytes is not enough. Things like
open() are also iterable (and probably many other things), so just support the commonly known list-like
objects.
telethon.utils.is_video(file)
Returns True if the file has a video mime type.
telethon.utils.pack_bot_file_id(file)
Inverse operation for resolve_bot_file_id.
The only parameters this method will accept are Document and Photo, and it will return a variable-length
file_id string.
If an invalid parameter is given, it will return None.
telethon.utils.parse_phone(phone)
Parses the given phone, or returns None if it’s invalid.
telethon.utils.parse_username(username)
Parses the given username or channel access hash, given a string, username or URL. Returns a tuple consisting
of both the stripped, lowercase username and whether it is a joinchat/ hash (in which case is not lowercase’d).
Returns (None, False) if the username or link is not valid.
telethon.utils.resolve_bot_file_id(file_id)
Given a Bot API-style file_id, returns the media it represents. If the file_id is not valid, None is returned
instead.
Note that the file_id does not have information such as image dimensions or file size, so these will be zero
if present.
For thumbnails, the photo ID and hash will always be zero.
telethon.utils.resolve_id(marked_id)
Given a marked ID, returns the original ID and its Peer type.
telethon.utils.resolve_inline_message_id(inline_msg_id)
Resolves an inline message ID. Returns a tuple of (message id, peer, dc id, access hash)
The peer may either be a PeerUser referencing the user who sent the message via the bot in a private conver-
sation or small group chat, or a PeerChannel if the message was sent in a channel.
The access_hash does not have any use yet.
telethon.utils.resolve_invite_link(link)
Resolves the given invite link. Returns a tuple of (link creator user id, global chat id,
random int).
Note that for broadcast channels or with the newest link format, the link creator user ID will be zero to protect
their identity. Normal chats and megagroup channels will have such ID.
Note that the chat ID may not be accurate for chats with a link that were upgraded to megagroup, since the link
can remain the same, but the chat ID will be correct once a new link is generated.
telethon.utils.sanitize_parse_mode(mode)
Converts the given parse mode into an object with parse and unparse callable properties.
telethon.utils.split_text(text, entities, *, limit=4096, max_entities=100, split_at=(’\\n’, ’\\s’, ’.’))
Split a message text and entities into multiple messages, each with their own set of entities. This allows sending
a very large message as multiple messages while respecting the formatting.
Arguments
text (str): The message text.
entities (List[MessageEntity]) The formatting entities.
limit (int): The maximum message length of each individual message.
max_entities (int): The maximum amount of entities that will be present in each individual message.
split_at (Tuplel[str]): The list of regular expressions that will determine where to split the text. By
default, a newline is searched. If no newline is present, a space is searched. If no space is found, the
split will be made at any character.
The last expression should always match a character, or else the text will stop being splitted and the
resulting text may be larger than the limit.
Yields Pairs of (str, entities) with the split message.
Example
very_long_markdown_text = "..."
text, entities = markdown.parse(very_long_markdown_text)
telethon.utils.stripped_photo_to_jpg(stripped)
Adds the JPG header and footer to a stripped image.
Ported from https://github.com/telegramdesktop/tdesktop/blob/bec39d89e19670eb436dc794a8f20b657cb87c71/
Telegram/SourceFiles/ui/image/image.cpp#L225
These are the base errors that Telegram’s API may raise.
See RPC Errors for a more in-depth explanation on how to handle all known possible errors and learning to determine
what a method may raise.
Errors not related to the Telegram API itself
exception telethon.errors.common.AlreadyInConversationError
Bases: Exception
Occurs when another exclusive conversation is opened in the same chat.
__weakref__
list of weak references to the object (if defined)
exception telethon.errors.common.AuthKeyNotFound
Bases: Exception
The server claims it doesn’t know about the authorization key (session file) currently being used. This might be
because it either has never seen this authorization key, or it used to know about the authorization key but has
forgotten it, either temporarily or permanently (possibly due to server errors).
If the issue persists, you may need to recreate the session file and login again. This is not done automatically
because it is not possible to know if the issue is temporary or permanent.
__weakref__
list of weak references to the object (if defined)
exception telethon.errors.common.BadMessageError(request, code)
Bases: Exception
Occurs when handling a bad_message_notification.
ErrorMessages = {16: 'msg_id too low (most likely, client time is wrong it would be wo
__weakref__
list of weak references to the object (if defined)
exception telethon.errors.common.CdnFileTamperedError
Bases: telethon.errors.common.SecurityError
Occurs when there’s a hash mismatch between the decrypted CDN file and its expected hash.
exception telethon.errors.common.InvalidBufferError(payload)
Bases: BufferError
Occurs when the buffer is invalid, and may contain an HTTP error code. For instance, 404 means “forgot-
ten/broken authorization key”, while
__weakref__
list of weak references to the object (if defined)
exception telethon.errors.common.InvalidChecksumError(checksum, valid_checksum)
Bases: Exception
Occurs when using the TCP full mode and the checksum of a received packet doesn’t match the expected
checksum.
__weakref__
list of weak references to the object (if defined)
exception telethon.errors.common.MultiError
Bases: Exception
Exception container for multiple TLRequest’s.
static __new__(cls, exceptions, result, requests)
Create and return a new object. See help(type) for accurate signature.
__weakref__
list of weak references to the object (if defined)
exception telethon.errors.common.ReadCancelledError
Bases: Exception
Occurs when a read operation was cancelled.
__weakref__
list of weak references to the object (if defined)
exception telethon.errors.common.SecurityError(*args)
Bases: Exception
Generic security error, mostly used when generating a new AuthKey.
__weakref__
list of weak references to the object (if defined)
exception telethon.errors.common.TypeNotFoundError(invalid_constructor_id, remain-
ing)
Bases: Exception
Occurs when a type is not found, for example, when trying to read a TLObject with an invalid constructor code.
__weakref__
list of weak references to the object (if defined)
exception telethon.errors.rpcbaseerrors.AuthKeyError(request, message, code=None)
Bases: telethon.errors.rpcbaseerrors.RPCError
Errors related to invalid authorization key, like AUTH_KEY_DUPLICATED which can cause the connection to
fail.
code = 406
message = 'AUTH_KEY'
exception telethon.errors.rpcbaseerrors.BadRequestError(request, message,
code=None)
Bases: telethon.errors.rpcbaseerrors.RPCError
The query contains errors. In the event that a request was created using a form and contains user generated data,
the user should be notified that the data must be corrected before the query is repeated.
code = 400
message = 'BAD_REQUEST'
telethon.errors.rpcbaseerrors.BotTimeout
alias of telethon.errors.rpcbaseerrors.TimedOutError
exception telethon.errors.rpcbaseerrors.FloodError(request, message, code=None)
Bases: telethon.errors.rpcbaseerrors.RPCError
The maximum allowed number of attempts to invoke the given method with the given input parameters has been
exceeded. For example, in an attempt to request a large number of text messages (SMS) for the same phone
number.
code = 420
message = 'FLOOD'
exception telethon.errors.rpcbaseerrors.ForbiddenError(request, message,
code=None)
Bases: telethon.errors.rpcbaseerrors.RPCError
Privacy violation. For example, an attempt to write a message to someone who has blacklisted the current user.
code = 403
message = 'FORBIDDEN'
exception telethon.errors.rpcbaseerrors.InvalidDCError(request, message,
code=None)
Bases: telethon.errors.rpcbaseerrors.RPCError
The request must be repeated, but directed to a different data center.
code = 303
message = 'ERROR_SEE_OTHER'
exception telethon.errors.rpcbaseerrors.NotFoundError(request, message,
code=None)
Bases: telethon.errors.rpcbaseerrors.RPCError
An attempt to invoke a non-existent object, such as a method.
code = 404
message = 'NOT_FOUND'
exception telethon.errors.rpcbaseerrors.RPCError(request, message, code=None)
Bases: Exception
Base class for all Remote Procedure Call errors.
__reduce__()
Helper for pickle.
__weakref__
list of weak references to the object (if defined)
code = None
message = None
exception telethon.errors.rpcbaseerrors.ServerError(request, message, code=None)
Bases: telethon.errors.rpcbaseerrors.RPCError
An internal server error occurred while a request was being processed for example, there was a disruption while
accessing a database or file storage.
code = 500
message = 'INTERNAL'
exception telethon.errors.rpcbaseerrors.TimedOutError(request, message,
code=None)
Bases: telethon.errors.rpcbaseerrors.RPCError
Clicking the inline buttons of bots that never (or take to long to) call answerCallbackQuery will result in
this “special” RPCError.
code = 503
message = 'Timeout'
exception telethon.errors.rpcbaseerrors.UnauthorizedError(request, message,
code=None)
Bases: telethon.errors.rpcbaseerrors.RPCError
There was an unauthorized attempt to use functionality available only to authorized users.
code = 401
message = 'UNAUTHORIZED'
2.39 Sessions
These are the different built-in session storage that you may subclass.
class telethon.sessions.abstract.Session
Bases: abc.ABC
__weakref__
list of weak references to the object (if defined)
auth_key
Returns an AuthKey instance associated with the saved data center, or None if a new one should be
generated.
cache_file(md5_digest, file_size, instance)
Caches the given file information persistently, so that it doesn’t need to be re-uploaded in case the file is
used again.
The instance will be either an InputPhoto or InputDocument, both with an .id and .
access_hash attributes.
clone(to_instance=None)
Creates a clone of this session file.
close()
Called on client disconnection. Should be used to free any used resources. Can be left empty if none.
dc_id
Returns the currently-used data center ID.
delete()
Called upon client.log_out(). Should delete the stored information from disk since it’s not valid anymore.
get_file(md5_digest, file_size, cls)
Returns an instance of cls if the md5_digest and file_size match an existing saved record.
The class will either be an InputPhoto or InputDocument, both with two parameters id and
access_hash in that order.
get_input_entity(key)
Turns the given key into an InputPeer (e.g. InputPeerUser). The library uses this method whenever
an InputPeer is needed to suit several purposes (e.g. user only provided its ID or wishes to use a cached
username to avoid extra RPC).
get_update_state(entity_id)
Returns the UpdateState associated with the given entity_id. If the entity_id is 0, it should
return the UpdateState for no specific channel (the “general” state). If no state is known it should
return None.
get_update_states()
Returns an iterable over all known pairs of (entity ID, update state).
classmethod list_sessions()
Lists available sessions. Not used by the library itself.
port
Returns the port to which the library should connect to.
process_entities(tlo)
Processes the input TLObject or list and saves whatever information is relevant (e.g., ID or access
hash).
save()
Called whenever important properties change. It should make persist the relevant session information to
disk.
server_address
Returns the server address where the library should connect to.
set_dc(dc_id, server_address, port)
Sets the information of the data center address and port that the library should connect to, as well as the
data center ID, which is currently unused.
set_update_state(entity_id, state)
Sets the given UpdateState for the specified entity_id, which should be 0 if the UpdateState
is the “general” state (and not for any specific channel).
takeout_id
Returns an ID of the takeout process initialized for this session, or None if there’s no were any unfinished
takeout requests.
class telethon.sessions.memory.MemorySession
Bases: telethon.sessions.abstract.Session
auth_key
Returns an AuthKey instance associated with the saved data center, or None if a new one should be
generated.
cache_file(md5_digest, file_size, instance)
Caches the given file information persistently, so that it doesn’t need to be re-uploaded in case the file is
used again.
The instance will be either an InputPhoto or InputDocument, both with an .id and .
access_hash attributes.
close()
Called on client disconnection. Should be used to free any used resources. Can be left empty if none.
dc_id
Returns the currently-used data center ID.
delete()
Called upon client.log_out(). Should delete the stored information from disk since it’s not valid anymore.
get_entity_rows_by_id(id, exact=True)
get_entity_rows_by_name(name)
get_entity_rows_by_phone(phone)
get_entity_rows_by_username(username)
get_file(md5_digest, file_size, cls)
Returns an instance of cls if the md5_digest and file_size match an existing saved record.
The class will either be an InputPhoto or InputDocument, both with two parameters id and
access_hash in that order.
get_input_entity(key)
Turns the given key into an InputPeer (e.g. InputPeerUser). The library uses this method whenever
an InputPeer is needed to suit several purposes (e.g. user only provided its ID or wishes to use a cached
username to avoid extra RPC).
get_update_state(entity_id)
Returns the UpdateState associated with the given entity_id. If the entity_id is 0, it should
return the UpdateState for no specific channel (the “general” state). If no state is known it should
return None.
get_update_states()
Returns an iterable over all known pairs of (entity ID, update state).
port
Returns the port to which the library should connect to.
process_entities(tlo)
Processes the input TLObject or list and saves whatever information is relevant (e.g., ID or access
hash).
save()
Called whenever important properties change. It should make persist the relevant session information to
disk.
server_address
Returns the server address where the library should connect to.
set_dc(dc_id, server_address, port)
Sets the information of the data center address and port that the library should connect to, as well as the
data center ID, which is currently unused.
set_update_state(entity_id, state)
Sets the given UpdateState for the specified entity_id, which should be 0 if the UpdateState
is the “general” state (and not for any specific channel).
takeout_id
Returns an ID of the takeout process initialized for this session, or None if there’s no were any unfinished
takeout requests.
class telethon.sessions.sqlite.SQLiteSession(session_id=None)
Bases: telethon.sessions.memory.MemorySession
This session contains the required information to login into your Telegram account. NEVER give the saved
session file to anyone, since they would gain instant access to all your messages and contacts.
If you think the session has been compromised, close all the sessions through an official Telegram client to
revoke the authorization.
auth_key
cache_file(md5_digest, file_size, instance)
Caches the given file information persistently, so that it doesn’t need to be re-uploaded in case the file is
used again.
The instance will be either an InputPhoto or InputDocument, both with an .id and .
access_hash attributes.
clone(to_instance=None)
Creates a clone of this session file.
close()
Closes the connection unless we’re working in-memory
delete()
Deletes the current session file
get_entity_rows_by_id(id, exact=True)
get_entity_rows_by_name(name)
get_entity_rows_by_phone(phone)
get_entity_rows_by_username(username)
get_file(md5_digest, file_size, cls)
Returns an instance of cls if the md5_digest and file_size match an existing saved record.
The class will either be an InputPhoto or InputDocument, both with two parameters id and
access_hash in that order.
get_update_state(entity_id)
Returns the UpdateState associated with the given entity_id. If the entity_id is 0, it should
return the UpdateState for no specific channel (the “general” state). If no state is known it should
return None.
get_update_states()
Returns an iterable over all known pairs of (entity ID, update state).
classmethod list_sessions()
Lists all the sessions of the users who have ever connected using this client and never logged out
process_entities(tlo)
Processes all the found entities on the given TLObject, unless .save_entities is False.
save()
Saves the current session object as session_user_id.session
set_dc(dc_id, server_address, port)
Sets the information of the data center address and port that the library should connect to, as well as the
data center ID, which is currently unused.
set_update_state(entity_id, state)
Sets the given UpdateState for the specified entity_id, which should be 0 if the UpdateState
is the “general” state (and not for any specific channel).
takeout_id
class telethon.sessions.string.StringSession(string: str = None)
Bases: telethon.sessions.memory.MemorySession
This session file can be easily saved and loaded as a string. According to the initial design, it contains only the
data that is necessary for successful connection and authentication, so takeout ID is not stored.
It is thought to be used where you don’t want to create any on-disk files but would still like to be able to save
and load existing sessions by other means.
You can use custom encode and decode functions, if present:
• encode definition must be def encode(value: bytes) -> str:.
The only part about network that you should worry about are the different connection modes, which are the following:
class telethon.network.connection.tcpfull.ConnectionTcpFull(ip, port, dc_id,
*, loggers,
proxy=None, lo-
cal_addr=None)
Bases: telethon.network.connection.connection.Connection
Default Telegram mode. Sends 12 additional bytes and needs to calculate the CRC value of the packet itself.
packet_codec
alias of FullPacketCodec
class telethon.network.connection.tcpfull.FullPacketCodec(connection)
Bases: telethon.network.connection.connection.PacketCodec
encode_packet(data)
Encodes single packet and returns encoded bytes.
read_packet(reader)
Reads single packet from reader object that should have readexactly(n) method.
tag = None
class telethon.network.connection.tcpabridged.AbridgedPacketCodec(connection)
Bases: telethon.network.connection.connection.PacketCodec
encode_packet(data)
Encodes single packet and returns encoded bytes.
obfuscate_tag = b'\xef\xef\xef\xef'
read_packet(reader)
Reads single packet from reader object that should have readexactly(n) method.
tag = b'\xef'
class telethon.network.connection.tcpabridged.ConnectionTcpAbridged(ip, port,
dc_id, *,
loggers,
proxy=None,
lo-
cal_addr=None)
Bases: telethon.network.connection.connection.Connection
This is the mode with the lowest overhead, as it will only require 1 byte if the packet length is less than 508
bytes (127 << 2, which is very common).
packet_codec
alias of AbridgedPacketCodec
class telethon.network.connection.tcpintermediate.ConnectionTcpIntermediate(ip,
port,
dc_id,
*,
log-
gers,
proxy=None,
lo-
cal_addr=None)
Bases: telethon.network.connection.connection.Connection
Intermediate mode between ConnectionTcpFull and ConnectionTcpAbridged. Always sends 4 ex-
tra bytes for the packet length.
packet_codec
alias of IntermediatePacketCodec
class telethon.network.connection.tcpintermediate.IntermediatePacketCodec(connection)
Bases: telethon.network.connection.connection.PacketCodec
encode_packet(data)
Encodes single packet and returns encoded bytes.
obfuscate_tag = b'\xee\xee\xee\xee'
read_packet(reader)
Reads single packet from reader object that should have readexactly(n) method.
tag = b'\xee\xee\xee\xee'
class telethon.network.connection.tcpintermediate.RandomizedIntermediatePacketCodec(connectio
Bases: telethon.network.connection.tcpintermediate.IntermediatePacketCodec
Data packets are aligned to 4bytes. This codec adds random bytes of size from 0 to 3 bytes, which are ignored
by decoder.
encode_packet(data)
Encodes single packet and returns encoded bytes.
obfuscate_tag = b'\xdd\xdd\xdd\xdd'
read_packet(reader)
Reads single packet from reader object that should have readexactly(n) method.
tag = None
class telethon.network.connection.tcpobfuscated.ConnectionTcpObfuscated(ip,
port,
dc_id,
*,
log-
gers,
proxy=None,
lo-
cal_addr=None)
Bases: telethon.network.connection.connection.ObfuscatedConnection
Mode that Telegram defines as “obfuscated2”. Encodes the packet just like ConnectionTcpAbridged, but
encrypts every message with a randomly generated key using the AES-CTR mode so the packets are harder to
discern.
obfuscated_io
alias of ObfuscatedIO
packet_codec
alias of telethon.network.connection.tcpabridged.AbridgedPacketCodec
class telethon.network.connection.tcpobfuscated.ObfuscatedIO(connection)
Bases: object
__weakref__
list of weak references to the object (if defined)
header = None
static init_header(packet_codec)
readexactly(n)
write(data)
class telethon.network.connection.http.ConnectionHttp(ip, port, dc_id, *, log-
gers, proxy=None, lo-
cal_addr=None)
Bases: telethon.network.connection.connection.Connection
connect(timeout=None, ssl=None)
Establishes a connection with the server.
packet_codec
alias of HttpPacketCodec
class telethon.network.connection.http.HttpPacketCodec(connection)
Bases: telethon.network.connection.connection.PacketCodec
encode_packet(data)
Encodes single packet and returns encoded bytes.
obfuscate_tag = None
read_packet(reader)
Reads single packet from reader object that should have readexactly(n) method.
tag = None
2.41 Helpers
__repr__()
Return repr(self).
__str__()
Return str(self).
__weakref__
list of weak references to the object (if defined)
telethon.helpers.add_surrogate(text)
telethon.helpers.del_surrogate(text)
telethon.helpers.ensure_parent_dir_exists(file_path)
Ensures that the parent directory exists
telethon.helpers.generate_key_data_from_nonce(server_nonce, new_nonce)
Generates the key data corresponding to the given nonce
telethon.helpers.generate_random_long(signed=True)
Generates a random long integer (8 bytes), which is optionally signed
telethon.helpers.get_running_loop()
telethon.helpers.retry_range(retries, force_retry=True)
Generates an integer sequence starting from 1. If retries is not a zero or a positive integer value, the sequence
will be infinite, otherwise it will end at retries + 1.
telethon.helpers.strip_text(text, entities)
Strips whitespace from the given surrogated text modifying the provided entities, also removing any empty
(0-length) entities.
This assumes that the length of entities is greater or equal to 0, and that no entity is out of bounds.
telethon.helpers.within_surrogate(text, index, *, length=None)
True if index is within a surrogate (before and after it, not at!).
t telethon.sessions.memory, 257
telethon.client.account, 156 telethon.sessions.sqlite, 258
telethon.client.auth, 157 telethon.sessions.string, 259
telethon.client.bots, 161 telethon.tl.custom, 218
telethon.client.buttons, 162 telethon.tl.custom.adminlogevent, 218
telethon.client.chats, 164 telethon.tl.custom.button, 221
telethon.client.dialogs, 172 telethon.tl.custom.chatgetter, 224
telethon.client.downloads, 176 telethon.tl.custom.conversation, 225
telethon.client.messageparse, 180 telethon.tl.custom.dialog, 227
telethon.client.messages, 180 telethon.tl.custom.draft, 228
telethon.client.telegrambaseclient, 151 telethon.tl.custom.file, 229
telethon.client.telegramclient, 150 telethon.tl.custom.forward, 230
telethon.client.updates, 190 telethon.tl.custom.inlinebuilder, 231
telethon.client.uploads, 193 telethon.tl.custom.inlineresult, 234
telethon.client.users, 197 telethon.tl.custom.inlineresults, 235
telethon.errors.common, 253 telethon.tl.custom.message, 238
telethon.errors.rpcbaseerrors, 254 telethon.tl.custom.messagebutton, 245
telethon.events, 216 telethon.tl.custom.participantpermissions,
telethon.events.album, 213 246
telethon.events.callbackquery, 209 telethon.tl.custom.qrlogin, 247
telethon.events.chataction, 202 telethon.tl.custom.sendergetter, 248
telethon.events.common, 200 telethon.utils, 248
telethon.events.inlinequery, 211
telethon.events.messagedeleted, 207
telethon.events.messageedited, 206
telethon.events.messageread, 208
telethon.events.newmessage, 201
telethon.events.raw, 216
telethon.events.userupdate, 204
telethon.helpers, 262
telethon.network.connection.http, 262
telethon.network.connection.tcpabridged,
260
telethon.network.connection.tcpfull, 260
telethon.network.connection.tcpintermediate,
260
telethon.network.connection.tcpobfuscated,
261
telethon.sessions.abstract, 256
265
Telethon Documentation, Release 1.26.0
267
Telethon Documentation, Release 1.26.0
268 Index
Telethon Documentation, Release 1.26.0
Index 269
Telethon Documentation, Release 1.26.0
(telethon.tl.custom.adminlogevent.AdminLogEventcode (telethon.errors.rpcbaseerrors.BadRequestError
attribute), 219 attribute), 254
changed_signatures code (telethon.errors.rpcbaseerrors.FloodError at-
(telethon.tl.custom.adminlogevent.AdminLogEvent tribute), 255
attribute), 219 code (telethon.errors.rpcbaseerrors.ForbiddenError at-
changed_sticker_set tribute), 255
(telethon.tl.custom.adminlogevent.AdminLogEventcode (telethon.errors.rpcbaseerrors.InvalidDCError at-
attribute), 219 tribute), 255
changed_title (telethon.tl.custom.adminlogevent.AdminLogEvent
code (telethon.errors.rpcbaseerrors.NotFoundError at-
attribute), 219 tribute), 255
changed_user_volume code (telethon.errors.rpcbaseerrors.RPCError at-
(telethon.tl.custom.adminlogevent.AdminLogEvent tribute), 255
attribute), 220 code (telethon.errors.rpcbaseerrors.ServerError at-
changed_username (telethon.tl.custom.adminlogevent.AdminLogEvent tribute), 255
attribute), 220 code (telethon.errors.rpcbaseerrors.TimedOutError at-
chat (telethon.tl.custom.chatgetter.ChatGetter at- tribute), 256
tribute), 224 code (telethon.errors.rpcbaseerrors.UnauthorizedError
chat_id (telethon.tl.custom.chatgetter.ChatGetter at- attribute), 256
tribute), 224 connect() (telethon.client.telegrambaseclient.TelegramBaseClient
chat_instance (telethon.events.callbackquery.CallbackQuery.Event method), 154
attribute), 210 connect() (telethon.network.connection.http.ConnectionHttp
ChatAction (class in telethon.events.chataction), 202 method), 262
ChatAction.Event (class in ConnectionHttp (class in
telethon.events.chataction), 203 telethon.network.connection.http), 262
ChatGetter (class in telethon.tl.custom.chatgetter), ConnectionTcpAbridged (class in
224 telethon.network.connection.tcpabridged),
ChatMethods (class in telethon.client.chats), 164 260
chunks() (in module telethon.utils), 249 ConnectionTcpFull (class in
clear() (telethon.tl.custom.button.Button static telethon.network.connection.tcpfull), 260
method), 222 ConnectionTcpIntermediate (class in
click() (telethon.tl.custom.inlineresult.InlineResult telethon.network.connection.tcpintermediate),
method), 234 260
click() (telethon.tl.custom.message.Message method), ConnectionTcpObfuscated (class in
240 telethon.network.connection.tcpobfuscated),
click() (telethon.tl.custom.messagebutton.MessageButton 261
method), 245 contact (telethon.events.userupdate.UserUpdate.Event
client (telethon.events.common.EventCommon at- attribute), 205
tribute), 200 CONTACT (telethon.tl.custom.inlineresult.InlineResult at-
client (telethon.tl.custom.message.Message attribute), tribute), 234
242 contact (telethon.tl.custom.message.Message at-
client (telethon.tl.custom.messagebutton.MessageButton tribute), 242
attribute), 245 Conversation (class in
clone() (telethon.sessions.abstract.Session method), telethon.tl.custom.conversation), 225
256 conversation() (telethon.client.dialogs.DialogMethods
clone() (telethon.sessions.sqlite.SQLiteSession method), 172
method), 259
close() (telethon.sessions.abstract.Session method), D
256 data (telethon.events.callbackquery.CallbackQuery.Event
close() (telethon.sessions.memory.MemorySession attribute), 210
method), 257 data (telethon.tl.custom.messagebutton.MessageButton
close() (telethon.sessions.sqlite.SQLiteSession attribute), 245
method), 259 date (telethon.tl.custom.adminlogevent.AdminLogEvent
code (telethon.errors.rpcbaseerrors.AuthKeyError at- attribute), 220
tribute), 254 dc_id (telethon.sessions.abstract.Session attribute), 256
270 Index
Telethon Documentation, Release 1.26.0
Index 271
Telethon Documentation, Release 1.26.0
encode_packet() (telethon.network.connection.tcpintermediate.IntermediatePacketCodec
forward_to() (telethon.events.album.Album.Event
method), 261 method), 214
encode_packet() (telethon.network.connection.tcpintermediate.RandomizedIntermediatePacketCodec
forward_to() (telethon.tl.custom.message.Message
method), 261 method), 243
encode_waveform() (in module telethon.utils), 249 FullPacketCodec (class in
end_takeout() (telethon.client.account.AccountMethods telethon.network.connection.tcpfull), 260
method), 156
ensure_parent_dir_exists() (in module G
telethon.helpers), 263 GAME (telethon.tl.custom.inlineresult.InlineResult at-
entity (telethon.tl.custom.draft.Draft attribute), 229 tribute), 234
ErrorMessages (telethon.errors.common.BadMessageError game (telethon.tl.custom.message.Message attribute),
attribute), 253 243
EventBuilder (class in telethon.events.common), 200 game() (telethon.tl.custom.button.Button static
EventBuilderDict (class in telethon.client.updates), method), 222
190 game() (telethon.tl.custom.inlinebuilder.InlineBuilder
EventCommon (class in telethon.events.common), 200 method), 233
expires (telethon.tl.custom.qrlogin.QRLogin at- generate_key_data_from_nonce() (in module
tribute), 247 telethon.helpers), 263
ext (telethon.tl.custom.file.File attribute), 230 generate_random_long() (in module
extend() (telethon.events.album.AlbumHack method), telethon.helpers), 263
215 geo (telethon.events.inlinequery.InlineQuery.Event at-
tribute), 213
F geo (telethon.events.userupdate.UserUpdate.Event at-
File (class in telethon.tl.custom.file), 229 tribute), 205
file (telethon.tl.custom.message.Message attribute), geo (telethon.tl.custom.message.Message attribute), 243
242 get_added_by() (telethon.events.chataction.ChatAction.Event
filter() (telethon.events.album.Album method), 215 method), 203
filter() (telethon.events.callbackquery.CallbackQuery get_admin_log() (telethon.client.chats.ChatMethods
method), 211 method), 167
filter() (telethon.events.common.EventBuilder get_appropriated_part_size() (in module
method), 200 telethon.utils), 249
filter() (telethon.events.inlinequery.InlineQuery get_attributes() (in module telethon.utils), 249
method), 213 get_buttons() (telethon.tl.custom.message.Message
filter() (telethon.events.messageread.MessageRead method), 243
method), 209 get_chat() (telethon.tl.custom.chatgetter.ChatGetter
filter() (telethon.events.newmessage.NewMessage method), 224
method), 202 get_dialogs() (telethon.client.dialogs.DialogMethods
filter() (telethon.events.raw.Raw method), 216 method), 175
flood_sleep_threshold get_display_name() (in module telethon.utils), 249
(telethon.client.telegrambaseclient.TelegramBaseClient
get_drafts() (telethon.client.dialogs.DialogMethods
attribute), 155 method), 175
FloodError, 254 get_edit() (telethon.tl.custom.conversation.Conversation
ForbiddenError, 255 method), 225
force_reply() (telethon.tl.custom.button.Button get_entities_text()
static method), 222 (telethon.tl.custom.message.Message method),
Forward (class in telethon.tl.custom.forward), 230 243
forward (telethon.events.album.Album.Event attribute), get_entity() (telethon.client.users.UserMethods
214 method), 197
forward (telethon.tl.custom.message.Message at- get_entity() (telethon.tl.custom.draft.Draft
tribute), 243 method), 229
forward_messages() get_entity_rows_by_id()
(telethon.client.messages.MessageMethods (telethon.sessions.memory.MemorySession
method), 183 method), 257
272 Index
Telethon Documentation, Release 1.26.0
Index 273
Telethon Documentation, Release 1.26.0
274 Index
Telethon Documentation, Release 1.26.0
Index 275
Telethon Documentation, Release 1.26.0
276 Index
Telethon Documentation, Release 1.26.0
Index 277
Telethon Documentation, Release 1.26.0
278 Index
Telethon Documentation, Release 1.26.0
Index 279
Telethon Documentation, Release 1.26.0
V
VENUE (telethon.tl.custom.inlineresult.InlineResult at-
280 Index