ulduldp commited on
Commit
bedd8b1
Β·
verified Β·
1 Parent(s): 1b2beaf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -33
app.py CHANGED
@@ -1,9 +1,3 @@
1
-
2
- BOT_TOKEN = "6740023413:AAHZ5iEiXjkY7IGwljg37llFbekKLbIVkkw"
3
-
4
- # πŸ”΄ HARD CODED VALUES
5
- GROUP_ID = -1003799294466
6
-
7
  import json
8
  import os
9
  import time
@@ -23,25 +17,37 @@ from telegram.ext import (
23
  filters
24
  )
25
 
 
 
26
 
27
  waiting_for_file = set()
28
 
29
 
30
- # πŸ“ JSON path
31
  def get_json_file(user_id):
32
  return f"{user_id}.json"
33
 
34
 
35
- # πŸ“₯ LOAD DATA
36
  def load_data(user_id):
37
  file = get_json_file(user_id)
 
38
  if os.path.exists(file):
39
- with open(file, "r") as f:
40
- return json.load(f)
 
 
 
 
 
 
 
 
 
41
  return {"files": []}
42
 
43
 
44
- # πŸ’Ύ SAVE DATA
45
  def save_data(user_id, data):
46
  with open(get_json_file(user_id), "w") as f:
47
  json.dump(data, f, indent=4)
@@ -96,7 +102,7 @@ async def save_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
96
  await update.message.reply_text("πŸ“ Send file to save")
97
 
98
 
99
- # πŸ“€ FILE HANDLER
100
  async def handle_file(update: Update, context: ContextTypes.DEFAULT_TYPE):
101
  user = update.effective_user
102
  user_id = user.id
@@ -118,14 +124,14 @@ async def handle_file(update: Update, context: ContextTypes.DEFAULT_TYPE):
118
 
119
  new_name = f"{user_id}_{timestamp}.{ext}"
120
 
121
- # 1️⃣ send file FIRST
122
  sent = await context.bot.send_document(
123
  chat_id=GROUP_ID,
124
  document=document.file_id,
125
  filename=new_name
126
  )
127
 
128
- # 2️⃣ JSON update
129
  data = load_data(user_id)
130
 
131
  entry = {
@@ -140,14 +146,7 @@ async def handle_file(update: Update, context: ContextTypes.DEFAULT_TYPE):
140
  data["files"].append(entry)
141
  save_data(user_id, data)
142
 
143
- # 3️⃣ send JSON
144
- """await context.bot.send_document(
145
- chat_id=GROUP_ID,
146
- document=InputFile(get_json_file(user_id)),
147
- filename=f"{user_id}.json",
148
- caption=f"πŸ“ Index of {user_id}"
149
- )"""
150
-
151
  await context.bot.send_document(
152
  chat_id=GROUP_ID,
153
  document=InputFile(
@@ -157,11 +156,11 @@ async def handle_file(update: Update, context: ContextTypes.DEFAULT_TYPE):
157
  caption=f"πŸ“ Index of {user_id}"
158
  )
159
 
160
- await update.message.reply_text("βœ… File saved!")
161
 
162
  except Exception as e:
163
  print("ERROR:", e)
164
- await update.message.reply_text(f"❌ {e}")
165
 
166
  waiting_for_file.discard(user_id)
167
 
@@ -172,7 +171,7 @@ async def myfiles(update: Update, context: ContextTypes.DEFAULT_TYPE):
172
  data = load_data(user_id)
173
 
174
  if not data["files"]:
175
- await update.message.reply_text("❌ No files")
176
  return
177
 
178
  msg = "πŸ“‚ Your Files:\n\n"
@@ -195,10 +194,13 @@ async def get_file(update: Update, context: ContextTypes.DEFAULT_TYPE):
195
 
196
  for f in data["files"]:
197
  if f["file_name"] == filename:
198
- await context.bot.send_document(update.effective_chat.id, f["file_id"])
 
 
 
199
  return
200
 
201
- await update.message.reply_text("❌ Not found")
202
 
203
 
204
  # πŸ—‘οΈ /delete
@@ -212,16 +214,16 @@ async def delete_file(update: Update, context: ContextTypes.DEFAULT_TYPE):
212
 
213
  filename = context.args[0]
214
 
215
- new_data = [f for f in data["files"] if f["file_name"] != filename]
216
 
217
- if len(new_data) == len(data["files"]):
218
  await update.message.reply_text("❌ File not found")
219
  return
220
 
221
- data["files"] = new_data
222
  save_data(user_id, data)
223
 
224
- await update.message.reply_text("πŸ—‘οΈ Deleted")
225
 
226
 
227
  # πŸ”Ž /search
@@ -261,7 +263,7 @@ async def stats(update: Update, context: ContextTypes.DEFAULT_TYPE):
261
  )
262
 
263
 
264
- # πŸš€ RUN
265
  app = ApplicationBuilder().token(BOT_TOKEN).build()
266
 
267
  app.add_handler(CommandHandler("start", start))
@@ -275,5 +277,5 @@ app.add_handler(CommandHandler("stats", stats))
275
  app.add_handler(CallbackQueryHandler(buttons))
276
  app.add_handler(MessageHandler(filters.Document.ALL, handle_file))
277
 
278
- print("πŸ”₯ ULTRA BOT RUNNING...")
279
  app.run_polling()
 
 
 
 
 
 
 
1
  import json
2
  import os
3
  import time
 
17
  filters
18
  )
19
 
20
+ BOT_TOKEN = "YOUR_BOT_TOKEN"
21
+ GROUP_ID = -5111627225
22
 
23
  waiting_for_file = set()
24
 
25
 
26
+ # πŸ“ JSON file name = userID.json
27
  def get_json_file(user_id):
28
  return f"{user_id}.json"
29
 
30
 
31
+ # πŸ“₯ LOAD JSON (safe)
32
  def load_data(user_id):
33
  file = get_json_file(user_id)
34
+
35
  if os.path.exists(file):
36
+ try:
37
+ with open(file, "r") as f:
38
+ data = json.load(f)
39
+
40
+ if "files" not in data or not isinstance(data["files"], list):
41
+ return {"files": []}
42
+
43
+ return data
44
+ except:
45
+ return {"files": []}
46
+
47
  return {"files": []}
48
 
49
 
50
+ # πŸ’Ύ SAVE JSON
51
  def save_data(user_id, data):
52
  with open(get_json_file(user_id), "w") as f:
53
  json.dump(data, f, indent=4)
 
102
  await update.message.reply_text("πŸ“ Send file to save")
103
 
104
 
105
+ # πŸ“€ FILE HANDLER (MAIN LOGIC)
106
  async def handle_file(update: Update, context: ContextTypes.DEFAULT_TYPE):
107
  user = update.effective_user
108
  user_id = user.id
 
124
 
125
  new_name = f"{user_id}_{timestamp}.{ext}"
126
 
127
+ # βœ… STEP 1: send file FIRST
128
  sent = await context.bot.send_document(
129
  chat_id=GROUP_ID,
130
  document=document.file_id,
131
  filename=new_name
132
  )
133
 
134
+ # βœ… STEP 2: JSON update
135
  data = load_data(user_id)
136
 
137
  entry = {
 
146
  data["files"].append(entry)
147
  save_data(user_id, data)
148
 
149
+ # βœ… STEP 3: send JSON with correct filename
 
 
 
 
 
 
 
150
  await context.bot.send_document(
151
  chat_id=GROUP_ID,
152
  document=InputFile(
 
156
  caption=f"πŸ“ Index of {user_id}"
157
  )
158
 
159
+ await update.message.reply_text("βœ… File saved successfully!")
160
 
161
  except Exception as e:
162
  print("ERROR:", e)
163
+ await update.message.reply_text(f"❌ Error: {e}")
164
 
165
  waiting_for_file.discard(user_id)
166
 
 
171
  data = load_data(user_id)
172
 
173
  if not data["files"]:
174
+ await update.message.reply_text("❌ No files found")
175
  return
176
 
177
  msg = "πŸ“‚ Your Files:\n\n"
 
194
 
195
  for f in data["files"]:
196
  if f["file_name"] == filename:
197
+ await context.bot.send_document(
198
+ chat_id=update.effective_chat.id,
199
+ document=f["file_id"]
200
+ )
201
  return
202
 
203
+ await update.message.reply_text("❌ File not found")
204
 
205
 
206
  # πŸ—‘οΈ /delete
 
214
 
215
  filename = context.args[0]
216
 
217
+ new_files = [f for f in data["files"] if f["file_name"] != filename]
218
 
219
+ if len(new_files) == len(data["files"]):
220
  await update.message.reply_text("❌ File not found")
221
  return
222
 
223
+ data["files"] = new_files
224
  save_data(user_id, data)
225
 
226
+ await update.message.reply_text("πŸ—‘οΈ File deleted")
227
 
228
 
229
  # πŸ”Ž /search
 
263
  )
264
 
265
 
266
+ # πŸš€ RUN BOT
267
  app = ApplicationBuilder().token(BOT_TOKEN).build()
268
 
269
  app.add_handler(CommandHandler("start", start))
 
277
  app.add_handler(CallbackQueryHandler(buttons))
278
  app.add_handler(MessageHandler(filters.Document.ALL, handle_file))
279
 
280
+ print("πŸ”₯ FINAL BOT RUNNING...")
281
  app.run_polling()