This commit is contained in:
2026-04-24 20:38:31 +03:00
commit 9138512e2f
8 changed files with 108 additions and 0 deletions

5
.dockerignore Normal file
View File

@@ -0,0 +1,5 @@
.git
.venv
__pycache__
*.pyc
.env

3
.env.example Normal file
View File

@@ -0,0 +1,3 @@
TELEGRAM_BOT_TOKEN=
TELEGRAM_CHAT_ID=
TELEGRAM_MESSAGE_THREAD_ID=

14
.gitignore vendored Normal file
View File

@@ -0,0 +1,14 @@
.venv/
__pycache__/
*.py[cod]
*$py.class
.Python
*.egg-info/
.eggs/
dist/
build/
.env
.idea/
.vscode/
*.swp
*.swo

12
Dockerfile Normal file
View File

@@ -0,0 +1,12 @@
FROM python:3.12-slim-bookworm
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY main.py settings.py .
EXPOSE 8000
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

8
docker-compose.yml Normal file
View File

@@ -0,0 +1,8 @@
services:
app:
build: .
ports:
- '8000:8000'
env_file:
- .env
restart: unless-stopped

49
main.py Normal file
View File

@@ -0,0 +1,49 @@
from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException
from httpx import AsyncClient
from pydantic import BaseModel, EmailStr
from settings import settings
class NotifyBody(BaseModel):
name: str
email: EmailStr
@asynccontextmanager
async def lifespan(app: FastAPI):
app.state.http = AsyncClient(timeout=30.0)
yield
await app.state.http.aclose()
app = FastAPI(lifespan=lifespan)
async def send_telegram_message(client: AsyncClient, text: str) -> None:
url = f'https://api.telegram.org/bot{settings.telegram_bot_token}/sendMessage'
payload = {
'chat_id': settings.telegram_chat_id,
'message_thread_id': settings.telegram_message_thread_id,
'text': text,
}
resp = await client.post(url, json=payload)
data = resp.json()
if resp.status_code != 200 or not data.get('ok'):
detail = data.get('description', resp.text)
raise HTTPException(status_code=502, detail=detail)
@app.post('/notify')
async def notify(body: NotifyBody):
text = f'Новая заявка\nИмя: {body.name}\nПочта: {body.email}'
try:
await send_telegram_message(app.state.http, text)
except HTTPException:
raise
except Exception as exc:
raise HTTPException(status_code=502, detail=str(exc)) from exc
return {'ok': True}

5
requirements.txt Normal file
View File

@@ -0,0 +1,5 @@
fastapi>=0.115.0
uvicorn[standard]>=0.32.0
pydantic-settings>=2.6.0
httpx>=0.27.0
email-validator>=2.2.0

12
settings.py Normal file
View File

@@ -0,0 +1,12 @@
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
model_config = SettingsConfigDict(env_file='.env', env_file_encoding='utf-8')
telegram_bot_token: str
telegram_chat_id: str
telegram_message_thread_id: int
settings = Settings()