GitHub Actions
Clean project deployment
c6262df
Raw History Blame Contribute Delete
3.08 kB
"""
Telegram alert dispatcher (spec §10).
Pushes SETUP / SNIPER signals to Telegram via the Bot API (httpx POST, no library).
WATCH signals are logged silently (never alerted). Token + chat id come from
SECRETS (config) — never hard-coded. If alerts are disabled (no creds), format()
still works for the dashboard and the dispatcher is a no-op.
Each alert carries: tier + conviction tag, levels, layer breakdown, and the TV /
Chart links (decision D7). Exactly-once delivery is enforced by the caller logging
to alerts_sent and only dispatching for newly-admitted signals.
"""
from __future__ import annotations
import httpx
import config
import links
# emoji per conviction tag (colored visual cue)
_TAG_EMOJI = {"elite": "🟣", "strong": "🟢", "moderate": "🟡", "watch": "⚪"}
_TIER_LABEL = {"sniper": "SNIPER", "setup": "SETUP", "watch": "WATCH"}
def format_alert(sig: dict) -> str:
"""Build the Telegram message (Markdown) from a signal row/dict."""
tag = sig.get("conviction_tag", "watch")
emoji = _TAG_EMOJI.get(tag, "⚪")
tier = _TIER_LABEL.get(sig["tier"], sig["tier"].upper())
pair = links.pair_label(sig["symbol"])
direction = sig["direction"].upper()
tv = links.tv_link(sig["symbol"])
lines = [
f"{emoji} *{tier} · {tag.title()}* — Reversal",
f"*Pair:* {pair} *Direction:* {direction}",
f"*Conviction:* {sig.get('conviction', 0)}/100 *Confluence:* {sig.get('score_total', 0)}/100",
"",
f"*Entry:* `{_fmt(sig['entry'])}`",
f"*Stop:* `{_fmt(sig['stop'])}` *T1:* `{_fmt(sig['target1'])}` *T2:* `{_fmt(sig['target2'])}`",
f"*R:R (T1):* 1:{sig.get('rr1', 0)}",
"",
f"*Layers* — L1 {sig.get('score_liq',0)} · L2(F) {sig.get('score_funding',0)} · "
f"OI {sig.get('score_oi',0)} · L3 {sig.get('score_priceflow',0)}",
]
if sig.get("cold_start"):
lines.append("_⚠️ Cold start: liquidation baseline still maturing._")
lines.append(f"\n[TradingView]({tv})")
return "\n".join(lines)
def _fmt(v: float) -> str:
if v is None:
return "—"
if v >= 100:
return f"{v:,.2f}"
if v >= 1:
return f"{v:.4f}"
return f"{v:.6f}"
async def send(text: str, client: httpx.AsyncClient | None = None) -> bool:
"""POST a message to Telegram. Returns True on delivery. No-op if disabled."""
if not config.ALERTS_ENABLED:
return False
url = f"https://api.telegram.org/bot{config.TELEGRAM_BOT_TOKEN}/sendMessage"
payload = {
"chat_id": config.TELEGRAM_CHAT_ID,
"text": text,
"parse_mode": "Markdown",
"disable_web_page_preview": True,
}
owns = client is None
client = client or httpx.AsyncClient(timeout=15)
try:
r = await client.post(url, json=payload)
return r.status_code == 200 and r.json().get("ok", False)
except Exception as e:
print(f"[alert] send failed: {type(e).__name__}: {e}", flush=True)
return False
finally:
if owns:
await client.aclose()