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

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

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

我想要的是给它一个 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/

相关文章:

python-3.x - 如何更改 Telegram channel 名称?

python - 如何在 python 中抑制 PDFMiner 的布局分析

python - 为什么此代码不检查文本文件中单词的有效性?

python - pyTelegramBotAPI 中带参数的命令

php - 如何使用 Telegram 机器人聊天 API 获取某人的用户个人资料图片?

python - 如何使用马拉松比赛运行多个 session ?

python-3.x - Python 3-TypeError : a bytes-like object is required,不是 'str'

linux - cv2 imdecode不返回任何base64字符串

node.js - 如何在 Telegram 上获取联系人

python - 使用 telethon 库从电报组中获取所有预定消息