Overview
The Telegram Notifier module provides a comprehensive interface for sending alerts, predictions, and trading signals via Telegram bot messages.TelegramNotifier Class
Constructor
bot_token(str): Telegram Bot API token (format:123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11)chat_id(str): Telegram chat ID where messages will be sent
bot_token(str): Stored bot tokenchat_id(str): Stored chat IDapi_url(str): Constructed Telegram API base URL
Methods
send_message
message(str): Message text to sendparse_mode(str, optional): Message formatting mode. Default:'Markdown''Markdown': Use Markdown formatting'HTML': Use HTML formatting
bool: True if message sent successfully, False otherwise
Example:
send_alert
crypto(str): Cryptocurrency symbol (e.g., “BTC”, “ETH”, “SOL”)alert_type(str): Type of alert (e.g., “Precio”, “RSI”, “Volume”)current_value(float): Current value that triggered the alertthreshold(float): Configured threshold valuecondition(str): Trigger condition (e.g., “Mayor que”, “Menor que”)
bool: True if alert sent successfully, False otherwise
Example:
send_prediction_alert
crypto(str): Cryptocurrency symbolcurrent_price(float): Current market pricepredicted_price(float): ML-predicted future pricehours(int): Prediction time horizon in hourschange_pct(float): Expected percentage change (can be positive or negative)confidence(float): Model confidence score (0-100)
bool: True if prediction alert sent successfully, False otherwise
Example:
send_signal
crypto(str): Cryptocurrency symbolsignal(str): Trading signal - must be one of:"COMPRA": Buy signal"VENTA": Sell signal"NEUTRAL": Neutral/hold signal
current_price(float): Current market pricersi(float): RSI (Relative Strength Index) valuemacd_signal(str): MACD signal description (e.g., “Bullish”, “Bearish”)
bool: True if signal sent successfully, False otherwise
Example:
test_connection
bool: True if connection successful, False otherwise
Example:
Helper Functions
create_telegram_bot_url
bot_token(str): Telegram bot token
str: Telegram bot URL (format: https://t.me/bot_username)
Example:
Complete Usage Example
Integration with Alert System
Error Handling
Setting Up Your Telegram Bot
Step 1: Create Bot
- Open Telegram and search for
@BotFather - Send
/newbotcommand - Follow prompts to name your bot
- Copy the bot token provided
Step 2: Get Chat ID
- Start a chat with your bot
- Send any message to the bot
- Visit:
https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates - Find the
chat.idvalue in the JSON response
Step 3: Test Configuration
Best Practices
-
Rate Limiting: Add delays between messages to avoid Telegram API limits
-
Error Handling: Always check return values
-
Test Connection: Verify connection before critical operations
-
Secure Credentials: Store tokens in environment variables
- Message Length: Keep messages under 4096 characters (Telegram limit)