|
8 | 8 |
|
9 | 9 | Usage: |
10 | 10 | python session_string_generator.py |
| 11 | + python session_string_generator.py --qr |
11 | 12 |
|
12 | 13 | Requirements: |
13 | 14 | - telethon |
|
19 | 20 | and usernames (e.g., "@mychannel"). |
20 | 21 | """ |
21 | 22 |
|
| 23 | +import argparse |
22 | 24 | import asyncio |
23 | 25 | import io |
24 | 26 | import os |
|
33 | 35 | load_dotenv() |
34 | 36 |
|
35 | 37 |
|
| 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 | + |
36 | 56 | def _check_installation() -> None: |
37 | 57 | try: |
38 | 58 | assert_safe_distribution() |
@@ -99,6 +119,7 @@ def _phone_login(client: TelegramClient) -> None: |
99 | 119 |
|
100 | 120 |
|
101 | 121 | def main() -> None: |
| 122 | + args = _parse_args() |
102 | 123 | _check_installation() |
103 | 124 |
|
104 | 125 | API_ID = os.getenv("TELEGRAM_API_ID") |
@@ -128,10 +149,15 @@ def main() -> None: |
128 | 149 | .lower() |
129 | 150 | ) |
130 | 151 |
|
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" |
135 | 161 |
|
136 | 162 | try: |
137 | 163 | client = TelegramClient(StringSession(), API_ID, API_HASH) |
|
0 commit comments