Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convector StringSession to SQLiteSession telethon

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.")
like image 603
Mr_Progger Avatar asked May 24 '26 10:05

Mr_Progger


1 Answers

The following should work in v1 of the library, however, it will change in future versions:

string = StringSession(...)
client = TelegramClient('sqlite-session-file-path', api_id, api_hash)

client.session.set_dc(string.dc_id, string.server_address, string.port)
client.session.auth_key = string.auth_key

You can always refer to the source code to see how it works:

  • https://github.com/LonamiWebs/Telethon/blob/v1.24.0/telethon/sessions/sqlite.py
  • https://github.com/LonamiWebs/Telethon/blob/v1.24.0/telethon/sessions/string.py
  • https://github.com/LonamiWebs/Telethon/blob/v1.24.0/telethon/sessions/memory.py
like image 178
Lonami Avatar answered May 26 '26 00:05

Lonami



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!