Files
tg_python/tg_bridge/login_cli.py
2026-04-23 22:06:19 +08:00

50 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""首次登录 Telegram生成 session 文件,供 tg_bridge 服务使用。
用法(在 wx_python 目录下):
python -m tg_bridge.login_cli
需已配置 TELEGRAM_API_ID、TELEGRAM_API_HASH、TELEGRAM_SESSION_PATH可选见 tg_bridge/.env.example
"""
from __future__ import annotations
import asyncio
from tg_bridge.client_factory import create_telegram_client
from tg_bridge.config import Settings
from tg_bridge.proxy import telethon_proxy_from_settings
from tg_bridge.winloop import apply_windows_selector_policy
async def main() -> None:
s = Settings.load()
try:
proxy = telethon_proxy_from_settings(s)
except ValueError as e:
print(e)
return
if proxy:
safe = {**proxy}
if safe.get("password"):
safe["password"] = "***"
print(
f"使用代理: {safe!r} connection={s.connection_mode} "
f"connect_timeout={s.connect_timeout}s"
)
else:
print(f"未配置代理(直连) connection={s.connection_mode}")
client = create_telegram_client(s)
await client.start()
if not await client.is_user_authorized():
print("登录未完成")
return
me = await client.get_me()
print(f"已保存会话: {s.session_path}")
print(f"当前账号: id={me.id} username={me.username!r}")
await client.disconnect()
if __name__ == "__main__":
apply_windows_selector_policy()
asyncio.run(main())