Silent notifications when human is online in web

This commit is contained in:
Markov 2026-02-28 18:42:54 +01:00
parent 4b3f7f0c36
commit 47c5c8f4b9

View File

@ -33,6 +33,16 @@ async def on_tracker_event(event: dict):
"""Forward Tracker events to Telegram topics.""" """Forward Tracker events to Telegram topics."""
event_type = event.get("type", "") event_type = event.get("type", "")
if event_type == "agent.status":
data = event.get("data", {})
slug = data.get("slug", "")
status = data.get("status", "")
if status == "online":
_online_members.add(slug)
else:
_online_members.discard(slug)
return
if event_type == "message.new": if event_type == "message.new":
msg = event.get("data", {}) msg = event.get("data", {})
author = msg.get("author", {}) author = msg.get("author", {})
@ -56,7 +66,9 @@ async def on_tracker_event(event: dict):
# Format: "Author: message" # Format: "Author: message"
tg_text = f"<b>{_escape_html(author_name)}</b>: {_escape_html(text)}" tg_text = f"<b>{_escape_html(author_name)}</b>: {_escape_html(text)}"
await _send_to_topic(topic_id, tg_text) # Silent if any human is online in web
silent = any(s for s in _online_members if s not in ("coder", "architect", "bridge"))
await _send_to_topic(topic_id, tg_text, silent=silent)
elif event_type == "task.created": elif event_type == "task.created":
task = event.get("data", {}) task = event.get("data", {})
@ -68,7 +80,8 @@ async def on_tracker_event(event: dict):
key = task.get("key", "?") key = task.get("key", "?")
title = task.get("title", "") title = task.get("title", "")
tg_text = f"📋 Новая задача <b>{_escape_html(key)}</b>: {_escape_html(title)}" tg_text = f"📋 Новая задача <b>{_escape_html(key)}</b>: {_escape_html(title)}"
await _send_to_topic(topic_id, tg_text) silent = any(s for s in _online_members if s not in ("coder", "architect", "bridge"))
await _send_to_topic(topic_id, tg_text, silent=silent)
elif event_type == "project.created": elif event_type == "project.created":
project = event.get("project", {}) project = event.get("project", {})
@ -90,6 +103,9 @@ async def on_tracker_event(event: dict):
logger.info("Auto-created topic %d for project %s (%s)", topic_id, project_name, project_id[:8]) logger.info("Auto-created topic %d for project %s (%s)", topic_id, project_name, project_id[:8])
# Track online members
_online_members: set[str] = set() # member slugs that are online
tracker = TrackerClient(config, on_message=on_tracker_event) tracker = TrackerClient(config, on_message=on_tracker_event)
@ -206,7 +222,7 @@ async def _create_topic(name: str) -> int | None:
return None return None
async def _send_to_topic(topic_id: int, text: str): async def _send_to_topic(topic_id: int, text: str, silent: bool = False):
"""Send a message to a specific Telegram topic.""" """Send a message to a specific Telegram topic."""
global _bot global _bot
if _bot is None: if _bot is None:
@ -218,7 +234,7 @@ async def _send_to_topic(topic_id: int, text: str):
message_thread_id=topic_id, message_thread_id=topic_id,
text=text, text=text,
parse_mode="HTML", parse_mode="HTML",
disable_notification=True, disable_notification=silent,
) )
except Exception as e: except Exception as e:
logger.error("Failed to send to topic %d: %s", topic_id, e) logger.error("Failed to send to topic %d: %s", topic_id, e)