- Implemented ITelegramNotificationService and TelegramNotificationService for sending alerts via Telegram. - Updated MonitoringSettings to include Telegram configuration options. - Enhanced AlertService to send alerts and resolutions through Telegram. - Added API endpoints for checking Telegram status and sending test alerts. - Updated README and TELEGRAM_SETUP.md with setup instructions and features. - Included example configuration in appsettings.telegram.example.json.
5.0 KiB
Telegram Bot Alert Setup Guide
The Resource Monitor Service supports sending alerts via Telegram bot. This allows you to receive real-time notifications about system resource warnings and critical alerts directly to your Telegram chat.
Prerequisites
-
Create a Telegram Bot:
- Open Telegram and search for
@BotFather - Send
/newbotcommand - Follow the instructions to create a new bot
- Save the Bot Token (format:
123456789:ABCdefGHIjklMNOpqrSTUvwxyz)
- Open Telegram and search for
-
Get Your Chat ID:
- Send a message to your bot
- Visit:
https://api.telegram.org/bot<YourBOTToken>/getUpdates - Find your chat ID in the response (it's a number, can be negative for groups)
Configuration
1. Edit appsettings.json
Add or update the Telegram configuration in your appsettings.json:
{
"MonitoringSettings": {
// ... other settings ...
"Telegram": {
"IsEnabled": true,
"BotToken": "123456789:ABCdefGHIjklMNOpqrSTUvwxyz",
"ChatIds": [12345678, -987654321],
"SendWarningAlerts": true,
"SendCriticalAlerts": true,
"SendResolutionNotifications": true,
"MessageTemplate": "🚨 *{Level} Alert*\n\n📊 *{Component}*\n💬 {Message}\n⏰ {Timestamp}",
"ResolutionTemplate": "✅ *Alert Resolved*\n\n📊 *{Component}*\n💬 {Message}\n⏰ Resolved at {ResolvedAt}"
}
}
}
2. Configuration Options
| Setting | Description | Default |
|---|---|---|
IsEnabled |
Enable/disable Telegram notifications | false |
BotToken |
Your Telegram bot token from BotFather | "" |
ChatIds |
Array of chat IDs to send alerts to | [] |
SendWarningAlerts |
Send warning level alerts | true |
SendCriticalAlerts |
Send critical level alerts | true |
SendResolutionNotifications |
Send alert resolution notifications | true |
MessageTemplate |
Template for alert messages | See above |
ResolutionTemplate |
Template for resolution messages | See above |
3. Message Templates
Templates support the following placeholders:
{Level}- Alert level (Warning, Critical){Component}- Component name (CPU, Memory, GPU, etc.){Message}- Full alert message{Timestamp}- Alert timestamp{CurrentValue}- Current resource value{ThresholdValue}- Threshold that was exceeded{ResolvedAt}- Resolution timestamp (resolution template only)
API Endpoints
Check Telegram Status
GET /api/telegram/status
Returns the current status of Telegram integration:
{
"enabled": true,
"connected": true
}
Send Test Alert
POST /api/telegram/test
Sends a test alert to verify the Telegram bot is working correctly.
Features
Alert Types
The bot sends different types of alerts:
-
Warning Alerts ⚠️
- Sent when thresholds are exceeded but not critical
- Notifications are silent (no sound/vibration)
-
Critical Alerts 🔴
- Sent when critical thresholds are exceeded
- Normal notifications (with sound/vibration)
-
Resolution Notifications ✅
- Sent when alerts are resolved
- Always silent notifications
Icons and Formatting
The bot automatically adds relevant icons:
- 🖥️ CPU usage
- 🌡️ Temperature alerts
- 💾 Memory usage
- 🎮 GPU usage
- 💽 Disk usage
- ⚙️ Process alerts
Messages are formatted using Telegram's Markdown formatting for better readability.
Multiple Chat Support
You can send alerts to multiple chats:
- Personal chats
- Group chats
- Channels (if bot is admin)
Just add multiple chat IDs to the ChatIds array.
Troubleshooting
Common Issues
-
Bot not responding:
- Verify bot token is correct
- Ensure bot is not blocked
- Check
/api/telegram/statusendpoint
-
Messages not received:
- Verify chat ID is correct
- Ensure you've sent at least one message to the bot
- Check bot has permission to send messages
-
Connection errors:
- Check internet connectivity
- Verify Telegram API is accessible
- Check firewall settings
Testing
- Set
IsEnabled: truein configuration - Restart the service
- Call
POST /api/telegram/testto send a test message - Check if the message is received in Telegram
Logs
Monitor the service logs for Telegram-related errors:
grep -i telegram logs/resourcemonitor-*.txt
Security Considerations
- Keep bot token secure - Never commit it to version control
- Use environment variables for sensitive configuration in production
- Limit chat IDs to trusted users/groups only
- Regular token rotation if compromised
Example Alert Messages
Warning Alert
⚠️ 🖥️ 🚨 Warning Alert
📊 CPU
💬 CPU Usage is warning: 85.2% (threshold: 80.0%)
⏰ 2025-08-07 14:30:15
Critical Alert
🔴 💾 🚨 Critical Alert
📊 Memory
💬 Memory Usage is critical: 96.8% (threshold: 95.0%)
⏰ 2025-08-07 14:35:22
Resolution
✅ 🖥️ Alert Resolved
📊 CPU
💬 CPU Usage is warning: 85.2% (threshold: 80.0%)
⏰ Resolved at 2025-08-07 14:32:45