refactor: update formatting
This commit is contained in:
parent
ff0f42626b
commit
eca9fb48cf
2 changed files with 622 additions and 463 deletions
|
|
@ -40,46 +40,52 @@ except ValueError:
|
|||
|
||||
print("\n----- Telegram Session String Generator -----\n")
|
||||
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("\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:
|
||||
# Connect to Telegram and generate the session string
|
||||
with TelegramClient(StringSession(), API_ID, API_HASH) as client:
|
||||
# The client.session.save() function from StringSession returns the session string
|
||||
session_string = StringSession.save(client.session)
|
||||
|
||||
|
||||
print("\nAuthentication successful!")
|
||||
print("\n----- Your Session String -----")
|
||||
print(f"\n{session_string}\n")
|
||||
print("Add this to your .env file as:")
|
||||
print(f"TELEGRAM_SESSION_STRING={session_string}")
|
||||
print("\nIMPORTANT: Keep this string private and never share it with anyone!")
|
||||
|
||||
|
||||
# Optional: auto-update the .env file
|
||||
choice = input("\nWould you like to automatically update your .env file with this session string? (y/N): ")
|
||||
if choice.lower() == 'y':
|
||||
choice = input(
|
||||
"\nWould you like to automatically update your .env file with this session string? (y/N): "
|
||||
)
|
||||
if choice.lower() == "y":
|
||||
try:
|
||||
# Read the current .env file
|
||||
with open('.env', 'r') as file:
|
||||
with open(".env", "r") as file:
|
||||
env_contents = file.readlines()
|
||||
|
||||
|
||||
# Update or add the SESSION_STRING line
|
||||
session_string_line_found = False
|
||||
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"
|
||||
session_string_line_found = True
|
||||
break
|
||||
|
||||
|
||||
if not session_string_line_found:
|
||||
env_contents.append(f"TELEGRAM_SESSION_STRING={session_string}\n")
|
||||
|
||||
|
||||
# Write back to the .env file
|
||||
with open('.env', 'w') as file:
|
||||
with open(".env", "w") as file:
|
||||
file.writelines(env_contents)
|
||||
|
||||
|
||||
print("\n.env file updated successfully!")
|
||||
except Exception as e:
|
||||
print(f"\nError updating .env file: {e}")
|
||||
|
|
@ -88,4 +94,4 @@ try:
|
|||
except Exception as e:
|
||||
print(f"\nError: {e}")
|
||||
print("Failed to generate session string. Please try again.")
|
||||
sys.exit(1)
|
||||
sys.exit(1)
|
||||
|
|
|
|||
Loading…
Reference in a new issue