A lightweight, production-ready Node.js backend service that monitors the health of downstream bots (e.g. Bot B, Bot C, Bot D) via heartbeat pings and alerts you on Telegram if any bot goes silent.
- π°οΈ Multi-User Dynamic Service Registration:
- Add services interactively directly in Telegram via
/newservice. - The bot prompts for Service Name and Service Description.
- Generates a Unique Secret Token (
sec_...) per service. No static shared secret token!
- Add services interactively directly in Telegram via
- π 3-State Health Lifecycle:
- π‘
initialized: Newly registered, awaiting first heartbeat (no false down alerts). - π’
up: Active and reporting regular heartbeats. - π΄
down: Inactivity exceeded timeout threshold (10 minutes).
- π‘
- π Custom Timezone Support:
- Configure your preferred timezone via
/settimezone <IANA Timezone>(e.g.Asia/Kolkata,UTC). - All dashboard and alert timestamps are formatted according to your chosen timezone.
- Configure your preferred timezone via
- π’ Pinned Channel Live Status Board:
- Link a public/private channel via
/setchannel <@channel or ID>. - Automatically posts and pins a comprehensive status board.
- Change-Only Rule: Message is edited only when a service changes status or is added/removed.
- Link a public/private channel via
- π₯ Group Alert Broadcasts:
- Link an alert group via
/setgroup(executed inside the group). - Always posts a new alert message on outage and recovery events.
- Link an alert group via
- π± Direct User Alerts:
- Outage and recovery alerts are always sent directly to the creator's private Telegram DM.
- π₯οΈ Virtualizor VPS 514 Infrastructure Monitoring:
- Live power status, CPU usage %, RAM usage %, disk storage, bandwidth, and API latency.
- Direct Reachability Fallback: Dual-layer verification with direct TCP/HTTP probing (ports 80, 443, 22, 25) preventing false alarms and accidental reboots when the Virtualizor control panel is unreachable.
- Automated 1-minute cron polling on Cloudflare Workers edge.
- Outage & recovery alerts dispatched to Telegram DM and linked groups.
- Interactive bot controls:
/vps,/vps_specs,/start_vps,/restart_vps,/stop_vpsandVPS 514 Livemenu button. - Real-time dark-mode glassmorphic Web UI at
/vpsand/dashboard.
- β‘ Cloudflare Workers, D1 & KV Architecture:
- Zero-maintenance serverless edge hosting with Cloudflare D1 (SQLite), KV (
VIRTUALIZOR_MONITOR_KV), and 1-minute Cron Triggers. - Live Deployment: https://monitor-bot.nubcoders.workers.dev
- Zero-maintenance serverless edge hosting with Cloudflare D1 (SQLite), KV (
βββ index.js # Main Express server, Telegram bot, and health-check loop
βββ package.json # Dependencies and scripts
βββ .env.example # Sample environment variables
βββ .env # Your actual secrets (DO NOT commit)
βββ test-server.js # Automated test suite
βββ test-ping.js # Simulation script to test ping endpoints
βββ README.md # Documentation and setup guide
- Node.js v18 or higher (Node 20+ recommended)
- npm v9 or higher
- A Telegram account
cd /root/Monitor-BOT
npm install-
Create your Telegram Bot:
- Open Telegram and search for @BotFather.
- Send
/newbotand follow the prompts to choose a name and username. - Copy the HTTP API token provided by BotFather (e.g.,
123456789:ABCdefGhIJKlmNoPQRsTUVwxyZ). This is yourTELEGRAM_BOT_TOKEN.
-
Find your Telegram Chat ID:
- Open Telegram and message @userinfobot or @raw_data_bot.
- It will reply with your numeric
Id(e.g.987654321). This is yourMY_CHAT_ID. - Send
/startto your newly created bot so it has permission to message you.
Create your .env file by copying .env.example:
cp .env.example .envOpen .env and configure your settings:
# Telegram Bot Credentials
TELEGRAM_BOT_TOKEN=123456789:ABCdefGhIJKlmNoPQRsTUVwxyZ
MY_CHAT_ID=987654321
# Security Token (Monitored bots must provide this in POST /ping)
SECRET_TOKEN=my-super-secret-random-token-here
# Server Configuration
PORT=3000
HOST=127.0.0.1
# Monitored Bots (Comma-separated)
EXPECTED_BOTS=Bot B,Bot C,Bot D
# Thresholds (Optional customizations)
CHECK_INTERVAL_MS=60000 # Health check interval (1 minute)
TIMEOUT_THRESHOLD_MS=600000 # Inactivity timeout (10 minutes)
ENABLE_RECOVERY_ALERTS=true # Notify when a bot recoversnpm startYou will see:
[Registry] Pre-registered monitored bot: "Bot B"
[Registry] Pre-registered monitored bot: "Bot C"
[Registry] Pre-registered monitored bot: "Bot D"
[Telegram] Bot A successfully connected to Telegram API.
[Health Check] Background loop scheduled to run every 60s (Timeout threshold: 600s)
====================================================
π Bot A Monitoring Server listening on 127.0.0.1:3000
POST http://127.0.0.1:3000/ping
GET http://127.0.0.1:3000/status
====================================================
To test endpoint authentication, outage detection, and recovery logic without sending external Telegram calls:
npm testTo test pinging from another terminal:
npm run test:pingMonitored bots send a heartbeat to this endpoint.
- URL:
/ping - Method:
POST - Headers:
Content-Type: application/json - Body:
{ "bot_name": "Bot B", "secret_token": "my-super-secret-random-token-here" }
200 OK:{ "status": "success", "message": "Heartbeat acknowledged for Bot B", "timestamp": 1726567890123 }403 Forbidden(Invalid or missingsecret_token):{ "error": "Forbidden: Invalid or missing secret_token" }400 Bad Request(Missing or invalidbot_name):{ "error": "Bad Request: \"bot_name\" must be a non-empty string" }
Returns the health status and last check-in times of all bots.
- URL:
/status - Method:
GET
{
"service": "Bot A Monitor",
"uptime_seconds": 320,
"check_interval_ms": 60000,
"timeout_threshold_ms": 600000,
"bots": [
{
"bot_name": "Bot B",
"status": "healthy",
"last_ping": "2026-09-17T10:11:45.852Z",
"minutes_since_ping": 2,
"alert_sent": false
},
{
"bot_name": "Bot C",
"status": "down",
"last_ping": "2026-09-17T09:55:00.000Z",
"minutes_since_ping": 18,
"alert_sent": true
}
]
}How downstream bots can report their heartbeat to Bot A:
curl -X POST http://127.0.0.1:3000/ping \
-H "Content-Type: application/json" \
-d '{"bot_name": "Bot B", "secret_token": "my-super-secret-random-token-here"}'// Add this heartbeat loop in Bot B / C / D
const MONITOR_URL = 'http://127.0.0.1:3000/ping';
const BOT_NAME = 'Bot B';
const SECRET_TOKEN = process.env.MONITOR_SECRET_TOKEN;
async function sendHeartbeat() {
try {
const res = await fetch(MONITOR_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ bot_name: BOT_NAME, secret_token: SECRET_TOKEN })
});
const data = await res.json();
console.log('[Heartbeat sent]:', data.status);
} catch (err) {
console.error('[Heartbeat failed]:', err.message);
}
}
// Send heartbeat every 3 minutes
setInterval(sendHeartbeat, 3 * 60 * 1000);
sendHeartbeat();import time
import requests
MONITOR_URL = "http://127.0.0.1:3000/ping"
BOT_NAME = "Bot C"
SECRET_TOKEN = "my-super-secret-random-token-here"
def send_heartbeat():
try:
payload = {"bot_name": BOT_NAME, "secret_token": SECRET_TOKEN}
r = requests.post(MONITOR_URL, json=payload, timeout=5)
print(f"Heartbeat response: {r.status_code}")
except Exception as e:
print(f"Heartbeat failed: {e}")
while True:
send_heartbeat()
time.sleep(180) # Ping every 3 minutesπ¨ [ALERT] Bot Unresponsive!
π€ Bot: Bot B
β±οΈ Time Since Last Ping: 11 minutes (exceeds 10m threshold)
π Last Seen: Thu, 17 Sep 2026 10:00:00 GMT
β οΈ Action: Please inspect the bot server process or logs immediately.
β
[RECOVERY] Bot Bot B has checked in and is back online!
π Timestamp: Thu, 17 Sep 2026 10:15:20 GMT
Deploy Bot A globally on Cloudflare Workers with Cloudflare D1 (Serverless SQLite) and Cron Triggers:
-
Log in to Cloudflare:
npx wrangler login
-
Create your Cloudflare D1 Database:
npm run db:create
Copy the returned
database_idand paste it intowrangler.jsoncunderd1_databases[0].database_id. -
Initialize the remote database schema:
npm run db:migrate:remote
-
Set Production Secrets on Cloudflare:
npx wrangler secret put SECRET_TOKEN npx wrangler secret put TELEGRAM_BOT_TOKEN npx wrangler secret put MY_CHAT_ID
-
Deploy the Worker:
npm run worker:deploy
-
Connect your Telegram Webhook:
curl -F "url=https://<your-worker-subdomain>.workers.dev/webhook" \ https://api.telegram.org/bot<YOUR_TELEGRAM_BOT_TOKEN>/setWebhook
(For local testing with Wrangler, run npm run worker:dev and node test-worker.js)
- Process Manager: Use PM2 to keep Bot A running continuously:
npm install -g pm2 pm2 start index.js --name "bot-a-monitor" pm2 save pm2 startup - Reverse Proxy & HTTPS: If deploying on a VPS or public server, place Bot A behind Nginx or Caddy with SSL/TLS encryption.