Spaces:
Sleeping
Sleeping
File size: 4,344 Bytes
6b1e8b6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | #devggn
from pyrogram import filters, Client
from devgagan import app
from pyromod import listen
import random
import os
import string
from devgagan.core.mongo import db
from devgagan.core.func import subscribe, chk_user
from config import API_ID as api_id, API_HASH as api_hash
from pyrogram.errors import (
ApiIdInvalid,
PhoneNumberInvalid,
PhoneCodeInvalid,
PhoneCodeExpired,
SessionPasswordNeeded,
PasswordHashInvalid,
FloodWait
)
def generate_random_name(length=7):
characters = string.ascii_letters + string.digits
return ''.join(random.choice(characters) for _ in range(length)) # Editted ...
async def delete_session_files(user_id):
session_file = f"session_{user_id}.session"
memory_file = f"session_{user_id}.session-journal"
session_file_exists = os.path.exists(session_file)
memory_file_exists = os.path.exists(memory_file)
if session_file_exists:
os.remove(session_file)
if memory_file_exists:
os.remove(memory_file)
# Delete session from the database
if session_file_exists or memory_file_exists:
await db.delete_session(user_id)
return True # Files were deleted
return False # No files found
@app.on_message(filters.command("logout"))
async def clear_db(client, message):
user_id = message.chat.id
files_deleted = await delete_session_files(user_id)
if files_deleted:
await message.reply("β
Your session data and files have been cleared from memory and disk.")
else:
await message.reply("β οΈ You are not logged in, no session data found.")
@app.on_message(filters.command("login"))
async def generate_session(_, message):
joined = await subscribe(_, message)
if joined == 1:
return
# user_checked = await chk_user(message, message.from_user.id)
# if user_checked == 1:
# return
user_id = message.chat.id
number = await _.ask(user_id, 'Please enter your phone number along with the country code. \nExample: +19876543210', filters=filters.text)
phone_number = number.text
try:
await message.reply("π² Sending OTP...")
client = Client(f"session_{user_id}", api_id, api_hash)
await client.connect()
except Exception as e:
await message.reply(f"β Failed to send OTP {e}. Please wait and try again later.")
try:
code = await client.send_code(phone_number)
except ApiIdInvalid:
await message.reply('β Invalid combination of API ID and API HASH. Please restart the session.')
return
except PhoneNumberInvalid:
await message.reply('β Invalid phone number. Please restart the session.')
return
try:
otp_code = await _.ask(user_id, "Please check for an OTP in your official Telegram account. Once received, enter the OTP in the following format: \nIf the OTP is `12345`, please enter it as `1 2 3 4 5`.", filters=filters.text, timeout=600)
except TimeoutError:
await message.reply('β° Time limit of 10 minutes exceeded. Please restart the session.')
return
phone_code = otp_code.text.replace(" ", "")
try:
await client.sign_in(phone_number, code.phone_code_hash, phone_code)
except PhoneCodeInvalid:
await message.reply('β Invalid OTP. Please restart the session.')
return
except PhoneCodeExpired:
await message.reply('β Expired OTP. Please restart the session.')
return
except SessionPasswordNeeded:
try:
two_step_msg = await _.ask(user_id, 'Your account has two-step verification enabled. Please enter your password.', filters=filters.text, timeout=300)
except TimeoutError:
await message.reply('β° Time limit of 5 minutes exceeded. Please restart the session.')
return
try:
password = two_step_msg.text
await client.check_password(password=password)
except PasswordHashInvalid:
await two_step_msg.reply('β Invalid password. Please restart the session.')
return
string_session = await client.export_session_string()
await db.set_session(user_id, string_session)
await client.disconnect()
await otp_code.reply("β
Login successful!")
|