init commit

This commit is contained in:
2025-08-17 11:44:54 +09:00
commit 5592014530
59 changed files with 3175 additions and 0 deletions

0
app/tasks/__init__.py Normal file
View File

15
app/tasks/celery_app.py Normal file
View File

@@ -0,0 +1,15 @@
from celery import Celery
from app.core.config import settings
celery_app = Celery(
"autopost",
broker=settings.celery_broker_url,
backend=settings.celery_result_backend,
)
celery_app.conf.update(
timezone=settings.tz,
task_serializer="json",
accept_content=["json"],
result_serializer="json",
)

56
app/tasks/senders.py Normal file
View File

@@ -0,0 +1,56 @@
from celery import shared_task
import httpx
from loguru import logger
TELEGRAM_API_BASE = "https://api.telegram.org/bot{token}/{method}"
def _build_keyboard(keyboard: dict | None) -> dict | None:
if not keyboard:
return None
rows = []
for row in keyboard.get("rows", []):
rows.append([{"text": btn["text"], "url": btn["url"]} for btn in row])
return {"inline_keyboard": rows}
@shared_task
def send_post_task(token: str, chat_id: int, payload: dict):
"""Отправка поста через Bot API. payload: {type, text, media_file_id, parse_mode, keyboard}
keyboard: {rows: [[{text, url}, ...], ...]}
"""
t = payload.get("type", "text")
reply_markup = _build_keyboard(payload.get("keyboard"))
method = "sendMessage"
data: dict = {"chat_id": chat_id}
if t == "text":
method = "sendMessage"
data.update({"text": payload.get("text", ""), "parse_mode": payload.get("parse_mode")})
elif t == "photo":
method = "sendPhoto"
data.update({"photo": payload.get("media_file_id"), "caption": payload.get("text", ""),
"parse_mode": payload.get("parse_mode")})
elif t == "video":
method = "sendVideo"
data.update({"video": payload.get("media_file_id"), "caption": payload.get("text", ""),
"parse_mode": payload.get("parse_mode")})
elif t == "animation":
method = "sendAnimation"
data.update({"animation": payload.get("media_file_id"), "caption": payload.get("text", ""),
"parse_mode": payload.get("parse_mode")})
if reply_markup:
data["reply_markup"] = reply_markup
url = TELEGRAM_API_BASE.format(token=token, method=method)
with httpx.Client(timeout=30) as client:
r = client.post(url, json=data)
try:
r.raise_for_status()
logger.info("Sent post to {} via {}", chat_id, method)
return r.json()
except httpx.HTTPStatusError as e:
logger.error("Telegram send failed: {} :: {}", e, r.text)
raise