Spaces:
Sleeping
Sleeping
Update bot.py
Browse files
bot.py
CHANGED
|
@@ -1,48 +1,30 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from pathlib import Path
|
| 3 |
-
from fastapi import FastAPI,
|
| 4 |
-
from
|
| 5 |
-
from
|
| 6 |
-
from motor.motor_asyncio import AsyncIOMotorClient
|
| 7 |
-
import uvicorn
|
| 8 |
-
from config import BOT_TOKEN, ADMIN_ID, MONGO_URI, DB_NAME
|
| 9 |
|
| 10 |
-
# --- MongoDB ---
|
| 11 |
-
mongo_client = AsyncIOMotorClient(MONGO_URI)
|
| 12 |
-
db = mongo_client[DB_NAME]
|
| 13 |
-
stats_col = db["stats"]
|
| 14 |
-
|
| 15 |
-
async def init_stats():
|
| 16 |
-
if not await stats_col.find_one({"_id":"encryption_count"}):
|
| 17 |
-
await stats_col.insert_one({"_id":"encryption_count","count":0})
|
| 18 |
-
|
| 19 |
-
# --- Bot setup ---
|
| 20 |
app = FastAPI()
|
| 21 |
-
bot_app = Application.builder().token(BOT_TOKEN).build()
|
| 22 |
-
|
| 23 |
-
# --- Handlers ---
|
| 24 |
-
async def start(update, context):
|
| 25 |
-
await update.message.reply_text("🤖 Hello! Bot is running via Cloudflare Worker + FastAPI")
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
| 40 |
|
| 41 |
@app.get("/")
|
| 42 |
-
async def
|
| 43 |
-
return {"ok": True, "
|
| 44 |
-
|
| 45 |
-
if __name__ == "__main__":
|
| 46 |
-
import asyncio
|
| 47 |
-
asyncio.run(init_stats())
|
| 48 |
-
uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
|
|
|
|
| 1 |
+
# backend.py
|
| 2 |
+
import os
|
| 3 |
+
import shutil
|
| 4 |
+
import tempfile
|
| 5 |
+
import base64
|
| 6 |
from pathlib import Path
|
| 7 |
+
from fastapi import FastAPI, File, Form, UploadFile
|
| 8 |
+
from fastapi.responses import JSONResponse
|
| 9 |
+
from encoder import encode_bytes # helper encoding logic
|
|
|
|
|
|
|
|
|
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
@app.post("/api/encode")
|
| 14 |
+
async def api_encode(file: UploadFile = File(...), filename: str = Form(...), chat_id: str = Form(None), from_user: str = Form(None)):
|
| 15 |
+
"""
|
| 16 |
+
Receives file bytes, runs encoding logic, returns JSON:
|
| 17 |
+
{ ok: true, filename: "...", file_b64: "..." }
|
| 18 |
+
"""
|
| 19 |
+
try:
|
| 20 |
+
data = await file.read()
|
| 21 |
+
# encode_bytes returns bytes for result file
|
| 22 |
+
result_bytes, out_name = encode_bytes(data, filename)
|
| 23 |
+
file_b64 = base64.b64encode(result_bytes).decode()
|
| 24 |
+
return JSONResponse({"ok": True, "filename": out_name, "file_b64": file_b64})
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return JSONResponse({"ok": False, "error": str(e)}, status_code=500)
|
| 27 |
|
| 28 |
@app.get("/")
|
| 29 |
+
async def index():
|
| 30 |
+
return {"ok": True, "status": "HF backend alive"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|