"""Telegram notifications for scalp-bot."""
from __future__ import annotations
import logging
import httpx
from config import TelegramConfig
log = logging.getLogger(__name__)
def send_telegram(cfg: TelegramConfig, message: str) -> bool:
"""Send message via Telegram bot API. Returns True on success."""
if not cfg.enabled or not cfg.bot_token or not cfg.chat_id:
return False
url = f"https://api.telegram.org/bot{cfg.bot_token}/sendMessage"
try:
resp = httpx.post(url, json={
"chat_id": cfg.chat_id,
"text": message,
"parse_mode": "HTML",
}, timeout=10)
if resp.status_code != 200:
log.warning("Telegram API error: %s", resp.text)
return False
return True
except httpx.HTTPError as e:
log.warning("Telegram send failed: %s", e)
return False
def notify_entry(cfg: TelegramConfig, symbol: str, side: str,
entry: float, stop: float, tp: float) -> None:
tag = symbol.split("/")[0]
icon = "\U0001f7e2" if side == "long" else "\U0001f534"
msg = (
f"{icon} {tag} {side.upper()}\n"
f"Entry: ${entry:,.2f}\n"
f"Stop: ${stop:,.2f}\n"
f"TP: ${tp:,.2f}"
)
send_telegram(cfg, msg)
def notify_exit(cfg: TelegramConfig, symbol: str, pnl: float,
reason: str) -> None:
tag = symbol.split("/")[0]
icon = "\u2705" if pnl >= 0 else "\u274c"
msg = f"{icon} {tag} CLOSED ({reason}) PnL ${pnl:+,.2f}"
send_telegram(cfg, msg)
def notify_or(cfg: TelegramConfig, dt_str: str,
or_high: float, or_low: float) -> None:
msg = (
f"\U0001f4ca Opening Range {dt_str}\n"
f"High: ${or_high:,.2f} | Low: ${or_low:,.2f}\n"
f"Range: ${or_high - or_low:,.2f}"
)
send_telegram(cfg, msg)