gpt4 book ai didi

python-3.x - 是否可以仅从 auth_key 开始创建电视节目客户端?

转载 作者:行者123 更新时间:2023-12-02 03:06:36 25 4
gpt4 key购买 nike

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

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

我想要的是给它一个 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(如您所愿)以及执行成功连接所需的其他信息。

关于python-3.x - 是否可以仅从 auth_key 开始创建电视节目客户端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58890931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com