Repository navigation
Expand file tree
/
Copy pathmigrate_session.py
More file actions
182 lines (154 loc) · 5.63 KB
/
Copy pathmigrate_session.py
File metadata and controls
182 lines (154 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env python3
"""Migrate a Telegram StringSession to a persistent SQLite session."""
import argparse
import asyncio
import os
import sys
from pathlib import Path
from typing import Optional
from dotenv import load_dotenv
from telethon import TelegramClient
from telethon.sessions import SQLiteSession, StringSession
from telegram_mcp.client_identity import client_identity_kwargs
from telegram_mcp.install_guard import UnsafeInstallationError, assert_safe_distribution
def _parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description=(
"Migrate TELEGRAM_SESSION_STRING to a persistent SQLite session. "
"Stop any running telegram-mcp process before migrating."
)
)
parser.add_argument(
"--account",
metavar="LABEL",
help=(
"Migrate TELEGRAM_SESSION_STRING_<LABEL> instead of the default account "
"(for example: --account work)."
),
)
parser.add_argument(
"--target",
metavar="PATH",
help=(
"Destination session name or path, without the .session suffix. "
"Defaults to TELEGRAM_SESSION_NAME[_<LABEL>] or telegram_mcp_session[_<label>]."
),
)
return parser.parse_args()
def _check_installation() -> None:
try:
assert_safe_distribution()
except UnsafeInstallationError as exc:
print(str(exc), file=sys.stderr)
raise SystemExit(1) from exc
def _env_names(account: Optional[str]) -> tuple[str, str, str]:
if not account:
return (
"TELEGRAM_SESSION_STRING",
"TELEGRAM_SESSION_NAME",
"telegram_mcp_session",
)
label = account.strip()
if not label or not label.replace("-", "").replace("_", "").isalnum():
raise ValueError("account label may contain only letters, numbers, '-' and '_'")
suffix = label.upper().replace("-", "_")
filename_label = label.lower().replace("-", "_")
return (
f"TELEGRAM_SESSION_STRING_{suffix}",
f"TELEGRAM_SESSION_NAME_{suffix}",
f"telegram_mcp_session_{filename_label}",
)
def _target_paths(target: str) -> tuple[str, Path]:
"""Return the Telethon session name and its resulting SQLite filename."""
expanded = Path(target).expanduser()
if expanded.name.endswith(".session"):
expanded = expanded.with_name(expanded.name[: -len(".session")])
session_name = str(expanded)
return session_name, Path(f"{session_name}.session").resolve()
async def migrate(
session_string: str,
api_id: int,
api_hash: str,
target: str,
) -> tuple[Path, object]:
"""Copy auth material into a new SQLite session and verify it with Telegram."""
session_name, session_file = _target_paths(target)
if session_file.exists():
raise FileExistsError(
f"destination already exists: {session_file}. Choose another --target."
)
sqlite_session = None
client = None
try:
source = StringSession(session_string)
sqlite_session = SQLiteSession(session_name)
sqlite_session.set_dc(source.dc_id, source.server_address, source.port)
sqlite_session.auth_key = source.auth_key
sqlite_session.save()
client = TelegramClient(
sqlite_session,
api_id,
api_hash,
**client_identity_kwargs(),
)
await client.connect()
if not await client.is_user_authorized():
raise RuntimeError("the source session is not authorized")
user = await client.get_me()
except Exception:
if client is not None:
await client.disconnect()
elif sqlite_session is not None:
sqlite_session.close()
session_file.unlink(missing_ok=True)
raise
else:
await client.disconnect()
try:
session_file.chmod(0o600)
except OSError:
# Some filesystems do not implement POSIX permissions.
pass
return session_file, user
async def _run(args: argparse.Namespace) -> int:
try:
string_env, name_env, default_target = _env_names(args.account)
except ValueError as exc:
print(f"Error: {exc}", file=sys.stderr)
return 1
api_id_raw = os.getenv("TELEGRAM_API_ID")
api_hash = os.getenv("TELEGRAM_API_HASH")
session_string = os.getenv(string_env)
if not api_id_raw or not api_hash:
print("Error: TELEGRAM_API_ID and TELEGRAM_API_HASH must be set", file=sys.stderr)
return 1
try:
api_id = int(api_id_raw)
except ValueError:
print("Error: TELEGRAM_API_ID must be an integer", file=sys.stderr)
return 1
if not session_string:
print(f"Error: {string_env} must be set", file=sys.stderr)
return 1
target = args.target or os.getenv(name_env) or default_target
try:
session_file, user = await migrate(session_string, api_id, api_hash, target)
except Exception as exc:
print(f"Migration failed: {exc}", file=sys.stderr)
return 1
display_name = getattr(user, "first_name", None) or "Telegram user"
user_id = getattr(user, "id", "unknown")
session_name = str(session_file.with_suffix(""))
print(f"Migration successful: {display_name} (id={user_id})")
print(f"Created {session_file}")
print("\nUpdate your environment:")
print(f" {name_env}={session_name}")
print(f" remove or comment out {string_env}")
print("\nKeep the .session file private and do not commit it.")
return 0
def main() -> None:
load_dotenv()
_check_installation()
raise SystemExit(asyncio.run(_run(_parse_args())))
if __name__ == "__main__":
main()