Created
January 30, 2024 11:41
-
-
Save Feiyuyu0503/d0c74f56df0de925a021d1b6004c4e88 to your computer and use it in GitHub Desktop.
telegram登录脚本
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from telethon import TelegramClient, events | |
from telethon.sessions import StringSession | |
# 可选 | |
saveToFile = True # 是否把登录成功后的session文件保存到本地,给其他环境复用 | |
#phone = '' # telegram的登录手机号. 在这预先填写好 | |
#password = '' # 两步验证密码,没设置过就不填 | |
SESSION_NAME = 'session_name' # 保存session文件的名字,给其他环境复用。其含义同 https://github.com/Feiyuyu0503/free-dall-e-proxy/blob/95ecee4aef77a2b468bf16e9c38c3b117c5e0bae/data/.env.example#L10 | |
# 必填 | |
api_id = 123456 # https://my.telegram.org/apps | |
api_hash = '' | |
client = TelegramClient(StringSession(), int(api_id), str(api_hash)) | |
#client.start(phone=phone, password=password) | |
client.start() | |
if saveToFile: | |
with open(SESSION_NAME+'.session', 'w') as f: | |
f.write(client.session.save()) | |
print("保留好这个session字符串,下次登录可以直接用这个字符串登录,不用再输入手机号和密码了:\n", client.session.save()) | |
# 程序输出示例如下 | |
""" | |
Please enter your phone (or bot token): +86189... | |
Please enter the code you received: 22616 | |
Please enter your password: | |
Signed in successfully as user; remember to not break the ToS or you will risk an account ban! | |
保留好这个session字符串,下次登录可以直接用这个字符串登录,不用再输入手机号和密码了: | |
2BctsOKw8u7koN...NqHR5ehWjJ8N60z... | |
""" | |
# 登录成功后 | |
""" | |
1. 你可以用上面输出的session字符串,在 data/.env中新增一个环境变量 SESSION_STRING=2BctsOKw8u7koN...NqHR5ehWjJ8N60z... | |
或者docker部署时,可以传入这个环境变量,例如 docker run -e SESSION_STRING=2BctsOKw8u7koN...NqHR5ehWjJ8N60z... -d --name free-dall-e-proxy -p 8080:8080 feiyuyu/free-dall-e-proxy | |
2. 你也可以直接把刚刚的session字符串保存成文件,以SESSION_NAME.session命名,上传到项目data目录下 | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment