-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth_setup.py
More file actions
101 lines (82 loc) · 3.18 KB
/
auth_setup.py
File metadata and controls
101 lines (82 loc) · 3.18 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
#!/usr/bin/env python3
"""
Telegram Authentication Setup Script
This script should be run interactively (not as a service) to set up
the initial authentication session for the Telegram client.
Usage:
python auth_setup.py
After successful authentication, the session will be saved and the
main client can run headless as a service.
"""
import asyncio
import logging
from telethon import TelegramClient
from config import Config, setup_logging, validate_config
async def setup_authentication():
"""Set up Telegram authentication interactively"""
logger = setup_logging()
logger.info("Starting Telegram Authentication Setup")
try:
# Validate configuration
validate_config()
# Create client
client = TelegramClient(
'telegram_session',
int(Config.TELEGRAM_API_ID),
Config.TELEGRAM_API_HASH
)
logger.info("Starting authentication process...")
logger.info(f"Phone number: {Config.TELEGRAM_PHONE}")
# Start the client - this will prompt for verification code
if Config.TELEGRAM_PASSWORD:
logger.info("Using 2FA password from configuration")
await client.start(phone=Config.TELEGRAM_PHONE, password=Config.TELEGRAM_PASSWORD)
else:
logger.info("Starting authentication (you may be prompted for verification code)")
await client.start(phone=Config.TELEGRAM_PHONE)
# Get user info to confirm successful login
me = await client.get_me()
logger.info(f"✅ Authentication successful!")
logger.info(f"Logged in as: {me.first_name} (@{me.username})")
logger.info(f"User ID: {me.id}")
# Test connection
logger.info("Testing connection...")
await client.get_dialogs(limit=1)
logger.info("✅ Connection test successful!")
# Disconnect
await client.disconnect()
logger.info("✅ Authentication setup completed successfully!")
logger.info("The session has been saved to 'telegram_session.session'")
logger.info("You can now run the main client as a service.")
return True
except Exception as e:
logger.error(f"❌ Authentication setup failed: {e}")
return False
def main():
"""Main function"""
print("=" * 60)
print("Telegram Authentication Setup")
print("=" * 60)
print()
print("This script will help you authenticate with Telegram.")
print("You may receive a verification code via SMS or Telegram app.")
print()
try:
success = asyncio.run(setup_authentication())
if success:
print()
print("🎉 Setup completed successfully!")
print("You can now start the main service.")
else:
print()
print("❌ Setup failed. Please check your configuration and try again.")
return 1
except KeyboardInterrupt:
print("\n❌ Setup cancelled by user.")
return 1
except Exception as e:
print(f"\n❌ Unexpected error: {e}")
return 1
return 0
if __name__ == "__main__":
exit(main())