Update app.py
Browse files
app.py
CHANGED
|
@@ -3,15 +3,19 @@ BOT_TOKEN = "6740023413:AAHZ5iEiXjkY7IGwljg37llFbekKLbIVkkw"
|
|
| 3 |
|
| 4 |
# π΄ HARD CODED VALUES
|
| 5 |
GROUP_ID = -1003799294466
|
| 6 |
-
|
| 7 |
import json
|
| 8 |
import time
|
| 9 |
from datetime import datetime
|
| 10 |
-
from telegram import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
from telegram.ext import (
|
| 12 |
ApplicationBuilder,
|
| 13 |
MessageHandler,
|
| 14 |
CommandHandler,
|
|
|
|
| 15 |
ContextTypes,
|
| 16 |
filters
|
| 17 |
)
|
|
@@ -20,16 +24,15 @@ from telegram.ext import (
|
|
| 20 |
waiting_for_file = set()
|
| 21 |
|
| 22 |
|
| 23 |
-
# π FIND USER JSON
|
| 24 |
async def find_user_json(context, user_id):
|
| 25 |
-
async for msg in context.bot.get_chat_history(GROUP_ID, limit=
|
| 26 |
-
if msg.document:
|
| 27 |
-
|
| 28 |
-
return msg.document.file_id
|
| 29 |
return None
|
| 30 |
|
| 31 |
|
| 32 |
-
# π₯ LOAD JSON FROM
|
| 33 |
async def load_data(context, user_id):
|
| 34 |
file_id = await find_user_json(context, user_id)
|
| 35 |
|
|
@@ -41,16 +44,14 @@ async def load_data(context, user_id):
|
|
| 41 |
|
| 42 |
try:
|
| 43 |
data = json.loads(content.decode("utf-8"))
|
| 44 |
-
|
| 45 |
if "files" not in data:
|
| 46 |
return {"files": []}
|
| 47 |
-
|
| 48 |
return data
|
| 49 |
except:
|
| 50 |
return {"files": []}
|
| 51 |
|
| 52 |
|
| 53 |
-
# π€ SAVE JSON
|
| 54 |
async def save_data(context, user_id, data):
|
| 55 |
json_bytes = json.dumps(data, indent=4).encode("utf-8")
|
| 56 |
|
|
@@ -62,6 +63,49 @@ async def save_data(context, user_id, data):
|
|
| 62 |
)
|
| 63 |
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
# π’ /save
|
| 66 |
async def save_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 67 |
waiting_for_file.add(update.effective_user.id)
|
|
@@ -109,10 +153,10 @@ async def handle_file(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
| 109 |
|
| 110 |
data["files"].append(entry)
|
| 111 |
|
| 112 |
-
# β
STEP 3: save JSON
|
| 113 |
await save_data(context, user_id, data)
|
| 114 |
|
| 115 |
-
await update.message.reply_text("β
File saved
|
| 116 |
|
| 117 |
except Exception as e:
|
| 118 |
print(e)
|
|
@@ -127,12 +171,12 @@ async def myfiles(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
| 127 |
data = await load_data(context, user_id)
|
| 128 |
|
| 129 |
if not data["files"]:
|
| 130 |
-
await update.message.reply_text("β No files")
|
| 131 |
return
|
| 132 |
|
| 133 |
-
msg = "π Files:\n\n"
|
| 134 |
for f in data["files"]:
|
| 135 |
-
msg += f"β’ {f['file_name']}\n"
|
| 136 |
|
| 137 |
await update.message.reply_text(msg)
|
| 138 |
|
|
@@ -156,7 +200,7 @@ async def get_file(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
| 156 |
)
|
| 157 |
return
|
| 158 |
|
| 159 |
-
await update.message.reply_text("β
|
| 160 |
|
| 161 |
|
| 162 |
# π /stats
|
|
@@ -172,15 +216,17 @@ async def stats(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
| 172 |
)
|
| 173 |
|
| 174 |
|
| 175 |
-
# π RUN
|
| 176 |
app = ApplicationBuilder().token(BOT_TOKEN).build()
|
| 177 |
|
|
|
|
| 178 |
app.add_handler(CommandHandler("save", save_command))
|
| 179 |
app.add_handler(CommandHandler("myfiles", myfiles))
|
| 180 |
app.add_handler(CommandHandler("get", get_file))
|
| 181 |
app.add_handler(CommandHandler("stats", stats))
|
| 182 |
|
|
|
|
| 183 |
app.add_handler(MessageHandler(filters.Document.ALL, handle_file))
|
| 184 |
|
| 185 |
-
print("π₯ CLOUD
|
| 186 |
app.run_polling()
|
|
|
|
| 3 |
|
| 4 |
# π΄ HARD CODED VALUES
|
| 5 |
GROUP_ID = -1003799294466
|
|
|
|
| 6 |
import json
|
| 7 |
import time
|
| 8 |
from datetime import datetime
|
| 9 |
+
from telegram import (
|
| 10 |
+
Update,
|
| 11 |
+
InlineKeyboardButton,
|
| 12 |
+
InlineKeyboardMarkup
|
| 13 |
+
)
|
| 14 |
from telegram.ext import (
|
| 15 |
ApplicationBuilder,
|
| 16 |
MessageHandler,
|
| 17 |
CommandHandler,
|
| 18 |
+
CallbackQueryHandler,
|
| 19 |
ContextTypes,
|
| 20 |
filters
|
| 21 |
)
|
|
|
|
| 24 |
waiting_for_file = set()
|
| 25 |
|
| 26 |
|
| 27 |
+
# π FIND USER JSON IN GROUP
|
| 28 |
async def find_user_json(context, user_id):
|
| 29 |
+
async for msg in context.bot.get_chat_history(GROUP_ID, limit=200):
|
| 30 |
+
if msg.document and msg.document.file_name == f"{user_id}.json":
|
| 31 |
+
return msg.document.file_id
|
|
|
|
| 32 |
return None
|
| 33 |
|
| 34 |
|
| 35 |
+
# π₯ LOAD JSON FROM GROUP
|
| 36 |
async def load_data(context, user_id):
|
| 37 |
file_id = await find_user_json(context, user_id)
|
| 38 |
|
|
|
|
| 44 |
|
| 45 |
try:
|
| 46 |
data = json.loads(content.decode("utf-8"))
|
|
|
|
| 47 |
if "files" not in data:
|
| 48 |
return {"files": []}
|
|
|
|
| 49 |
return data
|
| 50 |
except:
|
| 51 |
return {"files": []}
|
| 52 |
|
| 53 |
|
| 54 |
+
# π€ SAVE JSON TO GROUP
|
| 55 |
async def save_data(context, user_id, data):
|
| 56 |
json_bytes = json.dumps(data, indent=4).encode("utf-8")
|
| 57 |
|
|
|
|
| 63 |
)
|
| 64 |
|
| 65 |
|
| 66 |
+
# π START
|
| 67 |
+
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 68 |
+
keyboard = [
|
| 69 |
+
[InlineKeyboardButton("π€ Upload", callback_data="upload")],
|
| 70 |
+
[InlineKeyboardButton("π My Files", callback_data="files")],
|
| 71 |
+
[InlineKeyboardButton("π Stats", callback_data="stats")]
|
| 72 |
+
]
|
| 73 |
+
|
| 74 |
+
await update.message.reply_text(
|
| 75 |
+
"π Cloud Storage Bot (No Server Storage βοΈ)",
|
| 76 |
+
reply_markup=InlineKeyboardMarkup(keyboard)
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
# π BUTTON HANDLER
|
| 81 |
+
async def buttons(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 82 |
+
query = update.callback_query
|
| 83 |
+
await query.answer()
|
| 84 |
+
|
| 85 |
+
user_id = query.from_user.id
|
| 86 |
+
|
| 87 |
+
if query.data == "upload":
|
| 88 |
+
waiting_for_file.add(user_id)
|
| 89 |
+
await query.message.reply_text("π Send file to upload")
|
| 90 |
+
|
| 91 |
+
elif query.data == "files":
|
| 92 |
+
data = await load_data(context, user_id)
|
| 93 |
+
|
| 94 |
+
if not data["files"]:
|
| 95 |
+
await query.message.reply_text("β No files")
|
| 96 |
+
return
|
| 97 |
+
|
| 98 |
+
msg = "π Your Files:\n\n"
|
| 99 |
+
for f in data["files"][-10:]:
|
| 100 |
+
msg += f"β’ {f['file_name']}\n"
|
| 101 |
+
|
| 102 |
+
await query.message.reply_text(msg)
|
| 103 |
+
|
| 104 |
+
elif query.data == "stats":
|
| 105 |
+
data = await load_data(context, user_id)
|
| 106 |
+
await query.message.reply_text(f"π Total files: {len(data['files'])}")
|
| 107 |
+
|
| 108 |
+
|
| 109 |
# π’ /save
|
| 110 |
async def save_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 111 |
waiting_for_file.add(update.effective_user.id)
|
|
|
|
| 153 |
|
| 154 |
data["files"].append(entry)
|
| 155 |
|
| 156 |
+
# β
STEP 3: save JSON back to group
|
| 157 |
await save_data(context, user_id, data)
|
| 158 |
|
| 159 |
+
await update.message.reply_text("β
File saved successfully βοΈ")
|
| 160 |
|
| 161 |
except Exception as e:
|
| 162 |
print(e)
|
|
|
|
| 171 |
data = await load_data(context, 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"
|
| 178 |
for f in data["files"]:
|
| 179 |
+
msg += f"β’ {f['file_name']} ({f['upload_date']})\n"
|
| 180 |
|
| 181 |
await update.message.reply_text(msg)
|
| 182 |
|
|
|
|
| 200 |
)
|
| 201 |
return
|
| 202 |
|
| 203 |
+
await update.message.reply_text("β File not found")
|
| 204 |
|
| 205 |
|
| 206 |
# π /stats
|
|
|
|
| 216 |
)
|
| 217 |
|
| 218 |
|
| 219 |
+
# π RUN BOT
|
| 220 |
app = ApplicationBuilder().token(BOT_TOKEN).build()
|
| 221 |
|
| 222 |
+
app.add_handler(CommandHandler("start", start))
|
| 223 |
app.add_handler(CommandHandler("save", save_command))
|
| 224 |
app.add_handler(CommandHandler("myfiles", myfiles))
|
| 225 |
app.add_handler(CommandHandler("get", get_file))
|
| 226 |
app.add_handler(CommandHandler("stats", stats))
|
| 227 |
|
| 228 |
+
app.add_handler(CallbackQueryHandler(buttons))
|
| 229 |
app.add_handler(MessageHandler(filters.Document.ALL, handle_file))
|
| 230 |
|
| 231 |
+
print("π₯ CLOUD STORAGE BOT RUNNING...")
|
| 232 |
app.run_polling()
|