fix(): fixed bugs with cache invalidation, notifications and guardrails
This commit is contained in:
@@ -200,15 +200,24 @@ def _build_event_key(
|
||||
return f"{event_type}:{payload.experiment_id}:{bucket}"
|
||||
|
||||
|
||||
def _escape_markdown(text: str) -> str:
|
||||
for ch in r"\_*[]()~`>#+-=|{}.!":
|
||||
text = text.replace(ch, f"\\{ch}")
|
||||
return text
|
||||
|
||||
|
||||
def _send_telegram(config: dict[str, Any], payload: dict[str, Any]) -> None:
|
||||
bot_token = config.get("bot_token", "")
|
||||
chat_id = config.get("chat_id", "")
|
||||
if not bot_token or not chat_id:
|
||||
raise ValueError("Telegram config requires 'bot_token' and 'chat_id'.")
|
||||
|
||||
text = f"*{payload['title']}*\n\n{payload['body']}"
|
||||
title = _escape_markdown(payload["title"])
|
||||
body = _escape_markdown(payload["body"])
|
||||
text = f"*{title}*\n\n{body}"
|
||||
if payload.get("experiment_name"):
|
||||
text += f"\n\nExperiment: {payload['experiment_name']}"
|
||||
name = _escape_markdown(payload["experiment_name"])
|
||||
text += f"\n\nExperiment: {name}"
|
||||
|
||||
api_url = config.get(
|
||||
"api_url",
|
||||
@@ -219,7 +228,7 @@ def _send_telegram(config: dict[str, Any], payload: dict[str, Any]) -> None:
|
||||
json={
|
||||
"chat_id": chat_id,
|
||||
"text": text,
|
||||
"parse_mode": "Markdown",
|
||||
"parse_mode": "MarkdownV2",
|
||||
},
|
||||
timeout=10,
|
||||
)
|
||||
@@ -249,7 +258,7 @@ def _send_smtp(config: dict[str, Any], payload: dict[str, Any]) -> None:
|
||||
def flush_pending_notifications() -> dict[str, int]:
|
||||
pending = NotificationLog.objects.filter(
|
||||
status=NotificationStatus.PENDING,
|
||||
).select_related("channel")
|
||||
).select_related("channel").order_by("created_at")
|
||||
|
||||
senders = {
|
||||
ChannelType.TELEGRAM: _send_telegram,
|
||||
|
||||
Reference in New Issue
Block a user