44import getpass
55import argparse
66import logging
7- from typing import Dict
7+ from typing import Dict , Optional
88
99# Add project root to sys.path
1010project_root = Path (__file__ ).resolve ().parent .parent
1919import userbot .src .db_manager as db_manager
2020from userbot .src .encrypt import encryption_manager
2121
22- logging .basicConfig (level = logging .WARNING )
22+ logging .basicConfig (level = logging .WARNING , format = '%(name)s - %(levelname)s - %(message)s' )
2323logger = logging .getLogger ('manage_account_cli' )
2424logger .setLevel (logging .INFO )
2525
26- async def add_account_logic (args ) :
26+ async def add_account_logic (args : argparse . Namespace ) -> None :
2727 """
28- Logic to add a new account with a full interactive Telethon login session.
28+ Handles the logic for adding a new account with a full interactive session.
29+ Allows for detailed configuration of device, proxy, and other settings.
30+
31+ Args:
32+ args (argparse.Namespace): The command-line arguments, expecting `args.name`.
2933 """
3034 logger .info (f"--- Adding new account: { args .name } ---" )
3135
32- temp_client = None
36+ temp_client : Optional [ TelegramClient ] = None
3337 try :
3438 async with get_db () as db :
3539 if await db_manager .get_account (db , args .name ):
3640 logger .error (f"Error: Account '{ args .name } ' already exists." )
3741 return
3842
39- api_id = input ("Enter API ID: " ).strip ()
40- api_hash = getpass .getpass ("Enter API Hash: " ).strip ()
43+ api_id : str = input ("Enter API ID: " ).strip ()
44+ api_hash : str = getpass .getpass ("Enter API Hash: " ).strip ()
4145
4246 if not api_id .isdigit () or not api_hash :
4347 logger .error ("Error: API ID must be a number and API Hash cannot be empty." )
@@ -48,33 +52,80 @@ async def add_account_logic(args):
4852 await temp_client .connect ()
4953
5054 if not await temp_client .is_user_authorized ():
51- phone_number = input ("Session not found. Please enter your phone number (e.g., +1234567890): " ).strip ()
55+ phone_number : str = input ("Session not found. Please enter your phone number (e.g., +1234567890): " ).strip ()
5256 await temp_client .send_code_request (phone_number )
53- code = input ("Please enter the code you received: " ).strip ()
57+ code : str = input ("Please enter the code you received: " ).strip ()
5458 try :
5559 await temp_client .sign_in (phone_number , code )
5660 except SessionPasswordNeededError :
57- two_fa_password = getpass .getpass ("2FA Password required: " ).strip ()
61+ two_fa_password : str = getpass .getpass ("2FA Password required: " ).strip ()
5862 await temp_client .sign_in (password = two_fa_password )
5963
6064 me = await temp_client .get_me (input_peer = True )
61- user_id = me .user_id
62- access_hash = me .access_hash
65+ user_id : int = me .user_id
66+ access_hash : int = me .access_hash
6367 logger .info (f"Successfully logged in as user ID: { user_id } ." )
6468
6569 existing_by_id = await db_manager .get_account_by_user_id (db , user_id )
6670 if existing_by_id :
6771 logger .error (f"Error: This Telegram account (ID: { user_id } ) already exists as '{ existing_by_id .account_name } '." )
6872 return
6973
70- device_details = _generate_random_device ()
71- lang_code = input (f"Enter language code (e.g., ru, en) [ru]: " ).strip () or 'ru'
72- is_enabled = (input ("Enable this account now? (yes/no) [yes]: " ).strip ().lower () or 'yes' ).startswith ('y' )
73-
74+ # --- Interactive Configuration ---
75+ print ("\n --- Account Configuration ---" )
76+ lang_code : str = input (f"Enter language code (e.g., ru, en) [ru]: " ).strip () or 'ru'
77+ is_enabled : bool = (input ("Enable this account now? (yes/no) [yes]: " ).strip ().lower () or 'yes' ).startswith ('y' )
78+
79+ # Device Configuration
80+ if (input ("Configure custom device details? (yes/no) [no]: " ).strip ().lower ()).startswith ('y' ):
81+ device_model : str = input ("Enter device model: " ).strip ()
82+ system_version : str = input ("Enter system version: " ).strip ()
83+ app_version : str = input ("Enter app version: " ).strip ()
84+ else :
85+ device_details : Dict [str , str ] = _generate_random_device ()
86+ device_model = device_details ['device_model' ]
87+ system_version = device_details ['system_version' ]
88+ app_version = device_details ['app_version' ]
89+ logger .info ("Generated random device details." )
90+
91+ # Proxy Configuration
92+ proxy_type : Optional [str ] = None
93+ proxy_ip : Optional [str ] = None
94+ proxy_port : Optional [int ] = None
95+ proxy_username : Optional [str ] = None
96+ proxy_password : Optional [str ] = None
97+ if (input ("Configure a proxy for this account? (yes/no) [no]: " ).strip ().lower ()).startswith ('y' ):
98+ proxy_type_input = ""
99+ while proxy_type_input not in ["http" , "socks4" , "socks5" ]:
100+ proxy_type_input = input ("Enter proxy type (http, socks4, socks5): " ).strip ().lower ()
101+ proxy_type = proxy_type_input
102+ proxy_ip = input ("Enter proxy IP address: " ).strip ()
103+ proxy_port_str = ""
104+ while not proxy_port_str .isdigit ():
105+ proxy_port_str = input ("Enter proxy port: " ).strip ()
106+ proxy_port = int (proxy_port_str )
107+ if (input ("Does the proxy require authentication? (yes/no) [no]: " ).strip ().lower ()).startswith ('y' ):
108+ proxy_username = input ("Enter proxy username: " ).strip ()
109+ proxy_password = getpass .getpass ("Enter proxy password: " ).strip ()
110+
111+ logger .info ("\n Saving account to the database..." )
74112 new_account = await db_manager .add_account (
75- db , args .name , api_id , api_hash , lang_code , is_enabled ,
76- device_details ['device_model' ], device_details ['system_version' ], device_details ['app_version' ],
77- user_id , access_hash
113+ db ,
114+ account_name = args .name ,
115+ api_id = api_id ,
116+ api_hash = api_hash ,
117+ lang_code = lang_code ,
118+ is_enabled = is_enabled ,
119+ device_model = device_model ,
120+ system_version = system_version ,
121+ app_version = app_version ,
122+ user_telegram_id = user_id ,
123+ access_hash = access_hash ,
124+ proxy_type = proxy_type ,
125+ proxy_ip = proxy_ip ,
126+ proxy_port = proxy_port ,
127+ proxy_username = proxy_username ,
128+ proxy_password = proxy_password
78129 )
79130
80131 if new_account :
@@ -89,7 +140,7 @@ async def add_account_logic(args):
89140 await temp_client .disconnect ()
90141
91142
92- async def edit_account_logic (args ) :
143+ async def edit_account_logic (args : argparse . Namespace ) -> None :
93144 """Logic to edit an account's properties."""
94145 logger .info (f"--- Editing account: { args .name } ---" )
95146 async with get_db () as db :
@@ -108,7 +159,8 @@ async def edit_account_logic(args):
108159
109160# ... (other logic functions like toggle, delete)
110161
111- async def main ():
162+ async def main () -> None :
163+ """The main entry point for the CLI script."""
112164 parser = argparse .ArgumentParser (description = "DeBot Account Management CLI" )
113165 subparsers = parser .add_subparsers (dest = "command" , required = True )
114166
@@ -118,7 +170,7 @@ async def main():
118170
119171 # ... (full parser setup as in previous correct answer)
120172
121- args = parser .parse_args ()
173+ args : argparse . Namespace = parser .parse_args ()
122174
123175 await initialize_database ()
124176 if hasattr (args , 'func' ):
0 commit comments