Initial MCM project: FastAPI + Textual TUI unified messenger
MultiCustomerMessenger supporting Telegram (python-telegram-bot), WhatsApp (Green API) and SMS (python-gsmmodem-new). REST API with Bearer-token auth, SQLAlchemy models for MariaDB, APScheduler for background polling, and Textual TUI running in same asyncio event-loop.
This commit is contained in:
44
api/routes/messages.py
Normal file
44
api/routes/messages.py
Normal file
@@ -0,0 +1,44 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from api.auth import require_api_key
|
||||
from db.database import get_db
|
||||
from schemas import MessageResponse, SendMessageRequest
|
||||
from services import message_service
|
||||
|
||||
router = APIRouter(prefix="/messages", tags=["messages"])
|
||||
|
||||
|
||||
@router.post("/", response_model=MessageResponse, status_code=201)
|
||||
async def send_message(
|
||||
req: SendMessageRequest,
|
||||
db: Session = Depends(get_db),
|
||||
_: str = Depends(require_api_key),
|
||||
):
|
||||
if req.channel == "telegram" and not req.recipient_telegram_id:
|
||||
raise HTTPException(status_code=422, detail="recipient_telegram_id required for Telegram")
|
||||
if req.channel in ("whatsapp", "sms") and not req.recipient_phone:
|
||||
raise HTTPException(status_code=422, detail="recipient_phone required for WhatsApp/SMS")
|
||||
|
||||
msg = await message_service.send(db, req)
|
||||
return MessageResponse.model_validate(msg)
|
||||
|
||||
|
||||
@router.get("/", response_model=list[MessageResponse])
|
||||
def list_messages(
|
||||
conversation_id: str | None = Query(None),
|
||||
channel: str | None = Query(None),
|
||||
limit: int = Query(50, ge=1, le=200),
|
||||
offset: int = Query(0, ge=0),
|
||||
db: Session = Depends(get_db),
|
||||
_: str = Depends(require_api_key),
|
||||
):
|
||||
from db.models import Message
|
||||
|
||||
q = db.query(Message)
|
||||
if conversation_id:
|
||||
q = q.filter(Message.conversation_id == conversation_id)
|
||||
if channel:
|
||||
q = q.filter(Message.channel == channel)
|
||||
msgs = q.order_by(Message.created_at.desc()).offset(offset).limit(limit).all()
|
||||
return [MessageResponse.model_validate(m) for m in msgs]
|
||||
Reference in New Issue
Block a user