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, orNone
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 anInputPhoto
orInputDocument
, 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 themd5_digest
andfile_size
match an existing saved record. The class will either be anInputPhoto
orInputDocument
, both with two parametersid
andaccess_hash
in that order.
-
get_input_entity
(key)¶ Turns the given key into an
InputPeer
(e.g.InputPeerUser
). The library uses this method whenever anInputPeer
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 givenentity_id
. If theentity_id
is 0, it should return theUpdateState
for no specific channel (the “general” state). If no state is known it shouldreturn 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
orlist
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 specifiedentity_id
, which should be 0 if theUpdateState
is the “general” state (and not for any specific channel).
-
-
class
telethon.sessions.memory.
MemorySession
¶ Bases:
telethon.sessions.abstract.Session
-
auth_key
¶ Returns an
AuthKey
instance associated with the saved data center, orNone
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 anInputPhoto
orInputDocument
, 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 themd5_digest
andfile_size
match an existing saved record. The class will either be anInputPhoto
orInputDocument
, both with two parametersid
andaccess_hash
in that order.
-
get_input_entity
(key)¶ Turns the given key into an
InputPeer
(e.g.InputPeerUser
). The library uses this method whenever anInputPeer
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 givenentity_id
. If theentity_id
is 0, it should return theUpdateState
for no specific channel (the “general” state). If no state is known it shouldreturn 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
orlist
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 specifiedentity_id
, which should be 0 if theUpdateState
is the “general” state (and not for any specific channel).
-
-
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 anInputPhoto
orInputDocument
, 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 themd5_digest
andfile_size
match an existing saved record. The class will either be anInputPhoto
orInputDocument
, both with two parametersid
andaccess_hash
in that order.
-
get_update_state
(entity_id)¶ Returns the
UpdateState
associated with the givenentity_id
. If theentity_id
is 0, it should return theUpdateState
for no specific channel (the “general” state). If no state is known it shouldreturn 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 specifiedentity_id
, which should be 0 if theUpdateState
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
anddecode
functions, if present:encode
definition must bedef encode(value: bytes) -> str:
.decode
definition must bedef decode(value: str) -> bytes:
.
-
static
decode
(x: str) → bytes¶
-
static
encode
(x: bytes) → str¶
-
save
()¶ Called whenever important properties change. It should make persist the relevant session information to disk.