A Telegram bot that fetches ViewLinc sensor data every 60 seconds and provides real-time monitoring through Telegram commands. Runs independently as a systemd service without requiring a Shiny app or browser.
- Direct ViewLinc API integration with 60-second refresh intervals
- Real-time alarm notifications and monitoring
- Device monitoring - Automatic monitoring of VPN server, field routers, and vNETs with 1-hour offline delay before alarms
- Battery forecast alarms - Automatic alerts when batteries predicted to fail within 7 days
- CO2 monitoring - Full support for CO2 parameter with configurable alarm thresholds
- Time-series plotting with on-demand historical data fetching (1d, 3d, 7d, 30d)
- Time-based alarm muting (mute for X days with automatic expiration)
- Telegram command interface for sensor queries
- Runs as a systemd service with auto-restart
- No external dependencies beyond ViewLinc API access
telegram_bot_linux.R- Production bot scriptviewlinc-telegram.service- Systemd service configuration.Renviron.example- Environment variables templateINSTALL.md- Detailed installation guide
- R with packages:
httr,jsonlite,lubridate,grDevices,base64enc - Network access to ViewLinc server
- Telegram bot token and chat ID
Copy .Renviron.example to .Renviron and configure:
TELEGRAM_BOT_TOKEN=your_bot_token
TELEGRAM_ALLOWED_CHAT_IDS=your_chat_id
VIEWLINC_TOKEN=your_viewlinc_token
VIEWLINC_SERVER=https://your-viewlinc-server
BOT_BASE_DIR=/path/to/bot/directory
# Device monitoring hosts
VPN_SERVER=your.vpn.server.com
ROUTER_MARTIGNY=10.0.0.1
ROUTER_SAXON=10.0.0.2
ROUTER_BAGNES=10.0.0.3
ROUTER_DAILLES=10.0.0.4
VNET_MARTIGNY1=192.168.1.1
VNET_MARTIGNY2=192.168.1.2
VNET_SAXON1=192.168.2.1
VNET_SAXON2=192.168.2.2
VNET_VERBIER1=192.168.3.1
VNET_DAILLES1=192.168.4.1See INSTALL.md for detailed setup instructions.
Data & Monitoring:
/status - Display all sensor readings
/alarms - Show active alarms only
/stations - List all available stations
/latest <station> - Show readings for specific station
/battery [station] - Battery forecast (all stations or specific)
/1d <station> <param> - Plot last 1 day of data
/3d <station> <param> - Plot last 3 days of data
/7d <station> <param> - Plot last 7 days of data
/30d <station> <param> - Plot last 30 days of data
Alarm Management:
/mute <site> <param> [Xd] - Mute alarms (optional: for X days)
/muted - List active mutes with time remaining
/clearmute - Clear all muted alarms
System & Utilities:
/ping - Test bot connectivity
/server - Check device status (VPN, routers, vNETs)
/thresholds - Show all parameter alarm thresholds
/help - Display help message
Examples:
/latest Martigny
/battery - Forecast all battery stations
/battery Verbier - Forecast specific station
/7d Saxon volt
/mute "Les Dailles" turb 7d
Check status:
sudo systemctl status viewlinc-telegramView logs:
sudo journalctl -u viewlinc-telegram -fRestart service:
sudo systemctl restart viewlinc-telegramVerify data updates:
ls -lh $BOT_BASE_DIR/data/latest_status.rdsBot fails to start
- Verify using
telegram_bot_linux.Rfor full features - Check all environment variables are set (including
BOT_BASE_DIRandVIEWLINC_SERVER) - Ensure data directory exists and is writable
Telegram 409 error (multiple instances)
sudo systemctl stop viewlinc-telegram
pkill -f telegram_bot_linux.R
sleep 5
sudo systemctl start viewlinc-telegramPlotting fails
- Check that
location_idis being tracked in status data - Verify ViewLinc API access for historical data endpoint
- Check logs for API response details
Data not updating
- Check service status and logs
- Verify data file timestamp
- Test ViewLinc API connectivity
The bot operates three concurrent loops:
- Data refresh: Fetches from ViewLinc API every 60 seconds
- Command polling: Checks Telegram for commands every 2 seconds
- Alarm monitoring: Evaluates and notifies on alarms every 10 minutes
- Sensor parameter alarms (Depth, CDOM, Turbidity, DO, CO2, Conductivity, Temperature, Battery)
- Battery forecast alarms (auto-muted for 24h after notification)
- Device monitoring alarms (VPN, routers, vNETs - only after 1 hour offline)
- Alerts when batteries predicted to reach 10.5V within ≤7 days
- On-demand plotting: Fetches historical data from ViewLinc API when plot commands are used
The bot automatically monitors battery health using linear regression on historical voltage data:
- Forecast calculation: Uses 7 days of 2 AM voltage readings to predict battery depletion
- Automatic alarms: Sends alert when battery predicted to reach 10.5V within 7 days or less
- Auto-mute: Each battery forecast alarm is automatically muted for 24 hours to prevent spam
- Manual check: Use
/batteryto view forecasts for all stations anytime - Trend display: Shows voltage trend (V/day) for declining batteries
Forecast message format:
⚠️ BATTERY FORECAST ALARM
📍 Station: Verbier
🔋 Current: 11.62 V
📉 Trend: -0.015 V/day
⏰ Will reach 10.5V in ~7 days
📅 Estimated: 2026-03-04
The bot automatically monitors network infrastructure health:
- Monitored devices: VPN server, 4 field routers (Martigny, Saxon, Bagnes, Les Dailles), 6 vNETs, ViewLinc VM
- Ping frequency: Every 10 minutes
- Alarm delay: Only sends alarm after device is offline for ≥1 hour (prevents false alarms)
- Offline tracking: Tracks offline duration with timestamps
- Recovery notifications: Sends alert when device comes back online (only if alarm was sent)
- Manual check: Use
/serverto view current status of all devices anytime
Device alarm message format:
🔴 DEVICE ALARM
📍 Device: Martigny Router
❌ Status: OFFLINE for 1.2 hours
Device recovery message format:
✅ DEVICE RECOVERED
📍 Device: Martigny Router
✓ Status: ONLINE
Use /thresholds command to view all alarm thresholds, including:
- Battery Voltage: Alarm <10.8V, Warning 10.8-11.4V, OK ≥11.4V
- CO2: Alarm <150 or >5000 ppm, Warning 3000-5000 ppm, OK 150-3000 ppm
- Dissolved O2: Alarm <0 or >625 µM, Warning <120 or >360 µM, OK 120-360 µM
- Conductivity: Alarm <0 or >1000 µS/cm, Warning <100 or >900 µS/cm, OK 100-900 µS/cm
- Water Temperature: Alarm <0 or >25°C, Warning <0.5 or >20°C, OK 0.5-20°C
- And more...