File size: 731 Bytes
703eb53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from fastapi import FastAPI, UploadFile, File, Form
from fastapi.middleware.cors import CORSMiddleware
from aiogram import Bot
import os

BOT_TOKEN = os.getenv("BOT_TOKEN")
bot = Bot(token=BOT_TOKEN)
app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.post("/upload")
async def upload(
    file: UploadFile = File(...),
    new_name: str = Form(...),
    chat_id: str = Form(...)
):
    ext = os.path.splitext(file.filename)[1]
    renamed = f"{new_name}{ext}"
    with open(renamed, "wb") as f:
        f.write(await file.read())

    await bot.send_document(chat_id, open(renamed, "rb"))
    os.remove(renamed)
    return {"status": "sent"}