deusaw/traffic-bot
Lightweight VPS bilateral traffic monitoring Telegram Bot with daily reports, threshold alerts, panel usage sync and calibration.
π¦ Traffic Bot
Lightweight VPS bilateral traffic monitoring Telegram Bot
Designed for VPS providers like BandwagonHost that bill by inbound + outbound combined traffic
π¬π§ English | π¨π³ δΈζ
β¨ Features
| Feature | Description |
|---|---|
| π Daily Report | Auto-push daily digest at your chosen time β yesterday's usage, cycle total, days remaining |
| Real-time alerts at 10%, 20% β¦ 100% usage (checked every 5 minutes) | |
| π Manual Sync | Sync actual usage from your VPS panel, overriding local statistics |
| π Calibration Factor | Set a multiplier to correct systematic drift between vnStat and your panel |
| π Custom Billing Cycle | Supports any reset day (e.g. the 15th), not limited to calendar months |
| π Dual Timezone | Separate timezones for push notifications (your local time) and billing cycle (provider's reset time) β system clock untouched |
| π§Ή Auto Cleanup | Only retains current and previous cycle data β database stays lean |
| π Auth Protection | Responds only to your authorized Chat ID |
π Supported Environments
| Item | Support |
|---|---|
| OS | CentOS 7/8, Ubuntu 18.04/20.04/22.04, Debian 10/11/12 |
| Arch | x86_64 (AMD64), ARM64 (AArch64) |
| vnStat | Auto-adapts to both 1.x (older systems) and 2.x (newer systems) |
π Quick Start
Prerequisites
You'll need two things before deploying:
- Telegram Bot Token β Message @BotFather on Telegram, send
/newbot, and follow the prompts. You'll receive a token like123456789:ABCdefGHIjklMNOpqrsTUVwxyz - Your Chat ID β Message @userinfobot on Telegram, it will reply with your numeric ID
One-Click Deploy
SSH into your VPS as root and run:
git clone https://github.com/deusaw/traffic-bot.git
cd traffic-bot
bash install.shThe script will prompt you for Bot Token and Chat ID, then handle everything else:
Install curl/gcc/git β Install Go compiler β Install & configure vnStat β Compile β Register systemd service β Start
To skip interactive prompts, set environment variables beforehand:
export BOT_TOKEN="your_token"
export CHAT_ID="your_chat_id"
bash install.shOnce deployed, send /start to your Bot on Telegram and follow the 5-step setup wizard.
Updating
When a new version is available, run one command on your VPS:
bash /opt/traffic-bot/update.shPulls latest code β Recompiles β Restarts service. Fully automatic.
π€ Bot Commands
Basic Commands
| Command | Description |
|---|---|
/start |
First-time setup wizard (bandwidth β reset day β push time β push timezone β billing timezone) |
/help |
Show all available commands |
/status |
View current billing cycle status with progress bar and days remaining |
/daily |
View daily upload/download breakdown for the current cycle |
/config |
View current settings (bandwidth, reset day, push time, timezones, calibration factor) |
/report |
Trigger an immediate daily report (don't wait for scheduled time) |
/settings |
Re-enter setup wizard to change bandwidth, reset day, push time, or timezones |
Advanced Commands
| Command | Description |
|---|---|
/sync |
Manually sync panel usage. Enter the actual usage shown on your VPS panel (e.g. 6.81 GB). Bot overwrites local stats and may suggest a calibration factor based on the difference. Reply "yes" to apply |
/sync 6.81 GB |
Same as above, inline shorthand |
/calibrate |
Interactive calibration factor setup, showing current factor and raw vnStat data |
/calibrate 1.25 |
Directly set calibration factor to 1.25 |
/cancel |
Cancel an ongoing /sync or /calibrate interaction |
When do I need
/syncand/calibrate?vnStat and your VPS panel may report different numbers β this is common. For example, your panel shows 100 GB used, but vnStat only reports 80 GB.
- Use
/sync 100 GBto tell the Bot the real value. It will immediately show 100 GB and suggest a factor of 1.25 (= 100 Γ· 80)- Reply "yes" to apply the factor. From then on, vnStat data is automatically multiplied by 1.25 β no more manual syncing needed
π Timezone Configuration
The bot uses two separate timezones (neither changes your system clock):
| Timezone | Purpose | Example |
|---|---|---|
| Push Timezone | When you receive daily reports and alerts (your local time) | Asia/Shanghai |
| Billing Timezone | When the billing cycle resets (your VPS provider's time) | America/New_York |
For example, if you're in China using a BandwagonHost DC9 server (US East Coast):
- Push Timezone:
Asia/Shanghaiβ so10:00means Beijing time 10 AM - Billing Timezone:
America/New_Yorkβ traffic resets at midnight Eastern Time
Common timezone values: UTC, Asia/Shanghai, Asia/Tokyo, America/New_York, America/Los_Angeles, Europe/London
βοΈ Environment Variables
Passed via the systemd service file. The install script configures these automatically. To modify manually:
# Edit the service file
nano /etc/systemd/system/traffic-bot.service
# Reload and restart after changes
systemctl daemon-reload
systemctl restart traffic-bot| Variable | Description | Default |
|---|---|---|
BOT_TOKEN |
Telegram Bot Token | Required |
CHAT_ID |
Authorized Telegram Chat ID (only this ID can operate the Bot) | Required |
INTERFACE |
Network interface to monitor | eth0 (auto-detected by install script) |
DB_PATH |
SQLite database file path | /opt/traffic-bot/traffic-bot.db |
π οΈ Operations
# Check service status
systemctl status traffic-bot
# View live logs (Ctrl+C to exit)
journalctl -u traffic-bot -f
# Restart service
systemctl restart traffic-bot
# Stop service
systemctl stop traffic-bot
# Disable auto-start on boot
systemctl disable traffic-botπ Project Structure
traffic-bot/
βββ main.go # Entry point
βββ config.go # Environment variable loading
βββ db.go # SQLite database operations
βββ vnstat.go # vnStat data collection (auto-adapts 1.x / 2.x)
βββ bot.go # Telegram Bot command handling
βββ scheduler.go # Scheduled tasks (daily report, alert checks, data cleanup)
βββ utils.go # Utilities (formatting, cycle calculation, progress bar)
βββ install.sh # One-click deploy script
βββ update.sh # One-click update script
βββ traffic-bot.service # Systemd service template
βββ go.mod # Go module dependencies
βββ README.md
π License
MIT