Sessions
These are the different built-in session storage that you may subclass.
- class telethon.sessions.abstract.Session
Bases:
abc.ABC- __annotations__ = {}
- __weakref__
list of weak references to the object (if defined)
- abstract property auth_key
Returns an
AuthKeyinstance associated with the saved data center, orNoneif a new one should be generated.
- abstract 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
instancewill be either anInputPhotoorInputDocument, both with an.idand.access_hashattributes.
- 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.
- abstract property dc_id
Returns the currently-used data center ID.
- abstract delete()
Called upon client.log_out(). Should delete the stored information from disk since it’s not valid anymore.
- abstract get_file(md5_digest, file_size, cls)
Returns an instance of
clsif themd5_digestandfile_sizematch an existing saved record. The class will either be anInputPhotoorInputDocument, both with two parametersidandaccess_hashin that order.
- abstract get_input_entity(key)
Turns the given key into an
InputPeer(e.g.InputPeerUser). The library uses this method whenever anInputPeeris needed to suit several purposes (e.g. user only provided its ID or wishes to use a cached username to avoid extra RPC).
- abstract get_update_state(entity_id)
Returns the
UpdateStateassociated with the givenentity_id. If theentity_idis 0, it should return theUpdateStatefor no specific channel (the “general” state). If no state is known it shouldreturn None.
- abstract 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.
- abstract property port
Returns the port to which the library should connect to.
- abstract process_entities(tlo)
Processes the input
TLObjectorlistand saves whatever information is relevant (e.g., ID or access hash).
- abstract save()
Called whenever important properties change. It should make persist the relevant session information to disk.
- abstract property server_address
Returns the server address where the library should connect to.
- abstract 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.
- abstract set_update_state(entity_id, state)
Sets the given
UpdateStatefor the specifiedentity_id, which should be 0 if theUpdateStateis the “general” state (and not for any specific channel).
- abstract property takeout_id
Returns an ID of the takeout process initialized for this session, or
Noneif there’s no were any unfinished takeout requests.
- class telethon.sessions.memory.MemorySession
Bases:
telethon.sessions.abstract.Session- __annotations__ = {}
- property auth_key
Returns an
AuthKeyinstance associated with the saved data center, orNoneif 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
instancewill be either anInputPhotoorInputDocument, both with an.idand.access_hashattributes.
- close()
Called on client disconnection. Should be used to free any used resources. Can be left empty if none.
- property 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
clsif themd5_digestandfile_sizematch an existing saved record. The class will either be anInputPhotoorInputDocument, both with two parametersidandaccess_hashin that order.
- get_input_entity(key)
Turns the given key into an
InputPeer(e.g.InputPeerUser). The library uses this method whenever anInputPeeris 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
UpdateStateassociated with the givenentity_id. If theentity_idis 0, it should return theUpdateStatefor 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).
- property port
Returns the port to which the library should connect to.
- process_entities(tlo)
Processes the input
TLObjectorlistand 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.
- property 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
UpdateStatefor the specifiedentity_id, which should be 0 if theUpdateStateis the “general” state (and not for any specific channel).
- property takeout_id
Returns an ID of the takeout process initialized for this session, or
Noneif there’s no were any unfinished takeout requests.
- class telethon.sessions.sqlite.SQLiteSession(session_id=None)
Bases:
telethon.sessions.memory.MemorySessionThis 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.
- __annotations__ = {}
- property auth_key
Returns an
AuthKeyinstance associated with the saved data center, orNoneif 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
instancewill be either anInputPhotoorInputDocument, both with an.idand.access_hashattributes.
- 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
clsif themd5_digestandfile_sizematch an existing saved record. The class will either be anInputPhotoorInputDocument, both with two parametersidandaccess_hashin that order.
- get_update_state(entity_id)
Returns the
UpdateStateassociated with the givenentity_id. If theentity_idis 0, it should return theUpdateStatefor 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
UpdateStatefor the specifiedentity_id, which should be 0 if theUpdateStateis the “general” state (and not for any specific channel).
- property takeout_id
Returns an ID of the takeout process initialized for this session, or
Noneif there’s no were any unfinished takeout requests.
- class telethon.sessions.string.StringSession(string: Optional[str] = None)
Bases:
telethon.sessions.memory.MemorySessionThis 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
encodeanddecodefunctions, if present:encodedefinition must bedef encode(value: bytes) -> str:.decodedefinition must bedef decode(value: str) -> bytes:.
- __annotations__ = {}
- 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.