Sessions

Classes related to session data and session storages.

Note

This module is mostly of interest if you plan to write a custom session storage. You should not need to interact with these types directly under normal use.

See also

The Sessions concept for more details.

Storages

class telethon.session.Storage

Bases: ABC

Interface declaring the required methods of a session storage.

abstract async close()

Close the Session instance, if it was still open.

This method is called by the library post disconnect, even if the call to save() failed.

Note that load() may still be called after, in which case the session should be reopened.

Return type:

None

abstract async load()

Load the Session instance, if any.

This method is called by the library prior to connect.

Returns:

The previously-saved session.

Return type:

Session | None

abstract async save(session)

Save the Session instance to persistent storage.

This method is called by the library after significant changes to the session, such as login, logout, or to persist the update state prior to disconnection.

Parameters:

session (Session) – The session information that should be persisted.

Return type:

None

class telethon.session.SqliteSession(file)

Bases: Storage

Session storage backed by SQLite.

SQLite is a reliable way to persist data to disk and offers file locking.

Paths without extension will have '.session' appended to them. This is by convention, and to make it harder to commit session files to an VCS by accident (adding *.session to .gitignore will catch them).

Parameters:

file (str | Path) –

async close()

Close the Session instance, if it was still open.

This method is called by the library post disconnect, even if the call to save() failed.

Note that load() may still be called after, in which case the session should be reopened.

Return type:

None

async load()

Load the Session instance, if any.

This method is called by the library prior to connect.

Returns:

The previously-saved session.

Return type:

Session | None

async save(session)

Save the Session instance to persistent storage.

This method is called by the library after significant changes to the session, such as login, logout, or to persist the update state prior to disconnection.

Parameters:

session (Session) – The session information that should be persisted.

Return type:

None

class telethon.session.MemorySession(session=None)

Bases: Storage

Session storage without persistence.

Session data is only kept in memory and is not saved to disk.

Parameters:

session (Session | None) –

async close()

Close the Session instance, if it was still open.

This method is called by the library post disconnect, even if the call to save() failed.

Note that load() may still be called after, in which case the session should be reopened.

Return type:

None

async load()

Load the Session instance, if any.

This method is called by the library prior to connect.

Returns:

The previously-saved session.

Return type:

Session | None

async save(session)

Save the Session instance to persistent storage.

This method is called by the library after significant changes to the session, such as login, logout, or to persist the update state prior to disconnection.

Parameters:

session (Session) – The session information that should be persisted.

Return type:

None

session

Types

class telethon.session.Session(*, dcs=None, user=None, state=None)

Bases: object

A Telethon session.

A session instance contains the required information to login into your Telegram account. Never give the saved session file to anyone else or make it public.

Leaking the session file will grant a bad actor complete access to your account, including private conversations, groups you’re part of and list of contacts (though not secret chats).

If you think the session has been compromised, immediately terminate all sessions through an official Telegram client to revoke the authorization.

Parameters:
VERSION = 1

Current version.

Will be incremented if new fields are added.

dcs

List of known data-centers.

state

Update state.

user

Information about the logged-in user.

class telethon.session.DataCenter(*, id, ipv4_addr=None, ipv6_addr=None, auth=None)

Bases: object

Data-center information.

Parameters:
  • id (int) – See below.

  • ipv4_addr (str | None) – See below.

  • ipv6_addr (str | None) – See below.

  • auth (bytes | None) – See below.

auth

Authentication key to encrypt communication with.

id

The DC identifier.

ipv4_addr

The IPv4 socket server address of the DC, in 'ip:port' format.

ipv6_addr

The IPv6 socket server address of the DC, in 'ip:port' format.

class telethon.session.User(*, id, dc, bot, username)

Bases: object

Information about the logged-in user.

Parameters:
  • id (int) – See below.

  • dc (int) – See below.

  • bot (bool) – See below.

  • username (str | None) – See below.

bot

True if the user is from a bot account.

dc

Data-center identifier of the user’s “home” DC.

id

User identifier.

username

User’s primary username.

class telethon.session.UpdateState(*, pts, qts, date, seq, channels)

Bases: object

Update state for an account.

Parameters:
  • pts (int) – See below.

  • qts (int) – See below.

  • date (int) – See below.

  • seq (int) – See below.

  • channels (list[ChannelState]) – See below.

channels

Update state for channels.

date

Date of the latest update sequence.

pts

The primary partial sequence number.

qts

The secondary partial sequence number.

seq

The sequence number.

class telethon.session.ChannelState(*, id, pts)

Bases: object

Update state for a channel.

Parameters:
  • id (int) – See below.

  • pts (int) – See below.

id

The channel identifier.

pts

The channel’s partial sequence number.