Spaces:
Sleeping
Sleeping
Create bot.py
Browse files
bot.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, io, tempfile, random, shutil, base64, subprocess, sys, zipfile
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
from fastapi import FastAPI, Request
|
| 4 |
+
from telegram import Update
|
| 5 |
+
from telegram.ext import Application, CommandHandler, MessageHandler, filters
|
| 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 |
+
async def echo_doc(update, context):
|
| 28 |
+
await update.message.reply_text("📄 Got your document!")
|
| 29 |
+
|
| 30 |
+
bot_app.add_handler(CommandHandler("start", start))
|
| 31 |
+
bot_app.add_handler(MessageHandler(filters.Document.ALL, echo_doc))
|
| 32 |
+
|
| 33 |
+
# --- /poll route (used by Worker) ---
|
| 34 |
+
@app.post("/poll")
|
| 35 |
+
async def poll_endpoint(request: Request):
|
| 36 |
+
data = await request.json()
|
| 37 |
+
update = Update.de_json(data, bot_app.bot)
|
| 38 |
+
await bot_app.update_queue.put(update)
|
| 39 |
+
return {"ok": True}
|
| 40 |
+
|
| 41 |
+
@app.get("/")
|
| 42 |
+
async def home():
|
| 43 |
+
return {"ok": True, "message": "Backend online!"}
|
| 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)))
|