Skip to Content
Guidestdata to Telethon

tdata to Telethon

Take the account out of a Telegram Desktop tdata folder and hand it to Telethon: as a string session, as a .session file with its .json companion, or as a ready opentele2 TelegramClient. Telethon is a Python library, so Python connects; Node and Rust write the same strings and files for a Python process to use.

Warning:

Work on a copy of the folder. The string, the .session file and the folder are the same logged-in account: never share them, never commit them and keep them out of logs.

Install

Install
pip install opentele2

Python 3.8+ · abi3 wheels · works over Telethon

Load the folder

Copy the tdata folder of a logged-in Telegram Desktop next to your script. A folder with a local passcode needs it to load; a wrong or missing passcode fails the load. The main account is the one the conversions use.

load.py
from opentele2.td import TDesktop tdesk = TDesktop("tdata") # TDesktop("tdata", passcode="...") for a locked folder print(tdesk.accountsCount, tdesk.mainAccount.UserId, tdesk.mainAccount.MainDcId)

tdesk.accounts lists every account; each Account has the same ToTelethon as the folder.

Get a string session

A Telethon string session (version 1) holds the DC, its address and port and the auth key. The address is the first TCP endpoint of the main DC from the account’s stored config, or the built-in production table when it has none.

string.py
import asyncio from telethon.sessions import StringSession from opentele2.api import UseCurrentSession from opentele2.td import TDesktop async def main(): client = await TDesktop("tdata").ToTelethon(StringSession(), UseCurrentSession) print(client.session.save()) # the folder's own auth key; nothing is sent yet asyncio.run(main())

Without a client, telethon_string_session(TData.load("tdata").main_account) from opentele2._native gives the same string; pass True as the second argument for the IPv6 endpoint.

Connect with the Telegram Desktop profile

A session from tdata belongs to Telegram Desktop, so connect with the Telegram Desktop API profile. The opentele2 TelegramClient is a telethon.TelegramClient subclass that sends that profile in initConnection, lang_pack and tz_offset included.

connect.py
import asyncio from telethon.sessions import StringSession from opentele2.api import API from opentele2.tl import TelegramClient async def main(string: str): client = TelegramClient(StringSession(string), api=API.TelegramDesktop) await client.connect() print(await client.get_me()) await client.disconnect()

TDesktop("tdata").ToTelethon("telethon.session", UseCurrentSession) does both steps at once: it writes the folder’s key into telethon.session and returns the client, ready for connect().

Write a .session + .json pair

The pair is a Telethon SQLiteSession file (version 8) and a .json companion holding the API profile, so whoever loads it later connects with the same profile. The pair guide covers its keys and the way back.

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

SaveSessionJson(path, api=None, fetch_user_info=False) writes the folder’s own session with the folder’s profile; pass api= to store another one.

Or log a new session in

UseCurrentSession reuses the folder’s auth key, so Telethon and Telegram Desktop share one session. CreateNewSession logs a new one in by QR code through the folder’s session, on the device you choose; it needs the network and, with 2FA on, the password.

new_session.py
import asyncio from opentele2.api import API, CreateNewSession from opentele2.td import TDesktop async def main(): tdesk = TDesktop("tdata") client = await tdesk.ToTelethon( "new.session", CreateNewSession, api=API.TelegramAndroid.Generate("my-id"), password=None ) await client.connect() print(await client.get_me()) asyncio.run(main())

password= is the 2FA password; without it a 2FA account raises NoPasswordProvided, and a wrong one PasswordIncorrect (both in opentele2.exception).

Note:

Keep each session on the profile it was created with. A session that suddenly announces another device or app looks like a different client to the server; see initConnection fingerprints.

Next

Last updated on