dragxd commited on
Commit
703eb53
·
verified ·
1 Parent(s): 6c13f44

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -0
app.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, UploadFile, File, Form
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ from aiogram import Bot
4
+ import os
5
+
6
+ BOT_TOKEN = os.getenv("BOT_TOKEN")
7
+ bot = Bot(token=BOT_TOKEN)
8
+ app = FastAPI()
9
+
10
+ app.add_middleware(
11
+ CORSMiddleware,
12
+ allow_origins=["*"],
13
+ allow_methods=["*"],
14
+ allow_headers=["*"],
15
+ )
16
+
17
+ @app.post("/upload")
18
+ async def upload(
19
+ file: UploadFile = File(...),
20
+ new_name: str = Form(...),
21
+ chat_id: str = Form(...)
22
+ ):
23
+ ext = os.path.splitext(file.filename)[1]
24
+ renamed = f"{new_name}{ext}"
25
+ with open(renamed, "wb") as f:
26
+ f.write(await file.read())
27
+
28
+ await bot.send_document(chat_id, open(renamed, "rb"))
29
+ os.remove(renamed)
30
+ return {"status": "sent"}