Skip to content

TDesktop


class TDesktop

class TDesktop(BaseObject)

Telegram Desktop client.
A client can have multiple accounts, up to 3 - according to official Telegram Desktop client.

Attributes:

Name Type Description
api API The API this client is using.
accountsCount int The numbers of accounts in this client.
accounts List[Account] List of accounts in this client.
mainAccount Account The main account of this client.
basePath str The path to tdata folder.
passcode str Passcode of the client, the same as Local Passcode on Telegram Desktop.
Use to encrypt and decrypt tdata files.
AppVersion int App version of the client.
kMaxAccounts int See kMaxAccounts.
keyFile str See keyFile.
kDefaultKeyFile str Default value for keyFile.
kPerformanceMode bool Performance mode. When enabled, SavaTData() will be 200x faster.
See kPerformanceMode.

Methods:


TDesktop()

def __init__(basePath: str = None, api: Union[Type[APIData], APIData] = API.TelegramDesktop, passcode: str = None, keyFile: str = None) -> None

Initialize a TDesktop client

Arguments:

Name Type Default Description
basePath str None The path to the tdata folder.
If the path doesn't exists or its data is corrupted, a new instance will be created.
api API TelegramDesktop Which API to use. Read more here.
passcode str None The passcode for tdata, same as the Local Passcode on Telegram Desktop.
keyFile str "data" See keyFile.


isLoaded()

def isLoaded() -> bool

Return True if the client has successfully loaded accounts from tdata or TelegramClient


LoadTData()

def LoadTData(basePath: str = None, passcode: str = None, keyFile: str = None)

Loads accounts from tdata folder

Arguments:

Name Type Default Description
basePath str None The path to the folder.
passcode str None Read more here.
keyFile str None Read more here.

Raises:

TDataBadDecryptKey : The tdata folder is password-encrypted, please the set the argument passcode to decrypt it.

This function is not recommended to use

You should load tdata using TDesktop(basePath="path").
Don't manually load tdata using this function, bugs might pop up out of nowhere.

Examples:

# Using the API that we've generated before. Please refer to method API.Generate() to learn more.
oldAPI = API.TelegramDesktop.Generate(system="windows", unique_id="old.session")
oldclient = TelegramClient("old.session", api=oldAPI)
await oldClient.connect()

# We can safely use CreateNewSession with a different API.
# Be aware that you should not use UseCurrentSession with a different API than the one that first authorized it.
newAPI = API.TelegramAndroid.Generate("new_tdata")
tdesk = await TDesktop.FromTelethon(oldclient, flag=CreateNewSession, api=newAPI)

# Save the new session to a folder named "new_tdata"
tdesk.SaveTData("new_tdata")


SaveTData()

def SaveTData(basePath: str = None, passcode: str = None, keyFile: str = None) -> bool

Save the client session to a folder.

Arguments:

Name Type Default Description
basePath str None Path to the folder
If None then the data will be saved at the basePath given at creation.
passcode str None Read more here.
keyFile str None Read more here.

Examples:

Save a telethon session to tdata:

# Using the API that we've generated before. Please refer to method API.Generate() to learn more.
oldAPI = API.TelegramDesktop.Generate(system="windows", unique_id="old.session")
oldclient = TelegramClient("old.session", api=oldAPI)
await oldClient.connect()

# We can safely CreateNewSession with a different API.
# Be aware that you should not use UseCurrentSession with a different API than the one that first authorized it.
newAPI = API.TelegramAndroid.Generate("new_tdata")
tdesk = await TDesktop.FromTelethon(oldclient, flag=CreateNewSession, api=newAPI)

# Save the new session to a folder named "new_tdata"
tdesk.SaveTData("new_tdata")


ToTelethon()

@typing.overload
async def ToTelethon(session: Union[str, Session] = None, flag: Type[LoginFlag] = CreateNewSession, api: Union[Type[APIData], APIData] = API.TelegramDesktop, password: str = None) -> tl.TelegramClient

Arguments:

Name Type Default Description
session str, Session None The file name of the session file to be used, if None then the session will not be saved.
Read more here.
flag LoginFlag CreateNewSession The login flag. Read more here.
api APIData TelegramDesktop Which API to use. Read more here.
password str None Two-step verification password if needed.

Returns:

Examples:

Create a telethon session from tdata folder:

# Using the API that we've generated before. Please refer to method API.Generate() to learn more.
oldAPI = API.TelegramDesktop.Generate(system="windows", unique_id="old_tdata")
tdesk = TDesktop("old_tdata", api=oldAPI)

# We can safely authorize the new client with a different API.
newAPI = API.TelegramAndroid.Generate(unique_id="new.session")
client = await tdesk.ToTelethon(session="new.session", flag=CreateNewSession, api=newAPI)
await client.connect()
await client.PrintSessions()


FromTelethon()

@staticmethod
async def FromTelethon(telethonClient: tl.TelegramClient, flag: Type[LoginFlag] = CreateNewSession, api: Union[Type[APIData], APIData] = API.TelegramDesktop, password: str = None) -> TDesktop

Create an instance of TDesktop from TelegramClient.

Arguments:

Name Type Default Description
telethonClient TelegramClient The TelegramClient you want to convert from.
flag LoginFlag CreateNewSession The login flag. Read more here.
api APIData API.TelegramDesktop Which API to use. Read more here.
password str None Two-step verification password if needed.

Examples:

Save a telethon session to tdata:

# Using the API that we've generated before. Please refer to method API.Generate() to learn more.
oldAPI = API.TelegramDesktop.Generate(system="windows", unique_id="old.session")
oldclient = TelegramClient("old.session", api=oldAPI)
await oldClient.connect()

# We can safely CreateNewSession with a different API.
# Be aware that you should not use UseCurrentSession with a different API than the one that first authorized it.
newAPI = API.TelegramAndroid.Generate("new_tdata")
tdesk = await TDesktop.FromTelethon(oldclient, flag=CreateNewSession, api=newAPI)

# Save the new session to a folder named "new_tdata"
tdesk.SaveTData("new_tdata")


PerformanceMode()

@classmethod
def PerformanceMode(cls, enabled: bool = True)

Enable or disable performance mode. See kPerformanceMode.
It is enabled by default.

Arguments:

Name Type Default Description
enabled bool True Either enable or disable performance mode.


kMaxAccounts

The maximum amount of accounts a client can have


kDefaultKeyFile

See TDesktop.keyFile


kPerformanceMode

When enabled, SaveTData() will be 5000x faster.

  • What it does is using a constant localKey rather than generating it everytime when saving tdata.

  • The average time for generating localKey is about 250 to 350 ms, depend on your CPU.

  • When in performance mode, the average time to generate localKey is 0.0628 ms. Which is 5000x faster

  • Of course this comes with a catch, your tdata files will always use a same constant localKey. Basicly no protection at all, but who cares?

Note

Performance mode will be disabled if passcode is set.


api

@property
def api() -> APIData

The API this client is using.


basePath

@property
def basePath() -> Optional[str]

Base folder of TDesktop, this is where data stored
Same as tdata folder of Telegram Desktop


passcode

@property
def passcode() -> str

Passcode used to encrypt and decrypt data
Same as the Local Passcode of Telegram Desktop


keyFile

@property
def keyFile() -> str

The default value is "data", this argument is rarely ever used.
It is used by Telegram Desktop by running it with the "-key" argument. I don't know what's the use cases of it, maybe this was a legacy feature of Telegram Desktop.


localKey

@property
def localKey() -> Optional[td.AuthKey]

The key used to encrypt/decrypt data


AppVersion

@property
def AppVersion() -> Optional[int]

App version of TDesktop client


accountsCount

@property
def accountsCount() -> int

The number of accounts this client has


accounts

@property
def accounts() -> List[td.Account]

List of accounts this client hasn
If you want to get the main account, please use .mainAccount instead


mainAccount

@property
def mainAccount() -> Optional[td.Account]

The main account of the client