dtek-telegram-bot

A Telegram bot for viewing outage schedules and subscribing to change notifications.

Environment variables

VariableRequiredDefaultDescription
TELOXIDE_TOKENBot token from @BotFather
DATABASE_URLsqlite:dtek_bot.db?mode=rwcLocal SQLite for subscriptions and snapshots
CACHE_DURATION_MINUTES30Cache TTL in minutes
SCHEDULE_DB_URL(not set)Shared schedule DB path

Commands

CommandDescription
/startWelcome message and help
/helpSame as /start
/groupsShow group selection buttons
/subscribeManage group subscriptions
/myShow schedules for all subscribed groups
/statusCache status and bot info

Local database schema

-- User subscriptions
CREATE TABLE subscriptions (
    id         INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id    INTEGER NOT NULL,
    chat_id    INTEGER NOT NULL,
    group_name TEXT NOT NULL,
    created_at TEXT DEFAULT CURRENT_TIMESTAMP,
    UNIQUE(user_id, group_name)
);

-- Persistent schedule snapshot for change detection
CREATE TABLE schedule_snapshots (
    group_name TEXT PRIMARY KEY,
    data       TEXT NOT NULL,    -- JSON-serialised ScheduleData
    saved_at   INTEGER NOT NULL  -- Unix timestamp
);

Background tasks

The bot runs two background tasks:

Cache refresh — re-fetches schedules from DTEK (or shared DB) every CACHE_DURATION_MINUTES minutes, keeping the in-memory cache warm.

Change detector — every 15 minutes, compares the current schedule with the previous snapshot. When a change is detected for a group, all subscribers receive a notification.

The snapshot is stored in schedule_snapshots (SQLite), so change detection works correctly after a restart — no notifications are missed even if the bot was down when DTEK updated the schedule.

Running

TELOXIDE_TOKEN=123456:ABC-DEF \
DATABASE_URL=sqlite:tg_bot.db \
./dtek-telegram-bot