ulduldp commited on
Commit
2c48f22
Β·
verified Β·
1 Parent(s): 3322cfc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -105
app.py CHANGED
@@ -4,81 +4,49 @@ BOT_TOKEN = "6740023413:AAHZ5iEiXjkY7IGwljg37llFbekKLbIVkkw"
4
  # πŸ”΄ HARD CODED VALUES
5
  GROUP_ID = -1003799294466
6
 
7
-
8
  import json
9
  import os
10
  import time
11
- from telegram import Update, InputFile, InlineKeyboardButton, InlineKeyboardMarkup
 
12
  from telegram.ext import (
13
  ApplicationBuilder,
14
  MessageHandler,
15
  CommandHandler,
16
- CallbackQueryHandler,
17
  ContextTypes,
18
  filters
19
  )
20
 
 
 
21
 
22
  waiting_for_file = set()
23
 
24
 
 
25
  def get_json_file(user_id):
26
- return f"{user_id}_savedFiles.json"
27
 
28
 
 
29
  def load_data(user_id):
30
  file = get_json_file(user_id)
31
  if os.path.exists(file):
32
  with open(file, "r") as f:
33
  return json.load(f)
34
- return []
35
 
36
 
 
37
  def save_data(user_id, data):
38
  with open(get_json_file(user_id), "w") as f:
39
  json.dump(data, f, indent=4)
40
 
41
 
42
- # πŸš€ START MENU
43
- async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
44
- keyboard = [
45
- [InlineKeyboardButton("πŸ“€ Upload File", callback_data="upload")],
46
- [InlineKeyboardButton("πŸ“‚ My Files", callback_data="myfiles")],
47
- [InlineKeyboardButton("πŸ“Š Stats", callback_data="stats")]
48
- ]
49
- await update.message.reply_text(
50
- "πŸš€ Welcome to Cloud Bot",
51
- reply_markup=InlineKeyboardMarkup(keyboard)
52
- )
53
-
54
-
55
- # πŸ”˜ BUTTON HANDLER
56
- async def button_handler(update: Update, context: ContextTypes.DEFAULT_TYPE):
57
- query = update.callback_query
58
- await query.answer()
59
-
60
- user_id = query.from_user.id
61
-
62
- if query.data == "upload":
63
- waiting_for_file.add(user_id)
64
- await query.message.reply_text("πŸ“ Send file(s) now...")
65
-
66
- elif query.data == "myfiles":
67
- data = load_data(user_id)
68
-
69
- if not data:
70
- await query.message.reply_text("❌ No files found")
71
- return
72
-
73
- msg = "πŸ“‚ Your Files:\n\n"
74
- for f in data[-10:]:
75
- msg += f"β€’ {f['file_name']}\n"
76
-
77
- await query.message.reply_text(msg)
78
-
79
- elif query.data == "stats":
80
- data = load_data(user_id)
81
- await query.message.reply_text(f"πŸ“Š Total files: {len(data)}")
82
 
83
 
84
  # πŸ“€ FILE HANDLER
@@ -94,107 +62,100 @@ async def handle_file(update: Update, context: ContextTypes.DEFAULT_TYPE):
94
  return
95
 
96
  try:
97
- ext = document.file_name.split('.')[-1] if '.' in document.file_name else "dat"
 
 
 
98
  timestamp = int(time.time())
 
 
99
  new_name = f"{user_id}_{timestamp}.{ext}"
100
 
 
101
  sent = await context.bot.send_document(
102
  chat_id=GROUP_ID,
103
  document=document.file_id,
104
- filename=new_name,
105
- caption=f"πŸ“‚ {user.username or user_id}"
106
  )
107
 
 
108
  data = load_data(user_id)
109
 
110
- data.append({
111
  "file_name": new_name,
 
112
  "file_id": sent.document.file_id,
 
 
113
  "timestamp": timestamp
114
- })
115
-
116
- save_data(user_id, data)
117
 
118
- await update.message.reply_text("βœ… Saved!")
119
 
120
- except Exception as e:
121
- await update.message.reply_text(f"❌ {e}")
122
-
123
-
124
- # πŸ“₯ GET FILE
125
- async def get_file(update: Update, context: ContextTypes.DEFAULT_TYPE):
126
- user_id = update.effective_user.id
127
 
128
- if not context.args:
129
- await update.message.reply_text("Usage: /get filename")
130
- return
 
 
 
 
131
 
132
- filename = context.args[0]
133
- data = load_data(user_id)
134
 
135
- for f in data:
136
- if f["file_name"] == filename:
137
- await context.bot.send_document(update.effective_chat.id, f["file_id"])
138
- return
139
 
140
- await update.message.reply_text("❌ Not found")
141
 
142
 
143
- # πŸ—‘οΈ DELETE FILE
144
- async def delete_file(update: Update, context: ContextTypes.DEFAULT_TYPE):
145
  user_id = update.effective_user.id
146
-
147
- if not context.args:
148
- await update.message.reply_text("Usage: /delete filename")
149
- return
150
-
151
- filename = context.args[0]
152
  data = load_data(user_id)
153
 
154
- new_data = [f for f in data if f["file_name"] != filename]
155
-
156
- if len(new_data) == len(data):
157
- await update.message.reply_text("❌ File not found")
158
  return
159
 
160
- save_data(user_id, new_data)
161
- await update.message.reply_text("πŸ—‘οΈ Deleted")
 
 
 
162
 
163
 
164
- # πŸ”Ž SEARCH
165
- async def search(update: Update, context: ContextTypes.DEFAULT_TYPE):
166
  user_id = update.effective_user.id
 
167
 
168
  if not context.args:
169
- await update.message.reply_text("Usage: /search keyword")
170
  return
171
 
172
- keyword = context.args[0].lower()
173
- data = load_data(user_id)
174
-
175
- results = [f for f in data if keyword in f["file_name"].lower()]
176
-
177
- if not results:
178
- await update.message.reply_text("❌ No results")
179
- return
180
 
181
- msg = "πŸ”Ž Results:\n\n"
182
- for f in results:
183
- msg += f"β€’ {f['file_name']}\n"
 
 
 
 
184
 
185
- await update.message.reply_text(msg)
186
 
187
 
188
  # πŸš€ BOT START
189
  app = ApplicationBuilder().token(BOT_TOKEN).build()
190
 
191
- app.add_handler(CommandHandler("start", start))
 
192
  app.add_handler(CommandHandler("get", get_file))
193
- app.add_handler(CommandHandler("delete", delete_file))
194
- app.add_handler(CommandHandler("search", search))
195
-
196
- app.add_handler(CallbackQueryHandler(button_handler))
197
  app.add_handler(MessageHandler(filters.Document.ALL, handle_file))
198
 
199
- print("πŸ”₯ OP BOT RUNNING...")
200
  app.run_polling()
 
4
  # πŸ”΄ HARD CODED VALUES
5
  GROUP_ID = -1003799294466
6
 
 
7
  import json
8
  import os
9
  import time
10
+ from datetime import datetime
11
+ from telegram import Update, InputFile
12
  from telegram.ext import (
13
  ApplicationBuilder,
14
  MessageHandler,
15
  CommandHandler,
 
16
  ContextTypes,
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 path
27
  def get_json_file(user_id):
28
+ return f"{user_id}.json"
29
 
30
 
31
+ # πŸ“₯ load JSON
32
  def load_data(user_id):
33
  file = get_json_file(user_id)
34
  if os.path.exists(file):
35
  with open(file, "r") as f:
36
  return json.load(f)
37
+ return {"files": []}
38
 
39
 
40
+ # πŸ’Ύ save JSON
41
  def save_data(user_id, data):
42
  with open(get_json_file(user_id), "w") as f:
43
  json.dump(data, f, indent=4)
44
 
45
 
46
+ # 🟒 /save command
47
+ async def save_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
48
+ waiting_for_file.add(update.effective_user.id)
49
+ await update.message.reply_text("πŸ“ Send file to save")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
 
52
  # πŸ“€ FILE HANDLER
 
62
  return
63
 
64
  try:
65
+ # file info
66
+ original_name = document.file_name
67
+ ext = original_name.split('.')[-1] if '.' in original_name else "dat"
68
+
69
  timestamp = int(time.time())
70
+ upload_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
71
+
72
  new_name = f"{user_id}_{timestamp}.{ext}"
73
 
74
+ # 1️⃣ send file FIRST
75
  sent = await context.bot.send_document(
76
  chat_id=GROUP_ID,
77
  document=document.file_id,
78
+ filename=new_name
 
79
  )
80
 
81
+ # 2️⃣ JSON load/update
82
  data = load_data(user_id)
83
 
84
+ file_entry = {
85
  "file_name": new_name,
86
+ "original_name": original_name,
87
  "file_id": sent.document.file_id,
88
+ "file_size": document.file_size,
89
+ "upload_date": upload_date,
90
  "timestamp": timestamp
91
+ }
 
 
92
 
93
+ data["files"].append(file_entry)
94
 
95
+ save_data(user_id, data)
 
 
 
 
 
 
96
 
97
+ # 3️⃣ send JSON AFTER file
98
+ await context.bot.send_document(
99
+ chat_id=GROUP_ID,
100
+ document=InputFile(get_json_file(user_id)),
101
+ filename=f"{user_id}.json",
102
+ caption=f"πŸ“ File index for user {user_id}"
103
+ )
104
 
105
+ await update.message.reply_text("βœ… File saved & indexed!")
 
106
 
107
+ except Exception as e:
108
+ print("ERROR:", e)
109
+ await update.message.reply_text(f"❌ Error: {e}")
 
110
 
111
+ waiting_for_file.discard(user_id)
112
 
113
 
114
+ # πŸ“‹ /myfiles
115
+ async def myfiles(update: Update, context: ContextTypes.DEFAULT_TYPE):
116
  user_id = update.effective_user.id
 
 
 
 
 
 
117
  data = load_data(user_id)
118
 
119
+ if not data["files"]:
120
+ await update.message.reply_text("❌ No files found")
 
 
121
  return
122
 
123
+ msg = "πŸ“‚ Your Files:\n\n"
124
+ for f in data["files"][-10:]:
125
+ msg += f"β€’ {f['file_name']} ({f['upload_date']})\n"
126
+
127
+ await update.message.reply_text(msg)
128
 
129
 
130
+ # πŸ“₯ /get filename
131
+ async def get_file(update: Update, context: ContextTypes.DEFAULT_TYPE):
132
  user_id = update.effective_user.id
133
+ data = load_data(user_id)
134
 
135
  if not context.args:
136
+ await update.message.reply_text("Usage: /get filename")
137
  return
138
 
139
+ filename = context.args[0]
 
 
 
 
 
 
 
140
 
141
+ for f in data["files"]:
142
+ if f["file_name"] == filename:
143
+ await context.bot.send_document(
144
+ chat_id=update.effective_chat.id,
145
+ document=f["file_id"]
146
+ )
147
+ return
148
 
149
+ await update.message.reply_text("❌ File not found")
150
 
151
 
152
  # πŸš€ BOT START
153
  app = ApplicationBuilder().token(BOT_TOKEN).build()
154
 
155
+ app.add_handler(CommandHandler("save", save_command))
156
+ app.add_handler(CommandHandler("myfiles", myfiles))
157
  app.add_handler(CommandHandler("get", get_file))
 
 
 
 
158
  app.add_handler(MessageHandler(filters.Document.ALL, handle_file))
159
 
160
+ print("πŸ”₯ BOT RUNNING...")
161
  app.run_polling()