27 lines
1.0 KiB
Python
27 lines
1.0 KiB
Python
from typing import Union
|
|
import aiogram
|
|
import html
|
|
from src.notifications.base import INotificationService
|
|
from src.processor.dto import EnrichedNewsItemDTO
|
|
|
|
class TelegramNotifier(INotificationService):
|
|
def __init__(self, bot: aiogram.Bot, chat_id: Union[str, int]):
|
|
self.bot = bot
|
|
self.chat_id = chat_id
|
|
|
|
async def send_alert(self, item: EnrichedNewsItemDTO) -> None:
|
|
title = html.escape(item.title)
|
|
summary = html.escape(item.summary_ru)
|
|
anomalies_list = [html.escape(a) for a in item.anomalies_detected] if item.anomalies_detected else []
|
|
anomalies_text = ", ".join(anomalies_list) if anomalies_list else "None"
|
|
url = html.escape(item.url)
|
|
|
|
formatted_text = (
|
|
f"<b>{title}</b>\n\n"
|
|
f"Relevance: {item.relevance_score}/10\n"
|
|
f"Anomalies: {anomalies_text}\n\n"
|
|
f"{summary}\n\n"
|
|
f"<a href='{url}'>Source</a>"
|
|
)
|
|
await self.bot.send_message(chat_id=self.chat_id, text=formatted_text, parse_mode="HTML")
|