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:.
  • decode definition must be def 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.