-
Notifications
You must be signed in to change notification settings - Fork 0
feat(tg): send_trading_report script with HTML->plain fallback #200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,90 @@ | ||||||||||||||
| #!/usr/bin/env python3 | ||||||||||||||
| """Send the detailed trading report (Трейдинг button content) to the owner's | ||||||||||||||
| Telegram chat: data chunks immediately, LLM section after generation.""" | ||||||||||||||
|
|
||||||||||||||
| import json | ||||||||||||||
| import sys | ||||||||||||||
| import urllib.request | ||||||||||||||
| from pathlib import Path | ||||||||||||||
|
|
||||||||||||||
| ROOT = Path("/root/AIOS") | ||||||||||||||
| sys.path.insert(0, str(ROOT)) | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def _env(key: str) -> str: | ||||||||||||||
| if key in ("AIOS_TELEGRAM_TOKEN", "TELEGRAM_BOT_TOKEN"): | ||||||||||||||
| from tg_bot.credentials import secret_from_env_or_credential | ||||||||||||||
| value = secret_from_env_or_credential( | ||||||||||||||
| "AIOS_TELEGRAM_TOKEN", "TELEGRAM_BOT_TOKEN", credential="telegram_token" | ||||||||||||||
| ) | ||||||||||||||
| if value: | ||||||||||||||
| return value | ||||||||||||||
| if key in ("TELEGRAM_CHAT_ID", "AIOS_OWNER_CHAT_ID"): | ||||||||||||||
| from tg_bot.credentials import read_systemd_credential | ||||||||||||||
| value = read_systemd_credential("telegram_owner_chat_id") | ||||||||||||||
| if value: | ||||||||||||||
| return value | ||||||||||||||
| import os | ||||||||||||||
| v = os.environ.get(key, "") | ||||||||||||||
| if v: | ||||||||||||||
| return v | ||||||||||||||
| p = ROOT / ".env" | ||||||||||||||
| if p.exists(): | ||||||||||||||
| for line in p.read_text(encoding="utf-8").splitlines(): | ||||||||||||||
| line = line.strip() | ||||||||||||||
| if line.startswith(key + "="): | ||||||||||||||
| return line.split("=", 1)[1].strip().strip('"').strip("'") | ||||||||||||||
| return "" | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def _post(payload: dict, token: str) -> tuple[bool, str]: | ||||||||||||||
| req = urllib.request.Request( | ||||||||||||||
| f"https://api.telegram.org/bot{token}/sendMessage", | ||||||||||||||
| data=json.dumps(payload).encode(), | ||||||||||||||
| headers={"Content-Type": "application/json"}) | ||||||||||||||
| try: | ||||||||||||||
| with urllib.request.urlopen(req, timeout=60) as resp: | ||||||||||||||
| data = json.loads(resp.read()) | ||||||||||||||
| return bool(data.get("ok")), data.get("description", "") | ||||||||||||||
| except urllib.error.HTTPError as exc: | ||||||||||||||
| return False, exc.read().decode(errors="replace")[:300] | ||||||||||||||
| except Exception as exc: | ||||||||||||||
| return False, str(exc) | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def tg_send(text: str) -> tuple[bool, str]: | ||||||||||||||
| token = _env("TELEGRAM_BOT_TOKEN") or _env("AIOS_TELEGRAM_TOKEN") | ||||||||||||||
| chat = _env("TELEGRAM_CHAT_ID") or _env("AIOS_OWNER_CHAT_ID") | ||||||||||||||
| if not token or not chat: | ||||||||||||||
| return False, "no credentials" | ||||||||||||||
| base = { | ||||||||||||||
| "chat_id": int(chat), | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔥 The Roast: 🩹 The Fix:
Suggested change
📏 Severity: warning Reply with |
||||||||||||||
| "text": text[:3900], | ||||||||||||||
| "disable_web_page_preview": True, | ||||||||||||||
| } | ||||||||||||||
| ok, err = _post({**base, "parse_mode": "HTML"}, token) | ||||||||||||||
| if ok: | ||||||||||||||
| return True, "" | ||||||||||||||
| # HTML-режим мог сломаться о символы в LLM-тексте — ретрай без разметки | ||||||||||||||
| ok2, err2 = _post(base, token) | ||||||||||||||
| return ok2, f"html:{err[:80]}; plain:{err2[:120]}" | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def main() -> int: | ||||||||||||||
| from tg_bot.trading_report import full_report | ||||||||||||||
|
|
||||||||||||||
| messages = full_report() | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔥 The Roast: 🩹 The Fix:
Suggested change
📏 Severity: suggestion Reply with |
||||||||||||||
| sent = 0 | ||||||||||||||
| for i, msg in enumerate(messages, 1): | ||||||||||||||
| ok, err = tg_send(msg) | ||||||||||||||
| if ok: | ||||||||||||||
| sent += 1 | ||||||||||||||
| print(f"chunk {i}/{len(messages)}: sent ({len(msg)} chars)") | ||||||||||||||
| else: | ||||||||||||||
| print(f"chunk {i}/{len(messages)}: FAIL {err[:200]}") | ||||||||||||||
| print(f"total sent: {sent}/{len(messages)}") | ||||||||||||||
| return 0 if sent == len(messages) else 1 | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| if __name__ == "__main__": | ||||||||||||||
| raise SystemExit(main()) | ||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔥 The Roast:
ROOT = Path("/root/AIOS")— This script is so committed to its/root/AIOSaddress that it won't even consider running anywhere else. It's the "I only date people from my hometown" of Python scripts.🩹 The Fix:
📏 Severity: warning
Reply with
@kilocode-bot fix itto have Kilo Code address this issue.