Skip to Content
ExamplesConvert Telethon to tdata

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 opentele2.td import TDesktop from opentele2.tl import TelegramClient from opentele2.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 opentele2.td import TDesktop from opentele2.tl import TelegramClient from opentele2.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())
Last updated on