Last active
July 18, 2025 17:23
-
-
Save ergolyam/fde8656a5f08ad8e425379e8f664eae3 to your computer and use it in GitHub Desktop.
Script to get string telegram session
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env -S uv run --script | |
| # /// script | |
| # dependencies = [ "pyrofork", "tgcrypto-pyrofork" ] | |
| # /// | |
| import sys | |
| from pathlib import Path | |
| from pyrogram.client import Client | |
| tg_id: str = '1' | |
| tg_hash: str = 'b6b154c3707471f5339bd661645ed3d6' | |
| def get_output_path() -> Path: | |
| default_path = Path.cwd() / "session.txt" | |
| argv = sys.argv[1:] | |
| for flag in ( | |
| "--output", | |
| "-o" | |
| ): | |
| if flag in argv: | |
| idx = argv.index(flag) + 1 | |
| if idx < len(argv): | |
| return Path(argv[idx]).expanduser().resolve() | |
| print( | |
| "Error: --output/-o must be followed by a file path.", | |
| file=sys.stderr | |
| ) | |
| sys.exit(1) | |
| return default_path | |
| output_file: Path = get_output_path() | |
| if not output_file.parent.exists(): | |
| print( | |
| f"Error: directory '{output_file.parent}' does not exist.", | |
| file=sys.stderr | |
| ) | |
| sys.exit(1) | |
| with Client( | |
| name="dummy", | |
| api_id=tg_id, | |
| api_hash=tg_hash, | |
| in_memory=True | |
| ) as app: | |
| session_string = app.export_session_string() | |
| output_file.write_text( | |
| session_string, | |
| encoding="utf-8" | |
| ) | |
| print(f"Session string saved to: {output_file}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment