82 lines
2.5 KiB
Python
82 lines
2.5 KiB
Python
from fastapi import FastAPI, Request, HTTPException
|
||
from fastapi.templating import Jinja2Templates
|
||
from fastapi.staticfiles import StaticFiles
|
||
from fastapi.responses import FileResponse, JSONResponse
|
||
from pydantic import BaseModel, EmailStr
|
||
from aiogram import Bot, Dispatcher
|
||
from fastapi.middleware.wsgi import WSGIMiddleware
|
||
import uvicorn
|
||
|
||
BOT_TOKEN = '7102060229:AAE4SWmgKXkCBC482l8Ble5lKzlCV2YIWnM'
|
||
ID = '340394898'
|
||
|
||
app = FastAPI()
|
||
|
||
# Для совместимости с WSGI
|
||
# application = WSGIMiddleware(app)
|
||
|
||
app.mount("/static", StaticFiles(directory="static"), name="static")
|
||
templates = Jinja2Templates(directory="templates")
|
||
|
||
bot = Bot( # Образ Бота
|
||
token=BOT_TOKEN,
|
||
)
|
||
|
||
@app.get("/")
|
||
async def index(request: Request):
|
||
return templates.TemplateResponse("index.html", {"request": request})
|
||
|
||
@app.get("/tracklink-presentation")
|
||
async def get_tracklink_presentation():
|
||
# Здесь вы можете добавить логику для отслеживания открытий
|
||
# Например, увеличить счетчик в базе данных
|
||
file_path = "/static/doc/SYBIKO_TrackLink.pdf"
|
||
|
||
headers = {
|
||
'Content-Disposition': 'inline; filename="SYBIKO_TrackLink.pdf"'
|
||
}
|
||
|
||
return FileResponse(file_path, headers=headers, media_type='application/pdf')
|
||
|
||
@app.get("/news/conference-omsk")
|
||
async def news_conference_omsk(request: Request):
|
||
return templates.TemplateResponse("news1.html", {"request": request})
|
||
|
||
|
||
|
||
class ContactForm(BaseModel):
|
||
name: str
|
||
email: EmailStr
|
||
message: str
|
||
|
||
@app.post("/submit-form")
|
||
async def submit_form(form_data: ContactForm):
|
||
try:
|
||
# Здесь вы можете добавить логику для сохранения данных в базу данных
|
||
# или отправки электронного письма
|
||
await send_email(form_data)
|
||
return JSONResponse(content={"message": "Form submitted successfully"}, status_code=200)
|
||
except Exception as e:
|
||
raise HTTPException(status_code=500, detail=str(e))
|
||
|
||
async def send_email(form_data: ContactForm):
|
||
|
||
body = f"""
|
||
Собщение от sybiko.ru
|
||
Имя: {form_data.name}
|
||
Email: {form_data.email}
|
||
Сообщение:
|
||
{form_data.message}
|
||
"""
|
||
print(body)
|
||
se = await bot.send_message(
|
||
ID,
|
||
body,
|
||
)
|
||
print(se)
|
||
|
||
|
||
# # Для локальной разработки
|
||
# if __name__ == "__main__":
|
||
# uvicorn.run(app, host="0.0.0.0", port=8000)
|