Skip to content

Commit 05d4c14

Browse files
author
iqdoctor
committed
Make QR session login scriptable
Allow operators to choose QR login directly with a --qr flag instead of navigating the interactive login-method prompt. Keep the existing prompt as the default for backwards compatibility, and add --phone for symmetric explicit selection. Constraint: Upstream already has QR login support; the missing piece is a non-interactive CLI selector for operational runbooks. Rejected: Adding a separate QR generator script | duplicates existing session generator logic and increases maintenance surface. Confidence: high Scope-risk: narrow Directive: Keep session strings private; do not log generated session strings in automated wrappers. Tested: uv run black --check session_string_generator.py tests/test_session_string_generator.py; uv run pytest tests/test_session_string_generator.py -q; uv run pytest -q Not-tested: Real Telegram QR authorization, because it requires live account interaction.
1 parent 971158e commit 05d4c14

2 files changed

Lines changed: 67 additions & 4 deletions

File tree

session_string_generator.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
99
Usage:
1010
python session_string_generator.py
11+
python session_string_generator.py --qr
1112
1213
Requirements:
1314
- telethon
@@ -19,6 +20,7 @@
1920
and usernames (e.g., "@mychannel").
2021
"""
2122

23+
import argparse
2224
import asyncio
2325
import io
2426
import os
@@ -33,6 +35,24 @@
3335
load_dotenv()
3436

3537

38+
def _parse_args() -> argparse.Namespace:
39+
parser = argparse.ArgumentParser(
40+
description="Generate a Telegram session string for telegram-mcp."
41+
)
42+
login_group = parser.add_mutually_exclusive_group()
43+
login_group.add_argument(
44+
"--qr",
45+
action="store_true",
46+
help="Use Telegram QR login without prompting for a login method.",
47+
)
48+
login_group.add_argument(
49+
"--phone",
50+
action="store_true",
51+
help="Use phone number + verification code login without prompting for a login method.",
52+
)
53+
return parser.parse_args()
54+
55+
3656
def _check_installation() -> None:
3757
try:
3858
assert_safe_distribution()
@@ -99,6 +119,7 @@ def _phone_login(client: TelegramClient) -> None:
99119

100120

101121
def main() -> None:
122+
args = _parse_args()
102123
_check_installation()
103124

104125
API_ID = os.getenv("TELEGRAM_API_ID")
@@ -128,10 +149,15 @@ def main() -> None:
128149
.lower()
129150
)
130151

131-
print("\nChoose login method:")
132-
print(" 1) QR code login (recommended -- scan from your Telegram app)")
133-
print(" 2) Phone number + verification code")
134-
method = input("\nEnter 1 or 2 [default: 1]: ").strip() or "1"
152+
if args.qr:
153+
method = "1"
154+
elif args.phone:
155+
method = "2"
156+
else:
157+
print("\nChoose login method:")
158+
print(" 1) QR code login (recommended -- scan from your Telegram app)")
159+
print(" 2) Phone number + verification code")
160+
method = input("\nEnter 1 or 2 [default: 1]: ").strip() or "1"
135161

136162
try:
137163
client = TelegramClient(StringSession(), API_ID, API_HASH)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import pytest
2+
3+
import session_string_generator
4+
5+
6+
def test_parse_args_qr_selects_qr_login(monkeypatch):
7+
monkeypatch.setattr("sys.argv", ["session_string_generator.py", "--qr"])
8+
9+
args = session_string_generator._parse_args()
10+
11+
assert args.qr is True
12+
assert args.phone is False
13+
14+
15+
def test_parse_args_phone_selects_phone_login(monkeypatch):
16+
monkeypatch.setattr("sys.argv", ["session_string_generator.py", "--phone"])
17+
18+
args = session_string_generator._parse_args()
19+
20+
assert args.qr is False
21+
assert args.phone is True
22+
23+
24+
def test_parse_args_without_flags_keeps_interactive_login_choice(monkeypatch):
25+
monkeypatch.setattr("sys.argv", ["session_string_generator.py"])
26+
27+
args = session_string_generator._parse_args()
28+
29+
assert args.qr is False
30+
assert args.phone is False
31+
32+
33+
def test_parse_args_rejects_conflicting_login_modes(monkeypatch):
34+
monkeypatch.setattr("sys.argv", ["session_string_generator.py", "--qr", "--phone"])
35+
36+
with pytest.raises(SystemExit):
37+
session_string_generator._parse_args()

0 commit comments

Comments
 (0)