-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtelegram_delivery.py
More file actions
62 lines (55 loc) · 2.08 KB
/
telegram_delivery.py
File metadata and controls
62 lines (55 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
Telegram webhook delivery (v0.4.0) — formatted alerts under 2s latency.
"""
import logging
import time
from typing import Optional
logger = logging.getLogger(__name__)
def send_telegram(cfg: dict, message: str) -> bool:
"""Send message to Telegram. cfg has bot_token, chat_id. Returns True on success."""
bot_token = cfg.get("bot_token", "")
chat_id = cfg.get("chat_id", "")
if not bot_token or not chat_id or "YOUR_" in str(bot_token) or "YOUR_" in str(chat_id):
return False
try:
import requests
url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
payload = {"chat_id": chat_id, "text": message[:4096]}
r = requests.post(url, json=payload, timeout=10)
return r.status_code == 200
except Exception as e:
logger.error("Telegram send failed: %s", e)
return False
def format_signal(
direction: str, # BUY | SELL
asset: str,
source: str,
score: float,
model_note: str,
tier: int,
followers: int,
engagement_pct: float,
raw_text: str,
keyword_hits: Optional[list] = None,
) -> str:
"""Format signal for Telegram (matches README sample)."""
emoji = "🟢" if direction == "BUY" else "🔴"
tier_str = f"TIER {tier} (Top 20 watchlist)" if tier == 1 else f"TIER {tier} (Extended watchlist)"
reach = f"{followers:,} followers" if followers else "N/A"
eng = f" | Avg engagement: {engagement_pct}%" if engagement_pct else ""
kw = ", ".join(keyword_hits[:5]) if keyword_hits else "—"
lines = [
"═" * 42,
f" {emoji} {direction} SIGNAL DETECTED",
f" Asset: ${asset}",
f" Timestamp: {__import__('datetime').datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')} UTC",
f" Source: @{source}",
f" Tweet score: {score:.2f} {direction} ({model_note})",
f" Keyword hit: {kw}",
f" Reach: {reach}{eng}",
f" Signal tier: {tier_str}",
"─" * 42,
f' Raw: "{raw_text[:200]}{"..." if len(raw_text) > 200 else ""}"',
"═" * 42,
]
return "\n".join(lines)