-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_code.py
More file actions
61 lines (52 loc) · 1.6 KB
/
request_code.py
File metadata and controls
61 lines (52 loc) · 1.6 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
import asyncio
import re
import sys
from pathlib import Path
from telethon import TelegramClient
SESSION_DIR = Path(__file__).resolve().parent / "session"
APP_ID = 2040
APP_HASH = "b18441a1ff607e10a989891a5462e627"
DEVICE_MODEL = "Windows 11 x64"
SYSTEM_VERSION = "Satellite C660"
APP_VERSION = "6.5.1 x64"
LANG_CODE = "en"
SYSTEM_LANG_CODE = "en-US"
def norm_phone(s: str) -> str:
d = re.sub(r"\D", "", s)
return "+" + d if d else s
async def request_code(phone: str, proxy=None) -> bool:
phone = norm_phone(phone)
if not re.fullmatch(r"\+\d+", phone):
print("Invalid phone")
return False
session_path = SESSION_DIR / ("request_" + re.sub(r"\D", "", phone))
client = TelegramClient(
str(session_path),
APP_ID,
APP_HASH,
proxy=proxy,
device_model=DEVICE_MODEL,
system_version=SYSTEM_VERSION,
app_version=APP_VERSION,
lang_code=LANG_CODE,
system_lang_code=SYSTEM_LANG_CODE,
)
try:
await client.connect()
if await client.is_user_authorized():
return False
await client.send_code_request(phone)
print("Code requested for", phone)
return True
except Exception:
return False
finally:
d = client.disconnect()
if d is not None:
await d
if __name__ == "__main__":
phone = sys.argv[1].strip() if len(sys.argv) > 1 else input("Phone: ").strip()
if not phone:
sys.exit(1)
ok = asyncio.run(request_code(phone))
sys.exit(0 if ok else 1)