Skip to Content
ReferenceNodeopentele2/telethon

opentele2/telethon

Telethon SQLiteSession files (version 8) and the .session + .json pairs the Python package writes, over the bundled SQLite of the native module: no Python and no Telethon needed.

pair.mjs
import { loadTdata, profile } from 'opentele2' import { loadSessionPair, saveSessionPair } from 'opentele2/telethon' const { mainAccount } = await loadTdata('tdata') const paths = await saveSessionPair('telethon', mainAccount, profile('TelegramDesktop')) const { rows, profile: saved, json } = await loadSessionPair('telethon') console.log(paths.session, paths.json, rows.dcId, saved?.deviceModel, json.id)

Functions

telethon.d.ts
function writeSessionFile(path: string, rows: WriteSessionRows): Promise<string> function readSessionFile(path: string): Promise<ReadSessionRows> function saveSessionPair( base: string, account: AccountInput, profile: ApiProfile, options?: SaveSessionPairOptions, ): Promise<SessionPairPaths> function loadSessionPair(base: string): Promise<LoadedSessionPair>
FunctionDescription
writeSessionFile(path, rows)Writes rows and resolves to sessionPath(path): a schema that is not the version 8 layout is rebuilt, then the version and sessions rows are replaced in one transaction. Rejects with OT_BAD_LENGTH for a key that is not 256 bytes, ERR_INVALID_ARG_VALUE for other bad rows and OT_IO when SQLite fails.
readSessionFile(path)Reads the sessions row of the version 8 file at sessionPath(path), written by Python, writeSessionFile or Telethon. 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.
saveSessionPair(base, account, profile, options)Writes the pair SaveSessionJson writes and resolves to its paths: the account’s telethonSessionRows into the session file and sessionCompanionText(profile, extra) into the companion.
loadSessionPair(base)Reads the pair at sessionPairPaths(base) back. Rejects with OT_TFILE_NOT_FOUND when either file is missing, OT_IO when the companion is not a valid session JSON, and the readSessionFile errors.

Types

telethon.d.ts
interface ReadSessionRows extends Omit<TelethonSessionRows, 'takeoutId'> { takeoutId?: number | bigint } interface WriteSessionRows extends Omit<TelethonSessionRows, 'authKey' | 'takeoutId'> { authKey: Uint8Array takeoutId?: number | bigint | null } interface SaveSessionPairOptions { ipv6?: boolean extra?: Record<string, unknown> } interface LoadedSessionPair { paths: SessionPairPaths rows: ReadSessionRows profile: ApiProfile | null json: Record<string, unknown> }
FieldDescription
SaveSessionPairOptions.ipv6Write the main DC’s IPv6 endpoint instead of its IPv4 one.
SaveSessionPairOptions.extraCompanion keys after session_file, which they may override; id defaults to the account’s user id when it is not zero.
LoadedSessionPair.profileThe companion’s profile, or null when its profile keys are not in strict form.
LoadedSessionPair.jsonThe companion as parsed; an integer outside the safe range is a bigint.
ReadSessionRows.takeoutIdA takeout id outside the safe integer range is a bigint; absent is NULL.

The lower-level writeTelethonSessionFile, readTelethonSessionFile and the companion functions are on the Sessions page.

Last updated on