async def convert(sessionId, isShadow=None, userId=None):
if userId:
try:
if conn.cursor().execute(f'SELECT * FROM users WHERE userId == {userId}').fetchone()[0]:
path = f'users\\{userId}\\sessions\\'
else:
bot.send_message(userId, 'Ошибка, ваших сессий не найдено.')
return 'SESSIONS_NOT_FOUND not exception'
except Exception as e:
try:
bot.send_message(userId, 'Ошибка, ваших сессий не найдено.')
return f'SESSIONS_NOT_FOUND exception: {e}'
except telebot.apihelper.ApiTelegramException as e:
if 'chat not found' in e: return 'INVALID_ID'
else: return 'UNEXCEPTED_ERROR'
else:
if isShadow: path = f'shadowSessions\\'
else: path = f'sessions\\'
sessionData = json.load(open(f'{path}\\{sessionId.replace(".session",".json")}'))
app_id = sessionData["app_id"]
app_hash = sessionData["app_hash"]
try:
session = await SessionManager.from_telethon_file(Path(f'{path}{sessionId}'))
res = session.to_pyrogram_string()
return res
except Exception as e:
print(e)
return False
async def initAcc(sessionId, userId=None, isShadow=None):
try:
path = f'shadowSessions' if isShadow else f'users/{userId}/sessions' if userId else 'sessions'
if userId:
if not conn.cursor().execute(f'SELECT * FROM users WHERE userId == {userId}').fetchone()[0]:
bot.send_message(userId, 'Ошибка, ваших сессий не найдено.')
return 'SESSIONS_NOT_FOUND not exception'
else:
if isShadow:
path = f'shadowSessions'
else:
path = f'sessions'
sessionData = json.load(open(f'{path}/{sessionId.replace(".session",".json")}'))
app_id = sessionData["app_id"]
app_hash = sessionData["app_hash"]
proxy = sessionData["proxy"]
user = proxy[4] if proxy[4] != 'None' else None
password = proxy[5] if proxy[5] != 'None' else None
host = proxy[1]
port = proxy[2]
proxy = {
"scheme": 'socks5',
"hostname": host,
"port": int(port),
"username": user,
"password": password}
client = Client(f'{path}/{sessionId.replace(".session","")}', session_string=await convert(sessionId), api_id=app_id, api_hash=app_hash, proxy=proxy, lang_code='ru')
return client
except Exception as e:
print(e)
return False
1. Код конвертации telethon сессии в pyrogram сессию.
2. Создание объекта Client