refactor: update formatting

This commit is contained in:
Eugene Evstafev 2025-04-18 14:25:24 +01:00
parent ff0f42626b
commit eca9fb48cf
2 changed files with 622 additions and 463 deletions

1051
main.py

File diff suppressed because it is too large Load diff

View file

@ -40,46 +40,52 @@ except ValueError:
print("\n----- Telegram Session String Generator -----\n") print("\n----- Telegram Session String Generator -----\n")
print("This script will generate a session string for your Telegram account.") print("This script will generate a session string for your Telegram account.")
print("You will be asked to enter your phone number and the verification code sent to your Telegram app.") print(
"You will be asked to enter your phone number and the verification code sent to your Telegram app."
)
print("The generated session string can be added to your .env file.") print("The generated session string can be added to your .env file.")
print("\nYour credentials will NOT be stored on any server and are only used for local authentication.\n") print(
"\nYour credentials will NOT be stored on any server and are only used for local authentication.\n"
)
try: try:
# Connect to Telegram and generate the session string # Connect to Telegram and generate the session string
with TelegramClient(StringSession(), API_ID, API_HASH) as client: with TelegramClient(StringSession(), API_ID, API_HASH) as client:
# The client.session.save() function from StringSession returns the session string # The client.session.save() function from StringSession returns the session string
session_string = StringSession.save(client.session) session_string = StringSession.save(client.session)
print("\nAuthentication successful!") print("\nAuthentication successful!")
print("\n----- Your Session String -----") print("\n----- Your Session String -----")
print(f"\n{session_string}\n") print(f"\n{session_string}\n")
print("Add this to your .env file as:") print("Add this to your .env file as:")
print(f"TELEGRAM_SESSION_STRING={session_string}") print(f"TELEGRAM_SESSION_STRING={session_string}")
print("\nIMPORTANT: Keep this string private and never share it with anyone!") print("\nIMPORTANT: Keep this string private and never share it with anyone!")
# Optional: auto-update the .env file # Optional: auto-update the .env file
choice = input("\nWould you like to automatically update your .env file with this session string? (y/N): ") choice = input(
if choice.lower() == 'y': "\nWould you like to automatically update your .env file with this session string? (y/N): "
)
if choice.lower() == "y":
try: try:
# Read the current .env file # Read the current .env file
with open('.env', 'r') as file: with open(".env", "r") as file:
env_contents = file.readlines() env_contents = file.readlines()
# Update or add the SESSION_STRING line # Update or add the SESSION_STRING line
session_string_line_found = False session_string_line_found = False
for i, line in enumerate(env_contents): for i, line in enumerate(env_contents):
if line.startswith('TELEGRAM_SESSION_STRING='): if line.startswith("TELEGRAM_SESSION_STRING="):
env_contents[i] = f"TELEGRAM_SESSION_STRING={session_string}\n" env_contents[i] = f"TELEGRAM_SESSION_STRING={session_string}\n"
session_string_line_found = True session_string_line_found = True
break break
if not session_string_line_found: if not session_string_line_found:
env_contents.append(f"TELEGRAM_SESSION_STRING={session_string}\n") env_contents.append(f"TELEGRAM_SESSION_STRING={session_string}\n")
# Write back to the .env file # Write back to the .env file
with open('.env', 'w') as file: with open(".env", "w") as file:
file.writelines(env_contents) file.writelines(env_contents)
print("\n.env file updated successfully!") print("\n.env file updated successfully!")
except Exception as e: except Exception as e:
print(f"\nError updating .env file: {e}") print(f"\nError updating .env file: {e}")
@ -88,4 +94,4 @@ try:
except Exception as e: except Exception as e:
print(f"\nError: {e}") print(f"\nError: {e}")
print("Failed to generate session string. Please try again.") print("Failed to generate session string. Please try again.")
sys.exit(1) sys.exit(1)