Good day! I work with the telethon library and have a program that converts tdata (Telegram session) to StringSession telethon, but I need a telethon session in SQLite format (SQLiteSession), I found a code that can do the opposite, convert SQLiteSession to StringSession
data = StringSession.save (client.session)
but when trying to do the opposite, I get an error.
the code:
data = SQLiteSession.save (client.session)
error:
Traceback (most recent call last):
File "C: Users Dedik2 Desktop New folder TdataToSessoin main.py", line 40, in <module>
data = SQLiteSession.save (client.session)
File "d: programs python3.7.9 lib site-packages telethon sessions sqlite.py", line 222, in save
if self._conn is not None:
AttributeError: 'StringSession' object has no attribute '_conn'
Please help me to solve this problem
If you need it, here’s my entire code:
import os
from loguru import logger
from telethon.sync import TelegramClient
from telethon.sessions import StringSession, SQLiteSession
from convert_tdata import convert_tdata
API_HASH = "asahdjakshdk"
API_ID = 123123
sessions = []
for tdata in os.listdir ("tdatas"):
try:
auth_key = convert_tdata (f "tdatas / {tdata}") [0]
except Exception as err:
logger.error (err)
else:
logger.success (f "{tdata} converted successfully")
sessions.append (StringSession (auth_key))
logger.info ("Checking accounts")
for session in sessions:
client = TelegramClient (
session,
api_hash = API_HASH,
api_id = API_ID
)
try:
client.connect ()
me = client.get_me ()
except Exception as err:
logger.error (err)
else:
phone = client.get_me (). phone
auth_key = client.session.save ()
with TelegramClient (StringSession (auth_key), API_ID, API_HASH) as client:
data = SQLiteSession.save (client.session)
with open (f "sessions / {phone} .session", "w") as file:
file.write (auth_key)
logger.success (f "{me.phone} - saved.")
2
Answers
If you are also unable to get account authorization, you can try my code.
Sometimes the program couldn't find
client.session.auth_key.key
and I had to addtry except
The following should work in v1 of the library, however, it will change in future versions:
You can always refer to the source code to see how it works: