简体   繁体   中英

How to convert tdata to telethon session?

I need to convert telegram data to telethon session. How can i do this? I tried to find a solution, spent three days, but I didn't find anything

It's been nearly two years, but there's now a Python library called opentele created to do this.

Assuming you already have Python knowledge, please follow the steps below.

First you need to install opentele from PyPi:

pip install opentele

Preparing:

  • You need to have a tdata folder that is already authorized (have at least one logged-in account).
  • The default path to tdata folder on Windows is located at %appdata%\Telegram Desktop\tdata .
  • Create a python (.py) file and import these things:
     from opentele.td import TDesktop from opentele.tl import TelegramClient from opentele.api import API, UseCurrentSession, CreateNewSession import asyncio
  • And you also 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

  • Load the tdata folder into a TDesktop object:
     # 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 you can do this, either by using the current session or creating (login 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 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 result

  • The session will be saved to telethon.session file.
  • You can use this telethon.session with telethon, or use it directly with opentele - which is recommended because you don't need your own API_ID and API_HASH , the library by default will use Official Telegram API.

Remark

  • opentele is a very well documented library, you can find its documentation here .
  • It can also convert telethon sessions back to tdata and it's usable with Telegram Desktop.

Warning

  • According to its docs, you must continue using the associated API which you've had used to create that session whenever connecting to the server, or else you're at risk of losing that account.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM