Skip to Content

Sessions

Session strings and Telethon session files built from tdata accounts. Every function takes an AccountInput and the first TCP endpoint of its main DC: from the account’s stored MTP config when it has one, else from the built-in production table.

sessions.mjs
import { loadTdata, mtcuteStringSession, telethonStringSession } from 'opentele2' const { mainAccount } = await loadTdata('tdata') console.log(telethonStringSession(mainAccount)) // telethonStringSession(mainAccount, true) for IPv6 console.log(mtcuteStringSession(mainAccount))

String sessions

index.d.ts
function telethonStringSession(account: AccountInput, ipv6?: boolean | null): string function mtcuteStringSession(account: AccountInput): string
FunctionDescription
telethonStringSession(account, ipv6)The Telethon string session: the first endpoint of the main DC, IPv6 when ipv6 is set, and the main DC key.
mtcuteStringSession(account)The mtcute v3 string session: the first production IPv4 endpoint of the main DC, the user id and the main DC key. toStringSession in opentele2/mtcute is the same string.

Telethon .session files

index.d.ts
function telethonSessionRows(account: AccountInput, ipv6?: boolean | null): TelethonSessionRows function writeTelethonSessionFile(path: string, rows: TelethonSessionRowsInput): Promise<string> function readTelethonSessionFile(path: string): Promise<TelethonFileRows> function telethonSchema(): Array<TelethonTable> function sessionPath(path: string): string function sessionPairPaths(base: string): SessionPairPaths function sessionFileName(base: string): string interface TelethonSessionRows { dcId: number serverAddress: string port: number authKey: Buffer takeoutId?: number } interface TelethonSessionRowsInput { dcId: number serverAddress: string port: number authKey: Uint8Array takeoutId?: bigint } interface TelethonFileRows { dcId: number serverAddress: string port: number authKey: Buffer takeoutId?: bigint } interface TelethonTable { name: string; ddl: string; columns: Array<string> } interface SessionPairPaths { session: string; json: string }
FunctionDescription
telethonSessionRows(account, ipv6)The sessions row saved for the account, with the address text the row stores; throws OT_TDATA_BAD_CONFIG_DATA when the main DC has no endpoint.
writeTelethonSessionFile(path, rows)Writes a version 8 session file over the bundled SQLite and resolves to its path. A schema that is not the version 8 layout is dropped and rebuilt; a current one keeps its other rows.
readTelethonSessionFile(path)Reads the sessions row of a version 8 file, opened read-only; rejects with OT_TFILE_NOT_FOUND, OT_IO, OT_QDATASTREAM_FAILED, OT_TDATA_AUTH_KEY_NOT_FOUND, OT_BAD_LENGTH or OT_TDATA_BAD_CONFIG_DATA.
telethonSchema()The version 8 tables in creation order.
sessionPath(path)path itself when it ends with .session, else path with it appended.
sessionPairPaths(base)The pair paths: one trailing .session, then one .json, is removed and each suffix appended.
sessionFileName(base)The session_file value of the companion: the base name of the pair, for the platform Node runs on.

The SQL the writer runs and the layout constants are exported too: TELETHON_SESSION_VERSION (8), TELETHON_SESSION_SUFFIX, TELETHON_JSON_SUFFIX, TELETHON_DELETE_VERSION, TELETHON_INSERT_VERSION, TELETHON_DELETE_SESSIONS and TELETHON_INSERT_SESSION.

The .json companion

index.d.ts
function sessionCompanionJson(profile: ApiProfile, extra?: Record<string, unknown> | null): Record<string, unknown> function sessionCompanionText(profile: ApiProfile, extra?: Record<string, unknown> | null): string function readSessionCompanion(text: string): SessionCompanion interface SessionCompanion { profile?: ApiProfile json: Record<string, unknown> }
FunctionDescription
sessionCompanionJson(profile, extra)The companion object the Python package writes, keys in its order; extra holds session_file and, when known, id.
sessionCompanionText(profile, extra)The companion text byte for byte as Python’s json.dump(..., indent=2, ensure_ascii=False) prints it, without a trailing newline.
readSessionCompanion(text)Parses companion text: it must be a JSON object holding app_id and app_hash, else OT_INVALID_INPUT. profile is absent when a profile key is missing or has the wrong type; json is the whole object, with integers outside the safe range as bigint.

The opentele2/telethon subpath combines these into saveSessionPair and loadSessionPair. The keys of the companion are listed in the .session + .json guide.

Last updated on