Add SMS option to session generator
This commit is contained in:
@@ -3,14 +3,16 @@
|
||||
Generate a Telethon StringSession for OverUB.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
import os
|
||||
|
||||
from telethon import TelegramClient
|
||||
from telethon.errors import SessionPasswordNeededError
|
||||
from telethon.sessions import StringSession
|
||||
|
||||
|
||||
async def generate() -> None:
|
||||
async def generate(args: argparse.Namespace) -> None:
|
||||
if os.path.exists(".env"):
|
||||
with open(".env", "r", encoding="utf-8") as handle:
|
||||
for line in handle:
|
||||
@@ -33,7 +35,26 @@ async def generate() -> None:
|
||||
|
||||
print("Starting session generation. You will be prompted to log in.")
|
||||
client = TelegramClient(StringSession(), api_id, api_hash)
|
||||
await client.start()
|
||||
await client.connect()
|
||||
if await client.is_user_authorized():
|
||||
session_string = client.session.save()
|
||||
await client.disconnect()
|
||||
print("\nSession string:")
|
||||
print(session_string)
|
||||
print("\nAdd to .env as OVERUB_SESSION_STRING=" + session_string)
|
||||
return
|
||||
phone = args.phone or os.getenv("OVERUB_PHONE") or os.getenv("PHONE")
|
||||
if not phone:
|
||||
phone = input("Phone number (+123456789): ").strip()
|
||||
sent = await client.send_code_request(phone, force_sms=args.sms)
|
||||
code_type = getattr(sent, "type", None)
|
||||
print(f"Code delivery type: {type(code_type).__name__ if code_type else 'unknown'}")
|
||||
code = input("Enter the code you received: ").strip()
|
||||
try:
|
||||
await client.sign_in(phone=phone, code=code, phone_code_hash=sent.phone_code_hash)
|
||||
except SessionPasswordNeededError:
|
||||
password = input("Two-step verification password: ").strip()
|
||||
await client.sign_in(password=password)
|
||||
session_string = client.session.save()
|
||||
await client.disconnect()
|
||||
|
||||
@@ -43,7 +64,11 @@ async def generate() -> None:
|
||||
|
||||
|
||||
def main() -> None:
|
||||
asyncio.run(generate())
|
||||
parser = argparse.ArgumentParser(description="Generate Telethon StringSession for OverUB")
|
||||
parser.add_argument("--phone", default="")
|
||||
parser.add_argument("--sms", action="store_true", help="Force SMS delivery")
|
||||
args = parser.parse_args()
|
||||
asyncio.run(generate(args))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user