Repository navigation
Expand file tree
/
Copy pathbot.py
More file actions
709 lines (635 loc) · 29.4 KB
/
Copy pathbot.py
File metadata and controls
709 lines (635 loc) · 29.4 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
#!/usr/bin/env python3
"""
SessionMaster - Advanced Telegram Session Management Bot
Main application file
"""
import telebot
import asyncio
import os
import logging
import io
import sys
import time
import shutil
import threading
from datetime import datetime
from pathlib import Path
from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton
# Import modules with error handling
try:
from config import config
from database import db
from utils import sanitize_log_input, get_telegram_client
from session_manager import (
validate_session, get_session_info, subscribe_to_channel,
check_spam_block, send_message_to_user, delete_dialogs_by_type,
export_contacts, check_crypto_wallet
)
from session_operations import create_tdata
from exceptions import ConfigurationError
from strings import STRINGS
from scheduler import message_scheduler
from async_worker import AsyncWorker
from auth_manager import auth_manager
from web_interface import start_web
except ImportError as e:
print(f"Import warning: {e}")
# Create minimal fallbacks
STRINGS = {'bot_started': 'Bot started successfully'}
# Ensure auth_manager imported separately so phone auth still works if other optional imports fail
AUTH_AVAILABLE = False
try:
from auth_manager import auth_manager
AUTH_AVAILABLE = True
except Exception as e:
logging.warning(f"Auth manager not available: {e}")
# Global variables
bot = None
TOKEN = None
pending_message = {}
async_worker = None
def setup_logging():
"""Setup logging configuration"""
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('bot.log'),
logging.StreamHandler()
]
)
def create_directories():
"""Create necessary directories"""
directories = ['sessions', 'temp', 'logs', 'tdata']
for directory in directories:
os.makedirs(directory, exist_ok=True)
def check_config():
"""Check configuration"""
required_vars = ['API_ID', 'API_HASH', 'BOT_TOKEN']
missing = []
for var in required_vars:
if not config.get(var.lower()):
missing.append(var)
if missing:
print(f"Missing configuration: {', '.join(missing)}")
return False
return True
def get_user_session(user_id):
"""Get user session path"""
session_path = f"sessions/id_{user_id}.session"
if os.path.exists(session_path):
return session_path
return None
def run_coro_sync(coro):
"""Run coroutine synchronously"""
try:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
return loop.run_until_complete(coro)
except Exception as e:
logging.error(f"Error running coroutine: {e}")
return None
finally:
loop.close()
def send_session_info(message, session_id, info):
"""Send session information to user"""
if not info:
bot.send_message(message.chat.id, "Error getting session information.")
return
text = f"📱 **Session Info**\\n\\n"
text += f"👤 **Name:** {info.get('name', 'Unknown')}\\n"
text += f"🆔 **ID:** {info.get('id', 'Unknown')}\\n"
text += f"📞 **Phone:** {info.get('phone', 'Unknown')}\\n"
if info.get('username'):
text += f"📛 **Username:** @{info['username']}\\n"
text += f"💬 **Dialogs:** {info.get('dialogs', 0)}\\n"
text += f"📢 **Channels:** {info.get('channels', 0)}\\n"
text += f"👥 **Groups:** {info.get('groups', 0)}\\n"
text += f"🤖 **Bots:** {info.get('bots', 0)}\\n"
text += f"💭 **Private Chats:** {info.get('private_chats', 0)}\\n"
if info.get('premium'):
text += "⭐ **Premium Account**\\n"
if info.get('verified'):
text += "✅ **Verified Account**\\n"
# Create inline keyboard
markup = InlineKeyboardMarkup()
markup.row(
InlineKeyboardButton("📊 Update", callback_data="update"),
InlineKeyboardButton("🚫 Spam Check", callback_data="spamcheck")
)
markup.row(
InlineKeyboardButton("💬 Send Message", callback_data="message_user"),
InlineKeyboardButton("📢 Subscribe", callback_data="subscribe")
)
markup.row(
InlineKeyboardButton("🗑️ Delete Dialogs", callback_data="delete"),
InlineKeyboardButton("📋 Export Contacts", callback_data="import_contacts")
)
markup.row(
InlineKeyboardButton("💾 Generate TData", callback_data="tdata"),
InlineKeyboardButton("💰 Check Wallet", callback_data="toggle_crypto")
)
try:
if info.get('avatar'):
bot.send_photo(message.chat.id, info['avatar'], caption=text,
reply_markup=markup, parse_mode='Markdown')
else:
bot.send_message(message.chat.id, text, reply_markup=markup, parse_mode='Markdown')
except Exception as e:
logging.error(f"Error sending session info: {e}")
# Fallback without markdown
bot.send_message(message.chat.id, text.replace('*', ''), reply_markup=markup)
def register_handlers():
"""Register bot handlers"""
@bot.message_handler(commands=["start"])
def start_handler(message):
session_path = get_user_session(message.chat.id)
if session_path:
try:
info = run_coro_sync(get_session_info(session_path))
session_id = f"id_{message.chat.id}"
send_session_info(message, session_id, info)
except Exception as e:
logging.error(f"Error getting session info: {e}")
bot.send_message(message.chat.id, "Error getting session information.")
else:
markup = InlineKeyboardMarkup()
markup.add(InlineKeyboardButton("📱 Add by Phone", callback_data="add_session_phone"))
bot.send_message(message.chat.id, "No sessions found. Choose how to add your account:", reply_markup=markup)
bot.send_message(message.chat.id, "Or send a .session file directly")
@bot.message_handler(content_types=['document'])
def handle_session_file(message):
if not message.document.file_name.endswith(".session"):
bot.reply_to(message, "Please send a .session file")
return
try:
file_info = bot.get_file(message.document.file_id)
downloaded_file = bot.download_file(file_info.file_path)
except Exception as e:
logging.error(f"Error downloading file: {e}")
bot.reply_to(message, "Error downloading file.")
return
session_path = f"sessions/id_{message.chat.id}.session"
try:
with open(session_path, "wb") as f:
f.write(downloaded_file)
logging.debug(f"Session file saved to {session_path}")
except Exception as e:
logging.error(f"Error saving session file: {e}")
bot.reply_to(message, "Error saving file.")
return
if run_coro_sync(validate_session(session_path)):
try:
if 'db' in globals():
db.save_session(message.chat.id, f"id_{message.chat.id}", session_path)
info = run_coro_sync(get_session_info(session_path))
session_id = f"id_{message.chat.id}"
send_session_info(message, session_id, info)
except Exception as e:
logging.error(f"Error processing session info: {e}")
bot.reply_to(message, "Error processing session information.")
else:
os.remove(session_path)
logging.warning(f"Session file {session_path} is invalid.")
bot.reply_to(message, "Invalid session file.")
@bot.callback_query_handler(func=lambda call: call.data == "subscribe")
def handle_subscribe(call):
try:
bot.delete_message(call.message.chat.id, call.message.message_id)
except Exception as e:
logging.error(f"Error deleting message: {e}")
msg = bot.send_message(call.message.chat.id, "Send channel link:")
bot.register_next_step_handler(msg, process_subscription)
def process_subscription(message):
session_path = get_user_session(message.chat.id)
if not session_path:
bot.send_message(message.chat.id, "Session not found, please send session file.")
return
if run_coro_sync(subscribe_to_channel(session_path, message.text)):
bot.send_message(message.chat.id, "Successfully subscribed!")
try:
info = run_coro_sync(get_session_info(session_path))
session_id = f"id_{message.chat.id}"
send_session_info(message, session_id, info)
except Exception as e:
logging.error(f"Error refreshing session info after subscription: {e}")
bot.send_message(message.chat.id, "Error updating session information.")
else:
bot.send_message(message.chat.id, "Subscription error.")
@bot.callback_query_handler(func=lambda call: call.data == "spamcheck")
def handle_spam_check(call):
session_path = get_user_session(call.message.chat.id)
if not session_path:
bot.answer_callback_query(call.id, "Session not found!")
return
result = run_coro_sync(check_spam_block(session_path))
bot.answer_callback_query(call.id, f"Spam block: {'Yes' if result else 'No'}")
@bot.callback_query_handler(func=lambda call: call.data == "update")
def handle_update(call):
session_path = get_user_session(call.message.chat.id)
if not session_path:
bot.answer_callback_query(call.id, "Session not found!")
return
try:
info = run_coro_sync(get_session_info(session_path))
session_id = f"id_{call.message.chat.id}"
bot.send_message(call.message.chat.id, "Updated data:")
send_session_info(call.message, session_id, info)
except Exception as e:
logging.error(f"Error updating session info: {e}")
@bot.callback_query_handler(func=lambda call: call.data == "message_user")
def handle_message_user(call):
bot.send_message(call.message.chat.id, "Enter recipient username (without @):")
bot.register_next_step_handler(call.message, process_recipient_username)
def process_recipient_username(message):
chat_id = message.chat.id
pending_message[chat_id] = {"recipient": message.text.strip()}
bot.send_message(chat_id, "Enter message text or send photo/video/file:")
bot.register_next_step_handler(message, process_message_content)
def process_message_content(message):
chat_id = message.chat.id
if chat_id not in pending_message or "recipient" not in pending_message[chat_id]:
bot.send_message(chat_id, "Error: recipient not specified.")
return
recipient = pending_message[chat_id]["recipient"]
session_path = get_user_session(chat_id)
if not session_path:
bot.send_message(chat_id, "Session not found.")
return
text = None
media_path = None
if message.content_type == "text":
text = message.text
elif message.content_type in ["photo", "video", "document"]:
try:
if message.content_type == "photo":
file_info = bot.get_file(message.photo[-1].file_id)
elif message.content_type == "video":
file_info = bot.get_file(message.video.file_id)
elif message.content_type == "document":
file_info = bot.get_file(message.document.file_id)
downloaded_file = bot.download_file(file_info.file_path)
if not os.path.exists("temp"):
os.makedirs("temp")
if message.content_type == "document":
filename = message.document.file_name
elif message.content_type == "photo":
filename = f"{message.message_id}_photo.jpg"
elif message.content_type == "video":
filename = f"{message.message_id}_video.mp4"
media_path = os.path.join("temp", filename)
with open(media_path, "wb") as f:
f.write(downloaded_file)
except Exception as e:
logging.error(f"Error downloading media: {e}")
bot.send_message(chat_id, "Error downloading file.")
return
else:
bot.send_message(chat_id, "Unsupported message type.")
return
result = run_coro_sync(send_message_to_user(session_path, recipient, text, media_path))
if result:
bot.send_message(chat_id, "Message sent.")
else:
bot.send_message(chat_id, "Message sending error.")
if media_path and os.path.exists(media_path):
os.remove(media_path)
pending_message.pop(chat_id, None)
@bot.callback_query_handler(func=lambda call: call.data == "delete")
def handle_delete(call):
markup = InlineKeyboardMarkup()
markup.row(
InlineKeyboardButton("All Channels", callback_data="delete_channels"),
InlineKeyboardButton("All Chats", callback_data="delete_chats")
)
markup.row(
InlineKeyboardButton("All Bots", callback_data="delete_bots"),
InlineKeyboardButton("All Private Dialogs", callback_data="delete_personal")
)
bot.send_message(call.message.chat.id, "What to delete?", reply_markup=markup)
@bot.callback_query_handler(func=lambda call: call.data.startswith("delete_"))
def handle_delete_type(call):
session_path = get_user_session(call.message.chat.id)
if not session_path:
bot.answer_callback_query(call.id, "Session not found!")
return
dialog_type = call.data.replace("delete_", "")
result = delete_dialogs_by_type(session_path, dialog_type)
if result:
bot.answer_callback_query(call.id, f"Deleted {dialog_type}.")
else:
bot.answer_callback_query(call.id, "Deletion error.")
try:
bot.delete_message(call.message.chat.id, call.message.message_id)
except Exception as e:
logging.error(f"Error deleting delete selection message: {e}")
try:
info = run_coro_sync(get_session_info(session_path))
session_id = f"id_{call.message.chat.id}"
bot.send_message(call.message.chat.id, "Updated data:")
send_session_info(call.message, session_id, info)
except Exception as e:
logging.error(f"Error updating session info after deletion: {e}")
@bot.callback_query_handler(func=lambda call: call.data == "import_contacts")
def handle_import_contacts(call):
session_path = get_user_session(call.message.chat.id)
if not session_path:
bot.answer_callback_query(call.id, "Session not found!")
return
contacts_file = run_coro_sync(export_contacts(session_path))
if contacts_file:
contacts_file.seek(0)
bot.send_document(call.message.chat.id, contacts_file, caption="Contacts")
bot.answer_callback_query(call.id, "Contacts exported.")
else:
bot.answer_callback_query(call.id, "Contact export error.")
@bot.callback_query_handler(func=lambda call: call.data == "tdata")
def handle_tdata(call):
session_path = get_user_session(call.message.chat.id)
if not session_path:
bot.answer_callback_query(call.id, "Session not found!")
return
try:
zip_path = run_coro_sync(create_tdata(session_path))
if zip_path and os.path.exists(zip_path):
with open(zip_path, "rb") as f:
bot.send_document(call.message.chat.id, f, caption="TData archive")
bot.answer_callback_query(call.id, "TData generated.")
os.remove(zip_path)
dir_path = os.path.dirname(zip_path)
shutil.rmtree(dir_path, ignore_errors=True)
else:
bot.answer_callback_query(call.id, "TData generation error.")
except Exception as e:
logging.error(f"TData generation error: {e}")
bot.answer_callback_query(call.id, "TData generation error.")
@bot.callback_query_handler(func=lambda call: call.data == "toggle_crypto")
def handle_crypto(call):
session_path = get_user_session(call.message.chat.id)
if not session_path:
bot.answer_callback_query(call.id, "Session not found!")
return
result = run_coro_sync(check_crypto_wallet(session_path))
bot.send_message(call.message.chat.id, f"💵 Your balance:\\n\\n{result}")
bot.answer_callback_query(call.id, "Check completed.")
@bot.message_handler(commands=["wallet"])
def wallet_handler(message):
session_path = get_user_session(message.chat.id)
if not session_path:
bot.send_message(message.chat.id, "Session not found!")
return
result = run_coro_sync(check_crypto_wallet(session_path))
bot.send_message(message.chat.id, f"💵 Your balance:\\n\\n{result}")
# Phone authentication handlers
@bot.callback_query_handler(func=lambda call: call.data == "add_session_phone")
def handle_add_phone(call):
"""Handle add account by phone"""
bot.send_message(call.message.chat.id, "Enter your phone number (with country code, e.g., +1234567890):")
bot.register_next_step_handler(call.message, process_phone_number)
bot.answer_callback_query(call.id)
@bot.callback_query_handler(func=lambda call: call.data == "resend_code")
def handle_resend_code(call):
"""Handle resend code button"""
try:
if AUTH_AVAILABLE:
result = auth_manager.resend_code(call.message.chat.id)
if result['success']:
bot.send_message(call.message.chat.id, f"📨 {result['message']}")
bot.send_message(call.message.chat.id, "⏳ Please wait a few seconds, then enter the new verification code:")
bot.register_next_step_handler(call.message, process_verification_code)
else:
bot.send_message(call.message.chat.id, f"❌ {result['error']}")
try:
bot.answer_callback_query(call.id)
except:
pass
except Exception as e:
logging.error(f"Resend callback error: {e}")
try:
bot.answer_callback_query(call.id, "Error resending code")
except:
pass
@bot.callback_query_handler(func=lambda call: call.data == "restart_auth")
def handle_restart_auth(call):
"""Handle restart auth button"""
try:
if AUTH_AVAILABLE:
auth_manager.cancel_auth(call.message.chat.id)
bot.send_message(call.message.chat.id, "Authentication cancelled. Enter your phone number to start again:")
bot.register_next_step_handler(call.message, process_phone_number)
try:
bot.answer_callback_query(call.id)
except:
pass
except Exception as e:
logging.error(f"Restart auth error: {e}")
try:
bot.answer_callback_query(call.id, "Error restarting auth")
except:
pass
def process_phone_number(message):
"""Process phone number input"""
phone = str(message.text).strip()
# Basic phone validation
if not phone.startswith('+') or len(phone) < 10:
bot.send_message(message.chat.id, "Invalid phone format. Please use format: +1234567890")
return
# Start authentication
try:
if AUTH_AVAILABLE:
result = auth_manager.start_phone_auth(message.chat.id, phone)
if result['success']:
bot.send_message(message.chat.id, result['message'])
bot.send_message(message.chat.id, "⏳ Please wait a few seconds, then enter the verification code:")
bot.register_next_step_handler(message, process_verification_code)
else:
error_msg = result['error']
if 'api_id/api_hash combination is invalid' in error_msg:
bot.send_message(message.chat.id, "❌ Invalid API credentials. Please contact the bot administrator to update API_ID and API_HASH from https://my.telegram.org/apps")
else:
bot.send_message(message.chat.id, f"Error: {error_msg}")
else:
bot.send_message(message.chat.id, "Phone authentication not available. Please upload a .session file instead.")
except Exception as e:
logging.error(f"Phone auth error: {e}")
bot.send_message(message.chat.id, f"Error: {str(e)}")
def process_verification_code(message):
"""Process verification code input"""
code = message.text.strip()
try:
if AUTH_AVAILABLE:
result = auth_manager.verify_code(message.chat.id, code)
if result['success']:
if result['step'] == 'complete':
# Authentication complete
user_info = result['user_info']
text = f"✅ Successfully logged in!\\n\\n👤 {user_info['name']}\\n🆔 {user_info['id']}\\n📱 {user_info['phone']}"
if user_info['username']:
text += f"\\n📛 @{user_info['username']}"
bot.send_message(message.chat.id, text)
elif result['step'] == '2fa':
# Need 2FA password
bot.send_message(message.chat.id, result['message'])
bot.register_next_step_handler(message, process_2fa_password)
else:
error_msg = result['error']
bot.send_message(message.chat.id, f"❌ {error_msg}")
if result.get('expired'):
# Code expired, offer resend option
markup = InlineKeyboardMarkup()
markup.add(InlineKeyboardButton("📨 Resend Code", callback_data="resend_code"))
markup.add(InlineKeyboardButton("🔄 Restart Auth", callback_data="restart_auth"))
bot.send_message(message.chat.id, "🔄 Code expired. Choose an option:", reply_markup=markup)
else:
# Allow retry
bot.send_message(message.chat.id, "Please enter the verification code again:")
bot.register_next_step_handler(message, process_verification_code)
else:
bot.send_message(message.chat.id, "Authentication not available.")
except Exception as e:
logging.error(f"Verification error: {e}")
bot.send_message(message.chat.id, f"Error: {str(e)}")
def process_2fa_password(message):
"""Process 2FA password input"""
password = message.text.strip()
# Delete the password message for security
try:
bot.delete_message(message.chat.id, message.message_id)
except:
pass
try:
if AUTH_AVAILABLE:
result = auth_manager.verify_2fa(message.chat.id, password)
if result['success']:
# Authentication complete
user_info = result['user_info']
text = f"✅ Successfully logged in with 2FA!\\n\\n👤 {user_info['name']}\\n🆔 {user_info['id']}\\n📱 {user_info['phone']}"
if user_info['username']:
text += f"\\n📛 @{user_info['username']}"
bot.send_message(message.chat.id, text)
else:
bot.send_message(message.chat.id, f"❌ {result['error']}")
# Allow retry
bot.send_message(message.chat.id, "Please enter your 2FA password again:")
bot.register_next_step_handler(message, process_2fa_password)
else:
bot.send_message(message.chat.id, "2FA authentication not available.")
except Exception as e:
logging.error(f"2FA error: {e}")
bot.send_message(message.chat.id, f"Error: {str(e)}")
@bot.message_handler(commands=['cancel'])
def cancel_handler(message):
"""Cancel any pending authentication"""
try:
if AUTH_AVAILABLE and auth_manager.cancel_auth(message.chat.id):
bot.send_message(message.chat.id, "❌ Authentication cancelled.")
else:
bot.send_message(message.chat.id, "No pending authentication to cancel.")
except Exception as e:
logging.error(f"Cancel error: {e}")
bot.send_message(message.chat.id, "Error cancelling authentication.")
@bot.message_handler(commands=['addphone'])
def addphone_handler(message):
"""Quick command to add phone"""
bot.send_message(message.chat.id, "Enter your phone number (with country code, e.g., +1234567890):")
bot.register_next_step_handler(message, process_phone_number)
@bot.message_handler(commands=['resend'])
def resend_handler(message):
"""Resend verification code"""
try:
if AUTH_AVAILABLE:
result = auth_manager.resend_code(message.chat.id)
if result['success']:
bot.send_message(message.chat.id, f"📨 {result['message']}")
bot.send_message(message.chat.id, "⏳ Please wait a few seconds, then enter the new verification code:")
bot.register_next_step_handler(message, process_verification_code)
else:
bot.send_message(message.chat.id, f"❌ {result['error']}")
else:
bot.send_message(message.chat.id, "Resend not available.")
except Exception as e:
logging.error(f"Resend error: {e}")
bot.send_message(message.chat.id, f"Error: {str(e)}")
def initialize_bot():
"""Initialize the bot instance"""
global TOKEN, bot, async_worker
TOKEN = config.get('bot_token')
if not TOKEN:
raise ConfigurationError("Bot token not configured")
# Validate API credentials early
api_id = config.get('api_id')
api_hash = config.get('api_hash')
if not api_id or not str(api_id).isdigit():
raise ConfigurationError("API_ID missing or invalid. Please set API_ID in your .env")
if not api_hash or len(str(api_hash)) != 32:
raise ConfigurationError("API_HASH missing or invalid. Get credentials from https://my.telegram.org/apps")
# Start async worker if available
try:
if 'AsyncWorker' in globals():
async_worker = AsyncWorker()
async_worker.start()
except Exception as e:
logging.warning(f"Could not start async worker: {e}")
bot = telebot.TeleBot(TOKEN)
register_handlers()
logging.info(STRINGS.get('bot_started', 'Bot started successfully'))
def start_scheduler():
"""Start the message scheduler"""
try:
if 'message_scheduler' in globals():
def _start_scheduler():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(message_scheduler.start())
loop.run_forever()
scheduler_thread = threading.Thread(target=_start_scheduler, daemon=True)
scheduler_thread.start()
except Exception as e:
logging.warning(f"Could not start scheduler: {e}")
def start_bot():
"""Start the Telegram bot"""
try:
print("Starting Telegram bot...")
start_scheduler()
while True:
try:
bot.polling(none_stop=True, timeout=60)
except Exception as e:
logging.error(f"Polling error: {sanitize_log_input(str(e)) if 'sanitize_log_input' in globals() else str(e)}")
time.sleep(15)
except Exception as e:
logging.error(f"Bot error: {e}")
time.sleep(5)
start_bot() # Restart on error
def main():
"""Main launcher function"""
print("SessionMaster v2.0")
print("=" * 50)
# Setup
setup_logging()
if not check_config():
sys.exit(1)
create_directories()
initialize_bot()
print("Configuration validated")
print("Directories created")
# Start services
try:
# Start web interface in background thread if available
try:
if 'start_web' in globals():
web_thread = threading.Thread(target=start_web, daemon=True)
web_thread.start()
print("Web interface started on http://localhost:5000")
except Exception as e:
logging.warning(f"Could not start web interface: {e}")
# Start bot in main thread
start_bot()
except KeyboardInterrupt:
print("\\nShutting down SessionMaster...")
sys.exit(0)
except Exception as e:
logging.error(f"Startup error: {e}")
sys.exit(1)
if __name__ == "__main__":
main()