4.2 KiB
VK Activity Monitor
A service for monitoring the online status of VKontakte users with notifications via a VK bot and detection of "sync overlaps" — periods when two or more monitored users are online simultaneously. 📋 Features
Track online / offline status for a list of VK users.
Detect the app the user is connecting from (web-pc, android, ios, ipad, etc.).
Log events with timestamps to a file and stdout.
Instant notifications via a VK bot on status changes.
Detect "sync overlaps" — periods when 2+ monitored users are online at the same time, with duration calculation.
Uptime tracking for each session.
Flexible configuration via environment variables (.env).
🛠 Requirements
Python 3.10+ (uses list | None syntax).
Dependencies from requirements.txt:
bash
pip install -r requirements.txt
Main packages:
vk_api — VK API client.
python-dotenv — loads variables from .env.
⚙️ Configuration
Create a .env file in the project root (see .env.example): env
VK user token (for reading statuses)
VK_TOKEN=your_user_access_token
VK community/bot token (for sending notifications)
BOT_TOKEN=your_group_bot_token
Chat/user ID to send notifications to
NOTIFY_PEER_ID=123456789
Enable/disable notifications
NOTIFY_ENABLED=true
Comma-separated list of user IDs or screen names
USER_IDS=durov,id123456,some_nickname
Log file
LOG_FILE=online_log.txt
Polling interval in seconds
CHECK_INTERVAL=3
Obtaining tokens
VK_TOKEN (user token) — via vkhost.github.io or a VK Standalone app. Requires the users permission (read status).
BOT_TOKEN — create a VK community → Manage → API usage → Create key. Then enable message sending under Long Poll API / Messages.
NOTIFY_PEER_ID — user ID or conversation ID where the bot will post. For a conversation: 2000000000 + chat_id.
Parameters Variable Type Default Description VK_TOKEN str — Required. VK user token. BOT_TOKEN str — Bot token for notifications. NOTIFY_PEER_ID int 0 Notification recipient ID. NOTIFY_ENABLED bool true Enable notification sending. USER_IDS list — Required. List of IDs/screen names. LOG_FILE str online_log.txt Path to the log file. CHECK_INTERVAL int 3 VK API polling interval in seconds. 🚀 Running bash
python vk-activity.py
Stop with Ctrl+C. On shutdown, service stop is written to the log. 🧠 How it works
Initialization: loads .env, validates tokens, initializes the bot.
Target resolution: converts USER_IDS to numeric IDs, caches names.
Initial state: a single users.get request to fetch current online and online_app.
Poller: a separate thread polls users.get every CHECK_INTERVAL seconds:
on status change, writes to the log and sends a notification;
when online, records the session start time; when offline, computes uptime;
on any status change, calls check_intersection().
Overlaps: when 2+ users are online simultaneously, a sync overlap window starts; when it ends, the duration is recorded.
Log format text
[2026-10-11 14:32:01] service start [2026-10-11 14:32:01] targets: 3 [2026-10-11 14:32:01] interval: 3s [2026-10-11 14:32:02] auth ok [2026-10-11 14:32:02] watching: node-1, node-2, node-3 [2026-10-11 14:32:03] state node-1 -> idle [web-pc] [2026-10-11 14:32:05] heartbeat node-2 up [android] [2026-10-11 14:32:05] heartbeat node-3 up [ios] [2026-10-11 14:32:05] sync overlap detected: 2 nodes [node-2, node-3] [2026-10-11 14:32:05] window start: 14:32:05 [2026-10-11 14:35:22] heartbeat node-2 down (uptime 3.3 min, android) [2026-10-11 14:35:22] sync overlap closed: 3.3 min
📦 Project structure text
vk-activity/ ├── .env # local variables (not in git) ├── .env.example # variables template ├── .gitignore ├── requirements.txt ├── vk-activity.py # main script └── online_log.txt # log (created automatically)
⚠️ Notes
VK API has rate limits. Don't set CHECK_INTERVAL too low (below 2 sec) — you may hit Flood control.
The user token has a limited lifetime; when it expires, reissue it and update .env.
The script is designed for long-running operation. For production, run it via systemd or supervisor.
📄 License
MIT