|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | +from typing import Any |
| 5 | +from urllib.parse import quote |
| 6 | +from urllib.request import Request, urlopen |
| 7 | + |
| 8 | +from .publisher import report_filename |
| 9 | + |
| 10 | + |
| 11 | +def report_public_url(report: dict[str, Any], *, site_url: str) -> str: |
| 12 | + return f"{site_url.rstrip('/')}/{quote(report_filename(report))}" |
| 13 | + |
| 14 | + |
| 15 | +def _format_themes(report: dict[str, Any], *, limit: int) -> list[str]: |
| 16 | + theme_momentum = report.get("theme_momentum", {}) |
| 17 | + if not theme_momentum.get("available"): |
| 18 | + return ["- Themes: not available"] |
| 19 | + lines = ["Top themes:"] |
| 20 | + for theme in theme_momentum.get("top_themes", [])[:limit]: |
| 21 | + symbols = ", ".join(theme.get("top_symbols", [])[:5]) or "None" |
| 22 | + lines.append(f"- #{theme.get('rank')} {theme.get('theme_id')} score={theme.get('momentum_score')} symbols={symbols}") |
| 23 | + return lines |
| 24 | + |
| 25 | + |
| 26 | +def _format_recommendations(report: dict[str, Any], *, limit: int) -> list[str]: |
| 27 | + recommendations = report.get("recommendations", []) |
| 28 | + publishable = [ |
| 29 | + item |
| 30 | + for item in recommendations |
| 31 | + if item.get("recommendation_tier") in {"tier_1", "tier_2", "watchlist", "source_check"} |
| 32 | + ][:limit] |
| 33 | + if not publishable: |
| 34 | + return ["Recommendations: None promoted; review full report for monitor list."] |
| 35 | + lines = ["Recommendations:"] |
| 36 | + for item in publishable: |
| 37 | + lines.append( |
| 38 | + "- {symbol} | {tier} | {rating} | {horizon} | score={score}".format( |
| 39 | + symbol=item.get("symbol", ""), |
| 40 | + tier=item.get("recommendation_tier_label", item.get("recommendation_tier", "")), |
| 41 | + rating=item.get("rating_label", item.get("rating", "")), |
| 42 | + horizon=item.get("primary_horizon_label", ""), |
| 43 | + score=item.get("score", ""), |
| 44 | + ) |
| 45 | + ) |
| 46 | + return lines |
| 47 | + |
| 48 | + |
| 49 | +def format_telegram_message( |
| 50 | + report: dict[str, Any], |
| 51 | + *, |
| 52 | + site_url: str, |
| 53 | + max_recommendations: int = 5, |
| 54 | + max_themes: int = 5, |
| 55 | +) -> str: |
| 56 | + summary = report.get("summary", {}) |
| 57 | + lines = [ |
| 58 | + f"Quant Model Recommendations | {str(report.get('cadence', '')).title()} | {report.get('as_of', '')}", |
| 59 | + "", |
| 60 | + f"Mode: {report.get('mode', '')}", |
| 61 | + f"Source: {summary.get('source_mode', 'unknown')}", |
| 62 | + f"Source events: {summary.get('source_event_count', 0)}", |
| 63 | + "", |
| 64 | + *_format_themes(report, limit=max_themes), |
| 65 | + "", |
| 66 | + *_format_recommendations(report, limit=max_recommendations), |
| 67 | + "", |
| 68 | + "Policy: non-personalized model output only; no execution, allocation, or account-specific advice.", |
| 69 | + f"Full report: {report_public_url(report, site_url=site_url)}", |
| 70 | + ] |
| 71 | + return "\n".join(str(line) for line in lines if line is not None) |
| 72 | + |
| 73 | + |
| 74 | +def send_telegram_message(*, bot_token: str, chat_id: str, text: str, timeout: int = 20) -> dict[str, Any]: |
| 75 | + url = f"https://api.telegram.org/bot{bot_token}/sendMessage" |
| 76 | + payload = json.dumps( |
| 77 | + { |
| 78 | + "chat_id": chat_id, |
| 79 | + "text": text, |
| 80 | + "disable_web_page_preview": True, |
| 81 | + } |
| 82 | + ).encode("utf-8") |
| 83 | + request = Request(url, data=payload, headers={"Content-Type": "application/json"}, method="POST") |
| 84 | + with urlopen(request, timeout=timeout) as response: # noqa: S310 - fixed Telegram API endpoint. |
| 85 | + body = response.read().decode("utf-8") |
| 86 | + return json.loads(body) if body else {} |
0 commit comments