是否可以仅从 auth_key 开始创建电视节目客户端?

Is it possible to create a telethon client starting from auth_key only?

telethon 的 hello world 如下所示:

from telethon import TelegramClient

client = TelegramClient(name, api_id, api_hash)

async def main():
    # Now you can use all client methods listed below, like for example...
    await client.send_message('me', 'Hello to myself!')

with client:
    client.loop.run_until_complete(main())

像这样它会要求我在第一次登录时提供 phone 和确认码。 下次它将重用本地存储的信息。

我想要的是给它一个 auth_key 并使用它。 所以基本上我希望它看起来像这样: 从 telethon 导入 TelegramClient

auth_key = "ca03d.....f8ed" # a long hex string

client = TelegramClient(name, api_id, api_hash, auth_key=auth_key)

async def main():
    # Now you can use all client methods listed below, like for example...
    await client.send_message('me', 'Hello to myself!')

with client:
    client.loop.run_until_complete(main())

虽然可以直接使用 auth_key,但还有更好的选择,例如 using StringSession as documented:

from telethon.sync import TelegramClient
from telethon.sessions import StringSession

# Generating a new one
with TelegramClient(StringSession(), api_id, api_hash) as client:
    print(client.session.save())

# Converting SQLite (or any) to StringSession
with TelegramClient(name, api_id, api_hash) as client:
    print(StringSession.save(client.session))

# Usage
string = '1aaNk8EX-YRfwoRsebUkugFvht6DUPi_Q25UOCzOAqzc...'
with TelegramClient(StringSession(string), api_id, api_hash) as client:
    client.loop.run_until_complete(client.send_message('me', 'Hi'))

请注意不要共享此字符串,因为任何人都可以访问该帐户。此字符串包含 auth_key(如您所愿)以及执行成功连接所需的其他信息。