是否可以仅从auth_key创建telethon客户端? [英] Is it possible to create a telethon client starting from auth_key only?
问题描述
telethon的问候世界看起来像:
The hello world of telethon looks like:
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())
像这样,它将要求我通过提供电话和确认码进行首次登录. 下次它将重用本地存储的信息.
Like this it will ask me to sign in the first time, by providing phone and confirmation code. Next time it will reuse information stored locally.
我想要给它一个auth_key并使用它. 所以基本上我希望它看起来像这样: 从telethon导入TelegramClient
What i want is to give it a auth_key and use that. So basically i want it to look like this: 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())
推荐答案
While it is possible to use the auth_key
directly, there are better options available, such as 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
(根据需要)以及执行成功连接所需的其他必要信息.
Be careful not to share this string, as anyone would gain access to the account. This string contains the auth_key
(as you wanted) along with other required information to perform a successful connection.
这篇关于是否可以仅从auth_key创建telethon客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!