skip to Main Content

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


  1. Chosen as BEST ANSWER

    If you are also unable to get account authorization, you can try my code.

    from telethon.sync import TelegramClient
    from telethon.sessions import StringSession
    from loguru import logger
    def main(key, phone, API_HASH, API_ID):
          string = StringSession(key)
          client = TelegramClient(f'sessions/{phone}', API_ID, API_HASH)
          try:
              client.session.auth_key.key = string.auth_key.key
          except:
              client.session.auth_key = string.auth_key
              client.connect()
              client.disable()
              client.session.auth_key.key = string.auth_key.key
          client.connect()
          if client.is_user_authorized():
              client.disable()
          else:
              logger.error(f"{phone} - conversion problem")
              client.disable()
              exit()
          return
    

    Sometimes the program couldn't find client.session.auth_key.key and I had to add try except


  2. 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:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search