Skip to Content
Getting startedQuick start

Quick start

Point opentele2 at a Telegram Desktop tdata folder and use its account from your client library: Telethon in Python, mtcute in Node, grammers in Rust.

Warning:

Work on a copy of the folder, and never share the folder or the sessions made from it: each one is a logged-in account.

Install

Install
pip install opentele2

Python 3.8+ · abi3 wheels · works over Telethon

Find the folder

Copy the tdata folder of a logged-in Telegram Desktop. The files opentele2 reads are the key file and, per account, its MTP data file and map:

    • key_datas
    • D877F783D5D3EF8Cs

If Telegram Desktop has a local passcode, pass it when loading. See Telegram Desktop tdata for the layout.

Load it and get a session

convert.py
import asyncio from opentele2.api import UseCurrentSession from opentele2.td import TDesktop async def main(): tdesk = TDesktop("tdata") # TDesktop("tdata", passcode="...") for a locked folder client = await tdesk.ToTelethon("telethon.session", UseCurrentSession) await client.connect() print(await client.get_me()) asyncio.run(main())

ToTelethon returns an opentele2 TelegramClient, a telethon.TelegramClient subclass that sends the official Telegram Desktop profile in initConnection. UseCurrentSession reuses the folder’s own auth key and writes it into telethon.session; nothing is sent until connect().

Save a session to disk

save.py
import asyncio from opentele2.td import TDesktop async def main(): tdesk = TDesktop("tdata") session_file, json_file = await tdesk.SaveSessionJson("telethon.session") print(session_file, json_file) asyncio.run(main())

SaveSessionJson writes a Telethon .session file and its .json companion holding the API profile, so the pair can be loaded later with TelegramClient.FromSessionJson.

Go back to tdata

back.py
import asyncio from opentele2.api import API, UseCurrentSession from opentele2.td import TDesktop async def main(): tdesk = TDesktop("tdata") client = await tdesk.ToTelethon("telethon.session", UseCurrentSession) # A new session, logged in by QR code from the folder's session, on a generated device. tdesk2 = await client.ToTDesktop(api=API.TelegramDesktop.Generate("windows", "my-id")) tdesk2.SaveTData("tdata_new") asyncio.run(main())

Without a flag, ToTDesktop (like ToTelethon) uses CreateNewSession: it logs a new session in by QR code through the existing one, which needs the network and, with 2FA on, password=. ToTDesktop(flag=UseCurrentSession) stores the client’s own session instead, offline.

Next

Last updated on