This document covers how SignalTrader integrates with Telegram for signal monitoring, message processing, and notification delivery.
The Telegram integration uses the Telethon library to create a client that monitors specified channels for trading signals and sends notifications about trading activities.
-
Create Telegram Application
- Visit https://my.telegram.org/auth
- Log in with your phone number
- Navigate to "API development tools"
- Create a new application
-
Obtain API Keys
api_id: Integer ID for your applicationapi_hash: String hash for authentication
-
Configure Settings
"Telegram": { "api_id": 12345678, "api_hash": "abcdef1234567890abcdef1234567890" }
-
Create Bot
- Message @BotFather on Telegram
- Use
/newbotcommand - Follow instructions to create your bot
- Save the bot token
-
Get Chat ID
- Start a chat with @RawDataBot
- Send any message to receive your chat ID
-
Configure Notifications
"Notification": { "token": "1234567890:ABCdefGHIjklMNOpqrsTUVwxyz", "chatId": 123456789 }
The bot can monitor specific channels using whitelist/blacklist filtering:
"channels": {
"whiteList": ["trading_signals", "forex_pro"],
"blackList": ["spam_signals", "unreliable_trader"]
}Filtering Logic:
- If whitelist is not empty, only whitelisted channels are monitored
- Blacklist always takes precedence
- Channel usernames without @ symbol
- New Messages: Primary signal detection
- Edited Messages: Signal modifications (SL/TP updates)
- Deleted Messages: Position closures
- Reply Messages: Special commands (edit, delete, risk-free)
- Monitors all new messages in allowed channels
- Parses message content for trading signals
- Extracts: action, symbol, prices, risk levels
Messages that reply to original signals can trigger actions:
- Edit/Update: Modify stop loss or take profit
- Delete/Close: Close positions
- Half: Close half position
- Risk Free: Move SL to entry price
- Checks channel permissions
- Validates message format
- Prevents duplicate processing
-
Startup Notification
- Sent when bot starts
- Includes Jalali datetime
- Confirms successful initialization
-
Trading Notifications
- New position opened
- Position closed/modified
- Error alerts
- Profit/loss updates
-
System Notifications
- Connection issues
- Configuration errors
- Trading restrictions (timer violations)
sys: 2023-11-11 15:47:48 | mt: 2023-11-11 12:17:48
New BUY position opened for EURUSD
- Handles network interruptions
- Automatic retry with exponential backoff
- Maintains session persistence
- FloodWaitError: Rate limiting (automatic delay)
- AuthKeyError: Authentication issues
- NetworkMigrateError: Server switches
- ServerError: Temporary server issues
- Uses
TelegramSessionfor session persistence - Automatic session recovery
- Secure credential storage
- Never share
api_idandapi_hash - Use environment variables for sensitive data
- Regenerate credentials if compromised
- Only monitor trusted signal channels
- Use whitelist for production
- Regularly audit channel permissions
- Respects Telegram's rate limits
- Implements delays between requests
- Handles flood wait errors gracefully
- UTF-8 encoding/decoding
- Unicode normalization
- Emoji and formatting removal
- Persian/Arabic text support
- Generates message links for reference
- Supports both username and ID-based channels
- Creates permanent links to signals
- Supports various chat ID formats
- Handles channel, group, and private chat IDs
- Strips -100 prefixes for supergroups
- All message processing logged
- Channel filtering decisions recorded
- Error conditions captured
- Message processing times
- Connection status
- Rate limiting events
- Official docs: https://docs.telethon.dev/
- Client creation:
TelegramClient() - Event handling:
@client.on(events.NewMessage)
- Main client for Telegram API interaction
- Handles authentication and session management
- Provides async message handling
events.NewMessage: New message detectionevents.MessageEdited: Message modification handlingevents.MessageDeleted: Deletion processing
-
Authentication Failed
- Verify
api_idandapi_hash - Check phone number verification
- Ensure application is not deleted
- Verify
-
Channel Access Denied
- Confirm bot is added to private channels
- Check channel permissions
- Verify channel username spelling
-
Rate Limiting
- Reduce monitoring frequency
- Add delays between operations
- Monitor flood wait errors
-
Message Not Processing
- Check whitelist/blacklist settings
- Verify message format
- Review parsing logs
- Enable verbose logging
- Test with public channels first
- Use @RawDataBot for chat ID verification
- Monitor console output for errors
- Use dedicated Telegram application
- Implement proper error handling
- Monitor bot activity regularly
- Keep credentials secure
- Regularly review monitored channels
- Update whitelist/blacklist as needed
- Monitor signal quality and reliability
- Limit concurrent operations
- Implement proper async handling
- Monitor memory usage
- Clean up old sessions