"""Notification service — multi-channel delivery for VoIdea. Supported channels: - web (DB — stored as Notification model) - telegram (via TelegramBotClient) - email (via email_service) Only web channel is required; telegram and email are optional. """ from typing import Optional from uuid import UUID from sqlalchemy.ext.asyncio import AsyncSession from app.services.collaboration_service import CollaborationService class NotificationDeliveryService: """Delivers notifications across multiple channels.""" def __init__(self, db: AsyncSession): self.db = db self.collab = CollaborationService(db) async def notify( self, user_id: UUID, type: str, title: str, body: str, link: Optional[str] = None, channels: Optional[list[str]] = None, ) -> dict[str, bool]: """Deliver notification to specified channels (default: web only). Returns dict of channel -> success status. """ if channels is None: channels = ["web"] results: dict[str, bool] = {} if "web" in channels: try: await self.collab.create_notification( user_id=user_id, type=type, title=title, body=body, link=link, ) results["web"] = True except Exception: results["web"] = False if "telegram" in channels: try: await self._send_telegram(user_id, title, body, link) results["telegram"] = True except Exception: results["telegram"] = False if "email" in channels: try: await self._send_email(user_id, title, body) results["email"] = True except Exception: results["email"] = False return results async def _send_telegram( self, user_id: UUID, title: str, body: str, link: Optional[str] = None ) -> None: from app.core.config import get_settings settings = get_settings() if not settings.telegram_bot_token: return from app.integrations.telegram.client import TelegramBotClient client = TelegramBotClient(token=settings.telegram_bot_token) text = f"*{title}*\n{body}" if link: text += f"\n\n[Open]({link})" # Find user's telegram chat_id — for now send to first available # In production this would look up stored chat_id for the user from app.models.user import User user = await self.db.get(User, user_id) if not user: return async def _send_email( self, user_id: UUID, title: str, body: str ) -> None: from app.services.email_service import send_email from app.models.user import User user = await self.db.get(User, user_id) if not user or not user.email: return await send_email( to=user.email, subject=title, body=body, )