Convert tdata to Telethon
This example will show you how to initialize a TelegramClient from tdata.
Preparing
- You need to have a
tdatafolder which is already authorized (have at least one logged-in account). - The default path to
tdatafolder on Windows is located at%appdata%\Telegram Desktop\tdata. - 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 TDesktop from tdata folder
- First we need to load the
tdatafolder into aTDesktopobject:# Load TDesktop client from tdata folder tdataFolder = r"C:\Users\<username>\AppData\Roaming\Telegram Desktop\tdata" tdesk = TDesktop(tdataFolder) # Check if we have loaded any accounts assert tdesk.isLoaded()
Converting TDesktop to TelegramClient
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 TDesktop to Telethon using the current session. client = await tdesk.ToTelethon(session="telethon.session", flag=UseCurrentSession) - Create a new session:
# flag=CreateNewSession # # Convert TDesktop to Telethon 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. client = await tdesk.ToTelethon(session="telethon.session", flag=CreateNewSession)
Connect and print all logged-in sessions.
# Connect and print all logged-in sessions of this client.
# Telethon will save the session to telethon.session on creation.
await client.connect()
await client.PrintSessions()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 TDesktop client from tdata folder
tdataFolder = r"C:\Users\<username>\AppData\Roaming\Telegram Desktop\tdata"
tdesk = TDesktop(tdataFolder)
# Check if we have loaded any accounts
assert tdesk.isLoaded()
# flag=UseCurrentSession
#
# Convert TDesktop to Telethon using the current session.
client = await tdesk.ToTelethon(session="telethon.session", flag=UseCurrentSession)
# Connect and print all logged-in sessions of this client.
# Telethon will save the session to telethon.session on creation.
await client.connect()
await client.PrintSessions()
asyncio.run(main())Last updated on