Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
#
|
2 |
from flask import Flask, render_template, jsonify, request
|
3 |
from pyrogram import Client
|
4 |
import os
|
@@ -52,11 +52,12 @@ def system_info():
|
|
52 |
})
|
53 |
|
54 |
@app.route('/api/bot-stats')
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
60 |
free = 536870912 - monsize
|
61 |
monsize = get_size(monsize)
|
62 |
free = get_size(free)
|
@@ -69,15 +70,16 @@ async def bot_stats():
|
|
69 |
})
|
70 |
|
71 |
@app.route('/api/filters')
|
72 |
-
|
73 |
chat_id = request.args.get('chat_id')
|
74 |
if not chat_id:
|
75 |
return jsonify({"error": "Chat ID is required"}), 400
|
76 |
-
|
|
|
77 |
return jsonify({"filters": filters})
|
78 |
|
79 |
@app.route('/api/add-filter', methods=['POST'])
|
80 |
-
|
81 |
chat_id = request.form.get('chat_id')
|
82 |
text = request.form.get('text')
|
83 |
reply_text = request.form.get('reply_text')
|
@@ -86,26 +88,29 @@ async def add_filter_api():
|
|
86 |
alert = request.form.get('alert')
|
87 |
if not chat_id or not text or not reply_text:
|
88 |
return jsonify({"error": "Chat ID, text, and reply text are required"}), 400
|
89 |
-
|
|
|
90 |
return jsonify({"message": "Filter added successfully"})
|
91 |
|
92 |
@app.route('/api/delete-filter', methods=['POST'])
|
93 |
-
|
94 |
chat_id = request.form.get('chat_id')
|
95 |
text = request.form.get('text')
|
96 |
if not chat_id or not text:
|
97 |
return jsonify({"error": "Chat ID and text are required"}), 400
|
98 |
-
|
|
|
99 |
return jsonify({"message": "Filter deleted successfully"})
|
100 |
|
101 |
@app.route('/api/gfilters')
|
102 |
-
|
103 |
gfilters = 'gfilters'
|
104 |
-
|
|
|
105 |
return jsonify({"filters": filters})
|
106 |
|
107 |
@app.route('/api/add-gfilter', methods=['POST'])
|
108 |
-
|
109 |
gfilters = 'gfilters'
|
110 |
text = request.form.get('text')
|
111 |
reply_text = request.form.get('reply_text')
|
@@ -114,46 +119,49 @@ async def add_gfilter_api():
|
|
114 |
alert = request.form.get('alert')
|
115 |
if not text or not reply_text:
|
116 |
return jsonify({"error": "Text and reply text are required"}), 400
|
117 |
-
|
|
|
118 |
return jsonify({"message": "Global filter added successfully"})
|
119 |
|
120 |
@app.route('/api/delete-gfilter', methods=['POST'])
|
121 |
-
|
122 |
gfilters = 'gfilters'
|
123 |
text = request.form.get('text')
|
124 |
if not text:
|
125 |
return jsonify({"error": "Text is required"}), 400
|
126 |
-
|
|
|
127 |
return jsonify({"message": "Global filter deleted successfully"})
|
128 |
|
129 |
@app.route('/api/users')
|
130 |
-
|
131 |
-
|
|
|
132 |
user_list = []
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
})
|
139 |
return jsonify({"users": user_list})
|
140 |
|
141 |
@app.route('/api/chats')
|
142 |
-
|
143 |
-
|
|
|
144 |
chat_list = []
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
})
|
152 |
return jsonify({"chats": chat_list})
|
153 |
|
154 |
@app.route('/api/files')
|
155 |
-
|
156 |
-
|
|
|
157 |
file_list = []
|
158 |
for file in files:
|
159 |
file_list.append({
|
@@ -167,7 +175,7 @@ async def get_files_api():
|
|
167 |
return jsonify({"files": file_list})
|
168 |
|
169 |
@app.route('/api/add-file', methods=['POST'])
|
170 |
-
|
171 |
file_id = request.form.get('file_id')
|
172 |
file_ref = request.form.get('file_ref')
|
173 |
file_name = request.form.get('file_name')
|
@@ -187,77 +195,85 @@ async def add_file_api():
|
|
187 |
caption=caption
|
188 |
)
|
189 |
try:
|
190 |
-
|
|
|
191 |
except Exception as e:
|
192 |
return jsonify({"error": str(e)}), 500
|
193 |
return jsonify({"message": "File added successfully"})
|
194 |
|
195 |
@app.route('/api/delete-file', methods=['POST'])
|
196 |
-
|
197 |
file_id = request.form.get('file_id')
|
198 |
if not file_id:
|
199 |
return jsonify({"error": "File ID is required"}), 400
|
200 |
-
|
|
|
201 |
if result.deleted_count:
|
202 |
return jsonify({"message": "File deleted successfully"})
|
203 |
else:
|
204 |
return jsonify({"error": "File not found"}), 404
|
205 |
|
206 |
@app.route('/api/settings/<chat_id>')
|
207 |
-
|
208 |
-
|
|
|
209 |
return jsonify({"settings": settings})
|
210 |
|
211 |
@app.route('/api/save-settings', methods=['POST'])
|
212 |
-
|
213 |
chat_id = request.form.get('chat_id')
|
214 |
setting_key = request.form.get('setting_key')
|
215 |
setting_value = request.form.get('setting_value')
|
216 |
if not chat_id or not setting_key or not setting_value:
|
217 |
return jsonify({"error": "Chat ID, setting key, and setting value are required"}), 400
|
218 |
-
|
|
|
219 |
return jsonify({"message": "Settings saved successfully"})
|
220 |
|
221 |
@app.route('/api/ban-user', methods=['POST'])
|
222 |
-
|
223 |
user_id = request.form.get('user_id')
|
224 |
ban_reason = request.form.get('ban_reason', "No Reason")
|
225 |
if not user_id:
|
226 |
return jsonify({"error": "User ID is required"}), 400
|
227 |
-
|
|
|
228 |
temp.BANNED_USERS.append(int(user_id))
|
229 |
return jsonify({"message": "User banned successfully"})
|
230 |
|
231 |
@app.route('/api/unban-user', methods=['POST'])
|
232 |
-
|
233 |
user_id = request.form.get('user_id')
|
234 |
if not user_id:
|
235 |
return jsonify({"error": "User ID is required"}), 400
|
236 |
-
|
|
|
237 |
temp.BANNED_USERS.remove(int(user_id))
|
238 |
return jsonify({"message": "User unbanned successfully"})
|
239 |
|
240 |
@app.route('/api/disable-chat', methods=['POST'])
|
241 |
-
|
242 |
chat_id = request.form.get('chat_id')
|
243 |
reason = request.form.get('reason', "No Reason")
|
244 |
if not chat_id:
|
245 |
return jsonify({"error": "Chat ID is required"}), 400
|
246 |
-
|
|
|
247 |
temp.BANNED_CHATS.append(int(chat_id))
|
248 |
return jsonify({"message": "Chat disabled successfully"})
|
249 |
|
250 |
@app.route('/api/enable-chat', methods=['POST'])
|
251 |
-
|
252 |
chat_id = request.form.get('chat_id')
|
253 |
if not chat_id:
|
254 |
return jsonify({"error": "Chat ID is required"}), 400
|
255 |
-
|
|
|
256 |
temp.BANNED_CHATS.remove(int(chat_id))
|
257 |
return jsonify({"message": "Chat enabled successfully"})
|
258 |
|
259 |
@app.route('/api/upload-file', methods=['POST'])
|
260 |
-
|
261 |
if 'fileUpload' not in request.files:
|
262 |
return jsonify({"error": "No file part"}), 400
|
263 |
file = request.files['fileUpload']
|
@@ -266,14 +282,16 @@ async def upload_file_api():
|
|
266 |
if file:
|
267 |
file_path = os.path.join("/app/uploads", file.filename)
|
268 |
file.save(file_path)
|
269 |
-
|
|
|
270 |
os.remove(file_path)
|
271 |
return jsonify({"file_id": file_id, "file_ref": file_ref, "message": "File uploaded successfully"})
|
272 |
return jsonify({"error": "Failed to upload file"}), 500
|
273 |
|
274 |
@app.route('/api/download-file/<file_id>')
|
275 |
-
|
276 |
-
|
|
|
277 |
if not file_details:
|
278 |
return jsonify({"error": "File not found"}), 404
|
279 |
file = file_details[0]
|
@@ -286,12 +304,13 @@ async def download_file_api(file_id):
|
|
286 |
})
|
287 |
|
288 |
@app.route('/api/search-files', methods=['GET'])
|
289 |
-
|
290 |
query = request.args.get('query')
|
291 |
file_type = request.args.get('file_type')
|
292 |
if not query:
|
293 |
return jsonify({"error": "Query is required"}), 400
|
294 |
-
|
|
|
295 |
file_list = []
|
296 |
for file in files:
|
297 |
file_list.append({
|
@@ -305,28 +324,22 @@ async def search_files_api():
|
|
305 |
return jsonify({"files": file_list, "next_offset": next_offset, "total": total})
|
306 |
|
307 |
@app.route('/api/broadcast', methods=['POST'])
|
308 |
-
|
309 |
message_text = request.form.get('message_text')
|
310 |
if not message_text:
|
311 |
return jsonify({"error": "Message text is required"}), 400
|
312 |
-
|
313 |
-
|
|
|
314 |
done = 0
|
315 |
blocked = 0
|
316 |
deleted = 0
|
317 |
failed = 0
|
318 |
success = 0
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
except UserIsBlocked:
|
324 |
-
blocked += 1
|
325 |
-
except InputUserDeactivated:
|
326 |
-
deleted += 1
|
327 |
-
except Exception as e:
|
328 |
-
failed += 1
|
329 |
-
done += 1
|
330 |
return jsonify({
|
331 |
"total_users": total_users,
|
332 |
"completed": done,
|
@@ -336,37 +349,48 @@ async def broadcast_api():
|
|
336 |
"failed": failed
|
337 |
})
|
338 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
339 |
@app.route('/api/ban-users')
|
340 |
-
|
341 |
-
|
|
|
342 |
banned_users = []
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
"ban_reason": user['ban_status']['ban_reason']
|
349 |
-
})
|
350 |
return jsonify({"banned_users": banned_users})
|
351 |
|
352 |
@app.route('/api/banned-chats')
|
353 |
-
|
354 |
-
|
|
|
355 |
banned_chats = []
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
"reason": chat['chat_status']['reason']
|
363 |
-
})
|
364 |
return jsonify({"banned_chats": banned_chats})
|
365 |
|
366 |
@app.route('/api/user-info/<user_id>')
|
367 |
-
|
368 |
try:
|
369 |
-
|
|
|
370 |
return jsonify({
|
371 |
"first_name": user.first_name,
|
372 |
"last_name": user.last_name,
|
@@ -378,57 +402,62 @@ async def user_info_api(user_id):
|
|
378 |
return jsonify({"error": str(e)}), 500
|
379 |
|
380 |
@app.route('/api/chat-info/<chat_id>')
|
381 |
-
|
382 |
try:
|
383 |
-
|
|
|
384 |
return jsonify({
|
385 |
"title": chat.title,
|
386 |
"username": chat.username,
|
387 |
"id": chat.id,
|
388 |
-
"members_count":
|
389 |
})
|
390 |
except Exception as e:
|
391 |
return jsonify({"error": str(e)}), 500
|
392 |
|
393 |
@app.route('/api/restart-bot', methods=['POST'])
|
394 |
-
|
395 |
admin_id = request.form.get('admin_id')
|
396 |
if not admin_id or int(admin_id) not in ADMINS:
|
397 |
return jsonify({"error": "Unauthorized access"}), 401
|
398 |
-
|
399 |
-
|
|
|
400 |
return jsonify({"message": "Bot restarted successfully"})
|
401 |
|
402 |
@app.route('/api/add-connection', methods=['POST'])
|
403 |
-
|
404 |
group_id = request.form.get('group_id')
|
405 |
user_id = request.form.get('user_id')
|
406 |
if not group_id or not user_id:
|
407 |
return jsonify({"error": "Group ID and User ID are required"}), 400
|
408 |
-
|
|
|
409 |
if result:
|
410 |
return jsonify({"message": "Connection added successfully"})
|
411 |
else:
|
412 |
return jsonify({"error": "Connection already exists"}), 400
|
413 |
|
414 |
@app.route('/api/delete-connection', methods=['POST'])
|
415 |
-
|
416 |
group_id = request.form.get('group_id')
|
417 |
user_id = request.form.get('user_id')
|
418 |
if not group_id or not user_id:
|
419 |
return jsonify({"error": "Group ID and User ID are required"}), 400
|
420 |
-
|
|
|
421 |
if result:
|
422 |
return jsonify({"message": "Connection deleted successfully"})
|
423 |
else:
|
424 |
return jsonify({"error": "Connection not found"}), 404
|
425 |
|
426 |
@app.route('/api/group-stats')
|
427 |
-
|
428 |
chat_id = request.args.get('chat_id')
|
429 |
if not chat_id:
|
430 |
return jsonify({"error": "Chat ID is required"}), 400
|
431 |
-
|
|
|
432 |
if not chat:
|
433 |
return jsonify({"error": "Chat not found"}), 404
|
434 |
return jsonify({
|
@@ -453,19 +482,21 @@ def bot_status():
|
|
453 |
})
|
454 |
|
455 |
@app.route('/api/connections')
|
456 |
-
|
457 |
user_id = request.args.get('user_id')
|
458 |
if not user_id:
|
459 |
return jsonify({"error": "User ID is required"}), 400
|
460 |
-
|
|
|
461 |
if groupids is None:
|
462 |
return jsonify({"connections": []})
|
463 |
connections = []
|
464 |
for groupid in groupids:
|
465 |
try:
|
466 |
-
|
|
|
467 |
title = ttl.title
|
468 |
-
active =
|
469 |
connections.append({
|
470 |
"group_id": groupid,
|
471 |
"title": title,
|
@@ -476,5 +507,4 @@ async def connections_api():
|
|
476 |
return jsonify({"connections": connections})
|
477 |
|
478 |
if __name__ == '__main__':
|
479 |
-
bot.run()
|
480 |
app.run(host='0.0.0.0', port=7860, debug=False)
|
|
|
1 |
+
# app.py
|
2 |
from flask import Flask, render_template, jsonify, request
|
3 |
from pyrogram import Client
|
4 |
import os
|
|
|
52 |
})
|
53 |
|
54 |
@app.route('/api/bot-stats')
|
55 |
+
def bot_stats():
|
56 |
+
loop = asyncio.get_event_loop()
|
57 |
+
total_files = loop.run_until_complete(Media.count_documents())
|
58 |
+
total_users = loop.run_until_complete(db.total_users_count())
|
59 |
+
total_chats = loop.run_until_complete(db.total_chat_count())
|
60 |
+
monsize = loop.run_until_complete(db.get_db_size())
|
61 |
free = 536870912 - monsize
|
62 |
monsize = get_size(monsize)
|
63 |
free = get_size(free)
|
|
|
70 |
})
|
71 |
|
72 |
@app.route('/api/filters')
|
73 |
+
def get_filters_api():
|
74 |
chat_id = request.args.get('chat_id')
|
75 |
if not chat_id:
|
76 |
return jsonify({"error": "Chat ID is required"}), 400
|
77 |
+
loop = asyncio.get_event_loop()
|
78 |
+
filters = loop.run_until_complete(get_filters(chat_id))
|
79 |
return jsonify({"filters": filters})
|
80 |
|
81 |
@app.route('/api/add-filter', methods=['POST'])
|
82 |
+
def add_filter_api():
|
83 |
chat_id = request.form.get('chat_id')
|
84 |
text = request.form.get('text')
|
85 |
reply_text = request.form.get('reply_text')
|
|
|
88 |
alert = request.form.get('alert')
|
89 |
if not chat_id or not text or not reply_text:
|
90 |
return jsonify({"error": "Chat ID, text, and reply text are required"}), 400
|
91 |
+
loop = asyncio.get_event_loop()
|
92 |
+
loop.run_until_complete(add_filter(chat_id, text, reply_text, btn, file, alert))
|
93 |
return jsonify({"message": "Filter added successfully"})
|
94 |
|
95 |
@app.route('/api/delete-filter', methods=['POST'])
|
96 |
+
def delete_filter_api():
|
97 |
chat_id = request.form.get('chat_id')
|
98 |
text = request.form.get('text')
|
99 |
if not chat_id or not text:
|
100 |
return jsonify({"error": "Chat ID and text are required"}), 400
|
101 |
+
loop = asyncio.get_event_loop()
|
102 |
+
loop.run_until_complete(delete_filter(request, text, chat_id))
|
103 |
return jsonify({"message": "Filter deleted successfully"})
|
104 |
|
105 |
@app.route('/api/gfilters')
|
106 |
+
def get_gfilters_api():
|
107 |
gfilters = 'gfilters'
|
108 |
+
loop = asyncio.get_event_loop()
|
109 |
+
filters = loop.run_until_complete(get_gfilters(gfilters))
|
110 |
return jsonify({"filters": filters})
|
111 |
|
112 |
@app.route('/api/add-gfilter', methods=['POST'])
|
113 |
+
def add_gfilter_api():
|
114 |
gfilters = 'gfilters'
|
115 |
text = request.form.get('text')
|
116 |
reply_text = request.form.get('reply_text')
|
|
|
119 |
alert = request.form.get('alert')
|
120 |
if not text or not reply_text:
|
121 |
return jsonify({"error": "Text and reply text are required"}), 400
|
122 |
+
loop = asyncio.get_event_loop()
|
123 |
+
loop.run_until_complete(add_gfilter(gfilters, text, reply_text, btn, file, alert))
|
124 |
return jsonify({"message": "Global filter added successfully"})
|
125 |
|
126 |
@app.route('/api/delete-gfilter', methods=['POST'])
|
127 |
+
def delete_gfilter_api():
|
128 |
gfilters = 'gfilters'
|
129 |
text = request.form.get('text')
|
130 |
if not text:
|
131 |
return jsonify({"error": "Text is required"}), 400
|
132 |
+
loop = asyncio.get_event_loop()
|
133 |
+
loop.run_until_complete(delete_gfilter(request, text, gfilters))
|
134 |
return jsonify({"message": "Global filter deleted successfully"})
|
135 |
|
136 |
@app.route('/api/users')
|
137 |
+
def get_users_api():
|
138 |
+
loop = asyncio.get_event_loop()
|
139 |
+
users = loop.run_until_complete(db.get_all_users())
|
140 |
user_list = []
|
141 |
+
loop.run_until_complete(asyncio.gather(*(user_list.append({
|
142 |
+
"id": user['id'],
|
143 |
+
"name": user['name'],
|
144 |
+
"ban_status": user['ban_status']
|
145 |
+
}) for user in users)))
|
|
|
146 |
return jsonify({"users": user_list})
|
147 |
|
148 |
@app.route('/api/chats')
|
149 |
+
def get_chats_api():
|
150 |
+
loop = asyncio.get_event_loop()
|
151 |
+
chats = loop.run_until_complete(db.get_all_chats())
|
152 |
chat_list = []
|
153 |
+
loop.run_until_complete(asyncio.gather(*(chat_list.append({
|
154 |
+
"id": chat['id'],
|
155 |
+
"title": chat['title'],
|
156 |
+
"username": chat['username'],
|
157 |
+
"chat_status": chat['chat_status']
|
158 |
+
}) for chat in chats)))
|
|
|
159 |
return jsonify({"chats": chat_list})
|
160 |
|
161 |
@app.route('/api/files')
|
162 |
+
def get_files_api():
|
163 |
+
loop = asyncio.get_event_loop()
|
164 |
+
files = loop.run_until_complete(Media.find().to_list(None))
|
165 |
file_list = []
|
166 |
for file in files:
|
167 |
file_list.append({
|
|
|
175 |
return jsonify({"files": file_list})
|
176 |
|
177 |
@app.route('/api/add-file', methods=['POST'])
|
178 |
+
def add_file_api():
|
179 |
file_id = request.form.get('file_id')
|
180 |
file_ref = request.form.get('file_ref')
|
181 |
file_name = request.form.get('file_name')
|
|
|
195 |
caption=caption
|
196 |
)
|
197 |
try:
|
198 |
+
loop = asyncio.get_event_loop()
|
199 |
+
loop.run_until_complete(file.commit())
|
200 |
except Exception as e:
|
201 |
return jsonify({"error": str(e)}), 500
|
202 |
return jsonify({"message": "File added successfully"})
|
203 |
|
204 |
@app.route('/api/delete-file', methods=['POST'])
|
205 |
+
def delete_file_api():
|
206 |
file_id = request.form.get('file_id')
|
207 |
if not file_id:
|
208 |
return jsonify({"error": "File ID is required"}), 400
|
209 |
+
loop = asyncio.get_event_loop()
|
210 |
+
result = loop.run_until_complete(Media.collection.delete_one({'_id': file_id}))
|
211 |
if result.deleted_count:
|
212 |
return jsonify({"message": "File deleted successfully"})
|
213 |
else:
|
214 |
return jsonify({"error": "File not found"}), 404
|
215 |
|
216 |
@app.route('/api/settings/<chat_id>')
|
217 |
+
def get_settings_api(chat_id):
|
218 |
+
loop = asyncio.get_event_loop()
|
219 |
+
settings = loop.run_until_complete(get_settings(chat_id))
|
220 |
return jsonify({"settings": settings})
|
221 |
|
222 |
@app.route('/api/save-settings', methods=['POST'])
|
223 |
+
def save_settings_api():
|
224 |
chat_id = request.form.get('chat_id')
|
225 |
setting_key = request.form.get('setting_key')
|
226 |
setting_value = request.form.get('setting_value')
|
227 |
if not chat_id or not setting_key or not setting_value:
|
228 |
return jsonify({"error": "Chat ID, setting key, and setting value are required"}), 400
|
229 |
+
loop = asyncio.get_event_loop()
|
230 |
+
loop.run_until_complete(save_group_settings(chat_id, setting_key, setting_value))
|
231 |
return jsonify({"message": "Settings saved successfully"})
|
232 |
|
233 |
@app.route('/api/ban-user', methods=['POST'])
|
234 |
+
def ban_user_api():
|
235 |
user_id = request.form.get('user_id')
|
236 |
ban_reason = request.form.get('ban_reason', "No Reason")
|
237 |
if not user_id:
|
238 |
return jsonify({"error": "User ID is required"}), 400
|
239 |
+
loop = asyncio.get_event_loop()
|
240 |
+
loop.run_until_complete(db.ban_user(int(user_id), ban_reason))
|
241 |
temp.BANNED_USERS.append(int(user_id))
|
242 |
return jsonify({"message": "User banned successfully"})
|
243 |
|
244 |
@app.route('/api/unban-user', methods=['POST'])
|
245 |
+
def unban_user_api():
|
246 |
user_id = request.form.get('user_id')
|
247 |
if not user_id:
|
248 |
return jsonify({"error": "User ID is required"}), 400
|
249 |
+
loop = asyncio.get_event_loop()
|
250 |
+
loop.run_until_complete(db.remove_ban(int(user_id)))
|
251 |
temp.BANNED_USERS.remove(int(user_id))
|
252 |
return jsonify({"message": "User unbanned successfully"})
|
253 |
|
254 |
@app.route('/api/disable-chat', methods=['POST'])
|
255 |
+
def disable_chat_api():
|
256 |
chat_id = request.form.get('chat_id')
|
257 |
reason = request.form.get('reason', "No Reason")
|
258 |
if not chat_id:
|
259 |
return jsonify({"error": "Chat ID is required"}), 400
|
260 |
+
loop = asyncio.get_event_loop()
|
261 |
+
loop.run_until_complete(db.disable_chat(int(chat_id), reason))
|
262 |
temp.BANNED_CHATS.append(int(chat_id))
|
263 |
return jsonify({"message": "Chat disabled successfully"})
|
264 |
|
265 |
@app.route('/api/enable-chat', methods=['POST'])
|
266 |
+
def enable_chat_api():
|
267 |
chat_id = request.form.get('chat_id')
|
268 |
if not chat_id:
|
269 |
return jsonify({"error": "Chat ID is required"}), 400
|
270 |
+
loop = asyncio.get_event_loop()
|
271 |
+
loop.run_until_complete(db.re_enable_chat(int(chat_id)))
|
272 |
temp.BANNED_CHATS.remove(int(chat_id))
|
273 |
return jsonify({"message": "Chat enabled successfully"})
|
274 |
|
275 |
@app.route('/api/upload-file', methods=['POST'])
|
276 |
+
def upload_file_api():
|
277 |
if 'fileUpload' not in request.files:
|
278 |
return jsonify({"error": "No file part"}), 400
|
279 |
file = request.files['fileUpload']
|
|
|
282 |
if file:
|
283 |
file_path = os.path.join("/app/uploads", file.filename)
|
284 |
file.save(file_path)
|
285 |
+
loop = asyncio.get_event_loop()
|
286 |
+
file_id, file_ref = loop.run_until_complete(save_file(file_path))
|
287 |
os.remove(file_path)
|
288 |
return jsonify({"file_id": file_id, "file_ref": file_ref, "message": "File uploaded successfully"})
|
289 |
return jsonify({"error": "Failed to upload file"}), 500
|
290 |
|
291 |
@app.route('/api/download-file/<file_id>')
|
292 |
+
def download_file_api(file_id):
|
293 |
+
loop = asyncio.get_event_loop()
|
294 |
+
file_details = loop.run_until_complete(get_file_details(file_id))
|
295 |
if not file_details:
|
296 |
return jsonify({"error": "File not found"}), 404
|
297 |
file = file_details[0]
|
|
|
304 |
})
|
305 |
|
306 |
@app.route('/api/search-files', methods=['GET'])
|
307 |
+
def search_files_api():
|
308 |
query = request.args.get('query')
|
309 |
file_type = request.args.get('file_type')
|
310 |
if not query:
|
311 |
return jsonify({"error": "Query is required"}), 400
|
312 |
+
loop = asyncio.get_event_loop()
|
313 |
+
files, next_offset, total = loop.run_until_complete(get_search_results(query, file_type=file_type))
|
314 |
file_list = []
|
315 |
for file in files:
|
316 |
file_list.append({
|
|
|
324 |
return jsonify({"files": file_list, "next_offset": next_offset, "total": total})
|
325 |
|
326 |
@app.route('/api/broadcast', methods=['POST'])
|
327 |
+
def broadcast_api():
|
328 |
message_text = request.form.get('message_text')
|
329 |
if not message_text:
|
330 |
return jsonify({"error": "Message text is required"}), 400
|
331 |
+
loop = asyncio.get_event_loop()
|
332 |
+
users = loop.run_until_complete(db.get_all_users())
|
333 |
+
total_users = loop.run_until_complete(db.total_users_count())
|
334 |
done = 0
|
335 |
blocked = 0
|
336 |
deleted = 0
|
337 |
failed = 0
|
338 |
success = 0
|
339 |
+
loop.run_until_complete(asyncio.gather(*[
|
340 |
+
asyncio.create_task(handle_user(user, message_text, success, blocked, deleted, failed, done))
|
341 |
+
for user in users
|
342 |
+
]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
343 |
return jsonify({
|
344 |
"total_users": total_users,
|
345 |
"completed": done,
|
|
|
349 |
"failed": failed
|
350 |
})
|
351 |
|
352 |
+
async def handle_user(user, message_text, success, blocked, deleted, failed, done):
|
353 |
+
try:
|
354 |
+
await bot.send_message(user['id'], message_text)
|
355 |
+
success += 1
|
356 |
+
except UserIsBlocked:
|
357 |
+
blocked += 1
|
358 |
+
except InputUserDeactivated:
|
359 |
+
deleted += 1
|
360 |
+
except Exception as e:
|
361 |
+
failed += 1
|
362 |
+
done += 1
|
363 |
+
|
364 |
@app.route('/api/ban-users')
|
365 |
+
def ban_users_api():
|
366 |
+
loop = asyncio.get_event_loop()
|
367 |
+
users = loop.run_until_complete(db.get_all_users())
|
368 |
banned_users = []
|
369 |
+
loop.run_until_complete(asyncio.gather(*(banned_users.append({
|
370 |
+
"id": user['id'],
|
371 |
+
"name": user['name'],
|
372 |
+
"ban_reason": user['ban_status']['ban_reason']
|
373 |
+
}) for user in users if user['ban_status']['is_banned'])))
|
|
|
|
|
374 |
return jsonify({"banned_users": banned_users})
|
375 |
|
376 |
@app.route('/api/banned-chats')
|
377 |
+
def banned_chats_api():
|
378 |
+
loop = asyncio.get_event_loop()
|
379 |
+
chats = loop.run_until_complete(db.get_all_chats())
|
380 |
banned_chats = []
|
381 |
+
loop.run_until_complete(asyncio.gather(*(banned_chats.append({
|
382 |
+
"id": chat['id'],
|
383 |
+
"title": chat['title'],
|
384 |
+
"username": chat['username'],
|
385 |
+
"reason": chat['chat_status']['reason']
|
386 |
+
}) for chat in chats if chat['chat_status']['is_disabled'])))
|
|
|
|
|
387 |
return jsonify({"banned_chats": banned_chats})
|
388 |
|
389 |
@app.route('/api/user-info/<user_id>')
|
390 |
+
def user_info_api(user_id):
|
391 |
try:
|
392 |
+
loop = asyncio.get_event_loop()
|
393 |
+
user = loop.run_until_complete(bot.get_users(int(user_id)))
|
394 |
return jsonify({
|
395 |
"first_name": user.first_name,
|
396 |
"last_name": user.last_name,
|
|
|
402 |
return jsonify({"error": str(e)}), 500
|
403 |
|
404 |
@app.route('/api/chat-info/<chat_id>')
|
405 |
+
def chat_info_api(chat_id):
|
406 |
try:
|
407 |
+
loop = asyncio.get_event_loop()
|
408 |
+
chat = loop.run_until_complete(bot.get_chat(int(chat_id)))
|
409 |
return jsonify({
|
410 |
"title": chat.title,
|
411 |
"username": chat.username,
|
412 |
"id": chat.id,
|
413 |
+
"members_count": loop.run_until_complete(bot.get_chat_members_count(int(chat_id)))
|
414 |
})
|
415 |
except Exception as e:
|
416 |
return jsonify({"error": str(e)}), 500
|
417 |
|
418 |
@app.route('/api/restart-bot', methods=['POST'])
|
419 |
+
def restart_bot_api():
|
420 |
admin_id = request.form.get('admin_id')
|
421 |
if not admin_id or int(admin_id) not in ADMINS:
|
422 |
return jsonify({"error": "Unauthorized access"}), 401
|
423 |
+
loop = asyncio.get_event_loop()
|
424 |
+
loop.run_until_complete(bot.stop())
|
425 |
+
loop.run_until_complete(bot.start())
|
426 |
return jsonify({"message": "Bot restarted successfully"})
|
427 |
|
428 |
@app.route('/api/add-connection', methods=['POST'])
|
429 |
+
def add_connection_api():
|
430 |
group_id = request.form.get('group_id')
|
431 |
user_id = request.form.get('user_id')
|
432 |
if not group_id or not user_id:
|
433 |
return jsonify({"error": "Group ID and User ID are required"}), 400
|
434 |
+
loop = asyncio.get_event_loop()
|
435 |
+
result = loop.run_until_complete(add_connection(group_id, user_id))
|
436 |
if result:
|
437 |
return jsonify({"message": "Connection added successfully"})
|
438 |
else:
|
439 |
return jsonify({"error": "Connection already exists"}), 400
|
440 |
|
441 |
@app.route('/api/delete-connection', methods=['POST'])
|
442 |
+
def delete_connection_api():
|
443 |
group_id = request.form.get('group_id')
|
444 |
user_id = request.form.get('user_id')
|
445 |
if not group_id or not user_id:
|
446 |
return jsonify({"error": "Group ID and User ID are required"}), 400
|
447 |
+
loop = asyncio.get_event_loop()
|
448 |
+
result = loop.run_until_complete(delete_connection(user_id, group_id))
|
449 |
if result:
|
450 |
return jsonify({"message": "Connection deleted successfully"})
|
451 |
else:
|
452 |
return jsonify({"error": "Connection not found"}), 404
|
453 |
|
454 |
@app.route('/api/group-stats')
|
455 |
+
def group_stats_api():
|
456 |
chat_id = request.args.get('chat_id')
|
457 |
if not chat_id:
|
458 |
return jsonify({"error": "Chat ID is required"}), 400
|
459 |
+
loop = asyncio.get_event_loop()
|
460 |
+
chat = loop.run_until_complete(db.get_chat(chat_id))
|
461 |
if not chat:
|
462 |
return jsonify({"error": "Chat not found"}), 404
|
463 |
return jsonify({
|
|
|
482 |
})
|
483 |
|
484 |
@app.route('/api/connections')
|
485 |
+
def connections_api():
|
486 |
user_id = request.args.get('user_id')
|
487 |
if not user_id:
|
488 |
return jsonify({"error": "User ID is required"}), 400
|
489 |
+
loop = asyncio.get_event_loop()
|
490 |
+
groupids = loop.run_until_complete(all_connections(str(user_id)))
|
491 |
if groupids is None:
|
492 |
return jsonify({"connections": []})
|
493 |
connections = []
|
494 |
for groupid in groupids:
|
495 |
try:
|
496 |
+
loop = asyncio.get_event_loop()
|
497 |
+
ttl = loop.run_until_complete(bot.get_chat(int(groupid)))
|
498 |
title = ttl.title
|
499 |
+
active = loop.run_until_complete(if_active(str(user_id), str(groupid)))
|
500 |
connections.append({
|
501 |
"group_id": groupid,
|
502 |
"title": title,
|
|
|
507 |
return jsonify({"connections": connections})
|
508 |
|
509 |
if __name__ == '__main__':
|
|
|
510 |
app.run(host='0.0.0.0', port=7860, debug=False)
|