Skip to content

Convert Telethon to tdata

This example will show you how to create tdata folder from a TelegramClient session. This tdata folder can later be used by Telegram Desktop app.

Preparing

  • We need to import these things:
    from opentele.td import TDesktop
    from opentele.tl import TelegramClient
    from opentele.api import API, UseCurrentSession, CreateNewSession
    import asyncio
    
  • And we need to put the main code inside an async function:
    async def main():
        # PUT EXAMPLE CODE HERE
    
    asyncio.run(main())
    

Initialize TelegramClient

  • First we need to load a TelegramClient, either by loading an existing session or log in to a new session:
    # Load the client from telethon.session file
    # We don't need to specify api, api_id or api_hash, it will use TelegramDesktop API by default.
    client = TelegramClient("telethon.session")
    

Converting TelegramClient to TDesktop

There are two ways we can do this, either by using the current session, or create (log in to) a new session.

  • Use the current session:
    # flag=UseCurrentSession
    #
    # Convert Telethon to TDesktop using the current session.
    tdesk = await client.ToTDesktop(flag=UseCurrentSession)
    
  • Create a new session:
    # flag=CreateNewSession
    #
    # Convert Telethon to TDesktop by creating a new session.
    # CreateNewSession will use the current session (in tdata folder) to authorize a new session using QR Login.
    # If 2FA is enabled for this account, you must specify the password via the password argument.
    # This is of course slower than UseCurrentSession.
    tdesk = await client.ToTDesktop(flag=CreateNewSession)
    

Saving the TDesktop session to tdata folder

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

Final result example

from opentele.td import TDesktop
from opentele.tl import TelegramClient
from opentele.api import API, UseCurrentSession
import asyncio

async def main():

    # Load the client from telethon.session file
    # We don't need to specify api, api_id or api_hash, it will use TelegramDesktop API by default.
    client = TelegramClient("telethon.session")

    # flag=UseCurrentSession
    #
    # Convert Telethon to TDesktop using the current session.
    tdesk = await client.ToTDesktop(flag=UseCurrentSession)

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

asyncio.run(main())

The class links on this page point at upstream's documentation

opentele-ng has no API reference of its own yet, so TelegramClient and TDesktop above link to opentele.readthedocs.io — the generated reference for the original project, thedemons/opentele, last built in 2022. Class and method names are unchanged in this fork, so the signatures still apply, but nothing added or changed since is described there. For that, see the CHANGELOG.