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:
- And we need to put the main code inside an async function:
Initialize TDesktop from tdata folder¶
- First we need to load the
tdatafolder into aTDesktopobject:
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:
- 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 opentele.td import TDesktop
from opentele.tl import TelegramClient
from opentele.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())
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.