【问题标题】:How to convert tdata to telethon session?如何将 tdata 转换为 Telethon 会话?
【发布时间】:2020-08-10 22:41:25
【问题描述】:

我需要将电报数据转换为 Telethon 会话。 我怎样才能做到这一点? 我试图找到解决方案,花了三天时间,但我没有找到任何东西

【问题讨论】:

  • 你必须看看这两种格式是如何工作的,然后编写代码来翻译它们
  • 我明白,但我没有足够的经验来做这件事
  • 寻找相同的东西(准确地说是会话、api id 和 api 哈希),似乎唯一的方法是检查电报源代码并像电报一样解析文件。

标签: python api telegram telethon


【解决方案1】:

已经快两年了,但现在创建了一个名为 opentele 的 Python 库来执行此操作。

假设您已经具备 Python 知识,请按照以下步骤操作。

首先你需要从 PyPi 安装 opentele:

pip install opentele

准备:

  • 您需要拥有一个已获得授权的tdata 文件夹(至少有一个登录帐户)。
  • Windows 上tdata 文件夹的默认路径位于%appdata%\Telegram Desktop\tdata。
  • 创建一个 python (.py) 文件并导入以下内容:
    from opentele.td import TDesktop
    from opentele.tl import TelegramClient
    from opentele.api import API, UseCurrentSession, CreateNewSession
    import asyncio
    
  • 您还需要将主代码放在异步函数中:
    async def main():
        # PUT EXAMPLE CODE HERE
    
    asyncio.run(main())
    

从 tdata 文件夹初始化 TDesktop

  • 将tdata 文件夹加载到TDesktop 对象中:
    # 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()
    

将TDesktop 转换为TelegramClient

有两种方法可以做到这一点,使用当前会话或创建(登录)新会话。

  • 使用当前会话:
    # flag=UseCurrentSession
    #
    # Convert TDesktop to Telethon using the current session.
    client = await tdesk.ToTelethon(session="telethon.session", flag=UseCurrentSession)
    
  • 创建一个新会话:
    # 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 of this client.
# Telethon will save the session to telethon.session on creation.
await client.connect()
await client.PrintSessions()

最终结果示例

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())

结果

  • 会话将保存到telethon.session 文件。
  • 你可以将这个telethon.session与telethon一起使用,或者直接与opentele一起使用——推荐因为你不需要自己的API_ID和API_HASH,这个库默认使用官方Telegram API .

备注

  • opentele 是一个文档非常完善的库,您可以找到它的文档 here。
  • 它还可以将 Telethon 会话转换回 tdata,并且可以与 Telegram Desktop 一起使用。

警告

  • 根据其文档,您必须继续使用关联的 API,您在连接到服务器时曾使用它来创建该会话,否则您将面临丢失该帐户的风险。

【讨论】:

    猜你喜欢
    • 2020-12-02
    • 2013-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    • 2012-01-16
    相关资源
    最近更新 更多