briga-core-api / app /api /telegram.py
yabeskn's picture
feat: sync briga-backend — 2026 fallback chain (Gemini->OpenRouter :free->Groq), per-agent tier override, supabase JWT auth, agents router, retry columns
3a006a3 verified
Raw History Blame Contribute Delete
1.5 kB
"""Telegram Webhook API — Executive Command Gateway."""
from fastapi import APIRouter, Depends, Request, Header, HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession
from app.database import get_db
from app.services.telegram_ceo_service import TelegramCEOService
from app.config.settings import settings
from typing import Optional
import httpx
from loguru import logger
router = APIRouter(tags=["Executive Control"])
@router.post("/webhook/telegram")
@router.post("/v1/webhook/telegram")
async def telegram_webhook(
request: Request,
db: AsyncSession = Depends(get_db),
x_webhook_token: Optional[str] = Header(None)
):
"""Receives commands/updates directly from Telegram or API clients."""
data = await request.json()
# Check if this is a standard Telegram payload
is_telegram_payload = ("message" in data or "callback_query" in data or "edited_message" in data)
# Security Check: Only enforce for non-Telegram clients
if not is_telegram_payload:
expected_secret = settings.webhook_secret or settings.security_webhook_token
if expected_secret and x_webhook_token != expected_secret:
logger.warning("[Telegram Webhook] Unauthorized access attempt.")
return {"status": "unauthorized"}
ceo_service = TelegramCEOService(db)
# Process webhook natively via the service
result = await ceo_service.handle_webhook(data)
return result