Skip to content

Instantly share code, notes, and snippets.

@New-dev0
Last active May 30, 2025 11:09
Show Gist options
  • Select an option

  • Save New-dev0/c3b515d34cdd3470408a065ff48fc480 to your computer and use it in GitHub Desktop.

Select an option

Save New-dev0/c3b515d34cdd3470408a065ff48fc480 to your computer and use it in GitHub Desktop.
Script to Convert Telethon's StringSession to Pyrogram's
# Both Telethon and Pyrogram should be Installed
# Made only for Educational Purpose
# New-dev0 (2021)
import struct, base64
from telethon.sessions.string import StringSession
from telethon.sync import TelegramClient
from pyrogram.storage.storage import Storage
from pyrogram import utils
API_ID = 6
API_HASH = "eb06d4abfb49dc3eeb1aeb98ae0f581e"
def telethon_to_unpack(string):
ST = StringSession(string)
return ST
def pack_to_pyro(data, ses):
Dt = Storage.SESSION_STRING_FORMAT
return (
base64.urlsafe_b64encode(
struct.pack(Dt, data.dc_id, API_ID, None, data.auth_key.key, ses.id, ses.bot)
)
.decode()
.rstrip("=")
)
def start_session(string):
with TelegramClient(
StringSession(string), API_ID, API_HASH
) as ses:
ml = ses.get_me()
return ml
def tele_to_pyro(string):
DL = telethon_to_unpack(string)
MK = start_session(string)
return pack_to_pyro(DL, MK)
# Example
# telethon_string = "1A.....Z="
# tele_to_pyro(telethon_string)
@New-dev0

Copy link
Copy Markdown
Author

you can just Use/Import tele_to_pyro function and use it by inserting Telethon StringSession to Convert into StringSession of Pyrogram.

@gustch

gustch commented Feb 16, 2022

Copy link
Copy Markdown

Due to telegram update, dt = Storage.SESSION_STRING_FORMAT should fix to Storage.SESSION_STRING_FORMAT_64

@New-dev0

Copy link
Copy Markdown
Author

Thanks, fixed

@x53e76f0f

x53e76f0f commented May 12, 2022

Copy link
Copy Markdown

AttributeError: type object 'Storage' has no attribute 'SESSION_STRING_FORMAT_64'
Dt = Storage.SESSION_STRING_FORMAT if ses.id < utils.MAX_USER_ID_OLD else Storage.SESSION_STRING_FORMAT_64

@hongquanvn1998

Copy link
Copy Markdown

Hello. I have a question. What is param "string" into method tele_to_pyro. And how can i get it from session file. thanks for your help

@nazar220160

Copy link
Copy Markdown

@Lucienest

Copy link
Copy Markdown

@nazar220160

Copy link
Copy Markdown

these two doesn't support string session.

This script supports all kinds of sessions

@Lucienest

Copy link
Copy Markdown

these two doesn't support string session.

This script supports all kinds of sessions

now I saw @nazar220160
thanks for the script :)

@yarinas5

Copy link
Copy Markdown

someone can explain me how to start the script lol

@ayhan-dev

Copy link
Copy Markdown

😶

@yarinas5

Copy link
Copy Markdown

is it updated? cause past years have made several changes

@New-dev0

New-dev0 commented Jan 3, 2024

Copy link
Copy Markdown
Author

Updated the script!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment