是否可以仅从 auth_key 开始创建电视马拉松客户端?

tza*_*dor 3 python-3.x telegram telethon

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())
Run Code Online (Sandbox Code Playgroud)

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

我想要的是给它一个 auth_key 并使用它。所以基本上我希望它看起来像这样: from telethon import 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())
Run Code Online (Sandbox Code Playgroud)

Lon*_*ami 5

虽然可以auth_key直接使用,但还有更好的选项可用,例如按照文档使用StringSession

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'))
Run Code Online (Sandbox Code Playgroud)

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