-
Notifications
You must be signed in to change notification settings - Fork 0
Telegram Bot
Andrea Gigante edited this page Oct 12, 2025
·
1 revision
Complete guide to setting up and using the SafeHarbor Telegram monitoring bot for real-time VPN status notifications.
The SafeHarbor Telegram bot provides:
- Real-time VPN monitoring with instant notifications
- Interactive commands for status checking
- DNS leak detection with automatic alerts
- Connection failure alerts when VPN drops
- IP change notifications when VPN server changes
- Open Telegram and search for @BotFather
-
Send
/newbotcommand -
Choose a name:
SafeHarbor VPN Monitor(display name) -
Choose username:
safeharbor_vpn_bot(must end with 'bot') -
Save the Bot Token:
1234567890:ABCdefGHIjklMNOpqrsTUVwxyZ
- Search for @userinfobot
- Start a chat and send any message
- Copy your Chat ID:
123456789
# Replace YOUR_BOT_TOKEN with your actual token
curl "https://api.telegram.org/botYOUR_BOT_TOKEN/getUpdates"
# First send a message to your bot, then run the command above
# Look for "chat":{"id":123456789} in the responseAdd to your .env file:
# Telegram Configuration
TELEGRAM_BOT_TOKEN=1234567890:ABCdefGHIjklMNOpqrsTUVwxyZ
TELEGRAM_CHAT_ID=123456789
KEEPALIVE_API_KEY=your-secure-random-key-here# Test bot communication
curl -X POST "https://api.telegram.org/bot1234567890:ABCdefGHIjklMNOpqrsTUVwxyZ/sendMessage" \
-d "chat_id=123456789&text=SafeHarbor setup test - bot is working!"# Restart the keepalive server to load Telegram config
docker compose restart keepalive-server
# Check logs
docker compose logs -f keepalive-serverExpected startup logs:
Starting Keepalive Server...
Telegram bot polling started
β
VPN Keepalive Server Started
Server listening on port 5000
| Command | Description | Response |
|---|---|---|
/ping |
Test bot connectivity | Server status with uptime info |
/status |
Get current VPN status | Detailed client status report |
/help |
Show available commands | Command list with descriptions |
| Any other text | Unknown command/text | Friendly response with guidance |
User: /ping
Bot: π Pong!
Server time: 2025-10-12 15:30:22 CEST
Active clients: 1
Alert threshold: 5 minutes
Check interval: 5 minutes
User: /status
Bot: π VPN Status
π’ synology-vpn-media - Online
IP: 185.170.104.53
Last seen: 2 minutes ago
Time: 15:28:15 CEST
Server time: 15:30:22 CEST
User: /help
Bot: β VPN Keepalive Bot Help
Available commands:
π /ping - Test bot connectivity and get server info
π /status - Get detailed VPN client status
β /help - Show this help message
Automatic notifications:
β
VPN connection established
π VPN IP address changes
β VPN connection lost (after 5 minutes)
π DNS leak test results
Monitoring every 5 minutes
User: hello
Bot: π Hello! I'm your VPN monitoring bot.
I received: hello
Use /help to see available commands.
Available commands:
π /ping - Test connectivity
π /status - Get VPN status
β /help - Show help
π¨ VPN CONNECTION LOST! π¨
Client: synology-vpn-media
Time: 2025-10-12 15:30:15 (Europe/Madrid)
External IP: 79.117.62.134 (Real IP detected!)
Location: Spain, Madrid
ISP: Your Internet Provider
DNS Servers: 8.8.8.8, 1.1.1.1 (DNS leak detected!)
β οΈ Media services are now exposed!
β
VPN CONNECTION RESTORED β
Client: synology-vpn-media
Time: 2025-10-12 15:35:22 (Europe/Madrid)
External IP: 185.72.199.129 (VPN IP)
Location: Poland, Kujawsko-Pomorskie, ToruΕ
ISP: PrivateVPN
DNS Servers: VPN DNS (secure)
π‘οΈ All traffic now protected via VPN
π VPN IP ADDRESS CHANGED π
Client: synology-vpn-media
Time: 2025-10-12 16:15:30 (Europe/Madrid)
Previous IP: 185.170.104.53 (Poland)
New IP: 185.72.199.129 (Poland)
Location: ToruΕ, Kujawsko-Pomorskie, PL
DNS Status: Secure (no leak detected)
β οΈ NO VPN CLIENTS REPORTING β οΈ
Time: 2025-10-12 15:40:00 (Europe/Madrid)
Last client seen: 8 minutes ago
Status: All clients appear offline
Check your VPN containers immediately!
π DNS LEAK DETECTED! π
Client: synology-vpn-media
Time: 2025-10-12 15:25:10 (Europe/Madrid)
VPN Location: Poland (PL)
DNS Location: United States (US)
β οΈ Your DNS requests may be exposed!
# Required
TELEGRAM_BOT_TOKEN=1234567890:ABCdefGHIjklMNOpqrsTUVwxyZ
TELEGRAM_CHAT_ID=123456789
# Optional
KEEPALIVE_API_KEY=secure-random-key # API authentication
ALERT_THRESHOLD_MINUTES=5 # Alert delay (default: 5)
CHECK_INTERVAL_MINUTES=5 # Check frequency (default: 5)
TZ=Europe/Madrid # Timezone for notifications# Alert after 10 minutes instead of 5
ALERT_THRESHOLD_MINUTES=10# Check every 2 minutes instead of 5
CHECK_INTERVAL_MINUTES=2# Set timezone for notifications
TZ=America/New_York # Eastern Time
TZ=Europe/London # GMT/BST
TZ=Asia/Tokyo # JST
TZ=UTC # UTC time# Test token validity
curl "https://api.telegram.org/bot1234567890:ABCdefGHIjklMNOpqrsTUVwxyZ/getMe"
# Should return bot information
{
"ok": true,
"result": {
"id": 1234567890,
"is_bot": true,
"first_name": "SafeHarbor VPN Monitor",
"username": "safeharbor_vpn_bot"
}
}# Check recent messages
curl "https://api.telegram.org/bot1234567890:ABCdefGHIjklMNOpqrsTUVwxyZ/getUpdates"
# Look for your chat ID in the response# View Telegram-related logs
docker compose logs keepalive-server | grep -i telegram
# Expected patterns:
# β
Telegram message sent successfully
# π₯ Telegram command received: /ping
# Telegram bot polling started- Verify bot token is correct
- Ensure bot was created successfully via @BotFather
- Check for extra spaces or characters in token
- Verify chat ID is correct
- Send a message to the bot first (to start the chat)
- Use positive number for chat ID (not negative)
# Test direct message sending
curl -X POST "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage" \
-d "chat_id=$TELEGRAM_CHAT_ID&text=Test message"
# Check for network connectivity
docker exec keepalive-server curl -s https://api.telegram.org# Restart bot polling
docker compose restart keepalive-server
# Check if bot is polling
docker compose logs keepalive-server --tail 20[2025-10-12 15:30:26 CEST] π₯ Telegram command received: /status
[2025-10-12 15:30:26 CEST] β
Telegram message sent successfully
[2025-10-12 15:30:26 CEST] β
Status response sent
[2025-10-12 15:30:26 CEST] β Telegram polling error: HTTPSConnectionPool
[2025-10-12 15:30:26 CEST] β Failed to send Telegram message: Bad Request
- Never share your bot token publicly
- Use environment variables instead of hardcoding
- Regenerate token if compromised via @BotFather
- Chat ID is not sensitive but keep it private
- Only your specific chat will receive notifications
- Bot ignores messages from other users
- Bot communicates over HTTPS with Telegram API
- No sensitive data transmitted (only status information)
- API key protects keepalive endpoints
- Push notifications for instant VPN alerts
- Reply from notification to send bot commands
- Group chat support (add bot to group for team monitoring)
- Message history to track VPN status over time
Set up Telegram shortcuts for common commands:
- Open chat with bot
- Type
/to see command suggestions - Tap command for instant execution
The bot can monitor multiple VPN clients simultaneously:
π VPN Status
π’ client-1 - Online
IP: 185.170.104.53
Last seen: 1 minute ago
π’ client-2 - Online
IP: 91.236.55.128
Last seen: 2 minutes ago
π΄ client-3 - Offline
Last seen: 8 minutes ago
Modify keepalive-server.py to add custom notification types:
- Bandwidth usage alerts
- Download completion notifications
- Service restart alerts
- System resource warnings
Next: Learn about VPN Monitoring features and DNS leak detection.