forked from south1907/addmember-telegram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_session.py
More file actions
82 lines (66 loc) · 2.14 KB
/
init_session.py
File metadata and controls
82 lines (66 loc) · 2.14 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
#!/usr/bin/env python3
import json
import logging
import sys
from pathlib import Path
from typing import Dict, Any
from telethon import TelegramClient
from utils import *
def load_config() -> Dict[str, Any]:
"""Load and validate configuration."""
try:
with open('config.json', 'r', encoding='utf-8') as f:
config = json.loads(f.read())
required_fields = ['accounts', 'api_id', 'api_hash']
for field in required_fields:
if field not in config:
raise ValueError(f"Missing required config field: {field}")
return config
except Exception as e:
logging.error(f"Failed to load config: {e}")
sys.exit(1)
def initialize_session(phone: str, session_path: Path, api_id: int, api_hash: str) -> bool:
"""Initialize a single Telegram session."""
try:
client = TelegramClient(
str(session_path / phone),
api_id,
api_hash
)
client.start()
if client.is_user_authorized():
logging.info(f"Login successful for {phone}")
return True
else:
logging.warning(f"Login failed for {phone}")
return False
except Exception as e:
logging.error(f"Error initializing session for {phone}: {e}")
return False
finally:
if 'client' in locals():
client.disconnect()
def main():
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
# Load configuration
config = load_config()
# Setup paths
session_path = Path('session')
session_path.mkdir(exist_ok=True)
# Initialize sessions
success_count = 0
for phone in config['accounts']:
if initialize_session(
phone,
session_path,
int(config['api_id']),
config['api_hash']
):
success_count += 1
logging.info(f"Successfully initialized {success_count} out of {len(config['accounts'])} sessions")
if __name__ == "__main__":
main()