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.
49 lines
1.5 KiB
Python
49 lines
1.5 KiB
Python
from datetime import datetime
|
|
|
|
from fastapi import APIRouter, Depends
|
|
|
|
from api.auth import require_api_key
|
|
from config import settings
|
|
from schemas import ChannelStatusResponse, ChannelType, SystemStatusResponse
|
|
|
|
router = APIRouter(prefix="/channels", tags=["channels"])
|
|
|
|
# Wird in main.py gesetzt
|
|
_channel_registry: dict = {}
|
|
|
|
|
|
def register(telegram: object, whatsapp: object, sms: object) -> None:
|
|
_channel_registry["telegram"] = telegram
|
|
_channel_registry["whatsapp"] = whatsapp
|
|
_channel_registry["sms"] = sms
|
|
|
|
|
|
@router.get("/status", response_model=SystemStatusResponse)
|
|
async def channel_status(_: str = Depends(require_api_key)):
|
|
statuses = []
|
|
|
|
for name, channel_type in [
|
|
("telegram", ChannelType.telegram),
|
|
("whatsapp", ChannelType.whatsapp),
|
|
("sms", ChannelType.sms),
|
|
]:
|
|
ch = _channel_registry.get(name)
|
|
if ch is None:
|
|
statuses.append(
|
|
ChannelStatusResponse(channel=channel_type, enabled=False, connected=False)
|
|
)
|
|
continue
|
|
connected, detail = await ch.check_connection()
|
|
enabled = (
|
|
settings.telegram_enabled
|
|
if name == "telegram"
|
|
else (settings.whatsapp_enabled if name == "whatsapp" else settings.sms_enabled)
|
|
)
|
|
statuses.append(
|
|
ChannelStatusResponse(
|
|
channel=channel_type, enabled=enabled, connected=connected, detail=detail
|
|
)
|
|
)
|
|
|
|
return SystemStatusResponse(channels=statuses, database=True, timestamp=datetime.utcnow())
|