65 lines
2.6 KiB
Python
Executable File
65 lines
2.6 KiB
Python
Executable File
import aiohttp
|
|
import asyncio
|
|
import logging
|
|
from typing import Optional
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
class TelegramNotifier:
|
|
"""Telegram bot integration for alerts and reports"""
|
|
|
|
def __init__(self, bot_token: str, chat_id: str):
|
|
self.bot_token = bot_token
|
|
self.chat_id = chat_id
|
|
self.api_url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
|
|
|
|
async def send_alert(self, message: str) -> bool:
|
|
"""
|
|
Send alert message to Telegram.
|
|
NOW WITH FULL SUPPORT FOR REPORTS, NOT JUST STARTUP!
|
|
|
|
Args:
|
|
message: Message text (supports markdown)
|
|
|
|
Returns:
|
|
True if sent successfully
|
|
"""
|
|
try:
|
|
timeout = aiohttp.ClientTimeout(total=10)
|
|
async with aiohttp.ClientSession(timeout=timeout) as session:
|
|
payload = {
|
|
"chat_id": self.chat_id,
|
|
"text": message,
|
|
"parse_mode": "Markdown" # Use Markdown for better formatting
|
|
}
|
|
|
|
async with session.post(self.api_url, json=payload) as response:
|
|
if response.status == 200:
|
|
result = await response.json()
|
|
if result.get('ok'):
|
|
logger.info(f"✅ Telegram message sent (ID: {result.get('result', {}).get('message_id', 'N/A')})")
|
|
return True
|
|
else:
|
|
logger.warning(f"Telegram API error: {result.get('description', 'Unknown')}")
|
|
return False
|
|
else:
|
|
logger.warning(f"Telegram HTTP error: {response.status}")
|
|
return False
|
|
|
|
except asyncio.TimeoutError:
|
|
logger.warning("Telegram timeout (10s)")
|
|
return False
|
|
except Exception as e:
|
|
logger.error(f"Telegram send error: {e}")
|
|
return False
|
|
|
|
async def send_order_update(self, order_id: int, status: str, details: str):
|
|
"""Send order update to Telegram"""
|
|
message = f"📈 **Order Update**\n\nID: {order_id}\nStatus: {status}\nDetails: {details}"
|
|
return await self.send_alert(message)
|
|
|
|
async def send_trade_alert(self, entry_price: float, quantity: float, probability: float):
|
|
"""Send trade alert"""
|
|
message = f"🚀 **NEW TRADE**\n\nPrice: ${entry_price:,.2f}\nQty: {quantity}\nProbability: {probability*100:.1f}%"
|
|
return await self.send_alert(message)
|