# alexwbaule/telegramopenwrt architecture

> Architecture diagram of the GitHub repository alexwbaule/telegramopenwrt, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/alexwbaule/telegramopenwrt
- Repository: https://github.com/alexwbaule/telegramopenwrt

## Overview

##### Project type and purpose
- This is an open-source, plugin-based toolkit written in Bash for managing and retrieving information from OpenWrt routers over Telegram. It provides both:
- An event-driven “watcher” service (e.g., lanports, hosts_scan) that monitors router state and proactively notifies via Telegram.
- A polling-based “command processor” service (telegram_bot) that receives user commands from Telegram, dispatches them to plugins, and returns results.

##### File structure
- Top-level directories:
- etc/config/telegramopenwrt – UCI-style config (bot token, chat ID, settings)
- etc/init.d – SysV init scripts to launch services at boot (lanports, telegram_bot, hosts_scan)
- etc/telegramopenwrt/macaddr.ignore – ignore-list data file
- sbin/ – executable scripts installed into /sbin on the router (lanports, telegram_bot, telebot/telegram_sender, hosts_scan, etc.)
- usr/lib/telegramopenwrt/plugins/ – plugin API:
– actions/ – scripts that perform work in response to a user or context choice
– ctx/ – context scripts that gather user input and invoke actions
– help/ – static help text per command
– top-level plugin scripts (get_ip, fw_add, wifi_list, etc.)

##### Architecture insights from README
- Two principal services:
- lanports (and other init.d watchers) → monitors logread, DHCP events → invokes telebot/telegram_sender to send alerts
- telegram_bot → loops getUpdates → matches incoming commands → locates plugin in usr/lib/... → executes plugin → sends response via Telegram API
- Plugin design: each command has an optional ctx (for multi-step interaction) and action script, plus a help file for documentation.

a. Main components
- Configuration Store (etc/config/telegramopenwrt + macaddr.ignore)
- Init system (OpenWrt init.d)
- Event Watcher Services (lanports, hosts_scan)
- Command Poller Service (telegram_bot)
- Plugin Dispatcher & Plugins (usr/lib/telegramopenwrt/plugins/{ctx,actions,help,commands})
- Telegram API (external HTTPS endpoint)
- Router Subsystems (network interface, firewall/iptables, UCI, cameras, TV, file system)
b. Relationships and data flows
1. At boot, init.d registers & starts watchers and poller
2. Event Watchers → read logs (logread -f), DHCP leases → format alert → call telebot/telegram_sender → HTTP POST → Telegram API
3. Command Poller (telegram_bot) → HTTPS GET /getUpdates → receives update → invokes Plugin Dispatcher → plugin script interacts with Router Subsystems (uci, iptables, netstat, camera CLI, etc.) → returns output → HTTP POST /sendMessage → Telegram API → user
c. Architectural patterns
- Plugin architecture (loose coupling via filesystem)
- Producer/consumer: watchers produce alerts, poller consumes commands
- Context‐action pattern: ctx scripts for multi‐step UI, actions for atomic operations
d. Key technologies & libraries
- Bash scripting, OpenWrt UCI, logread, iptables, curl (HTTPS), coreutils
- Telegram Bot API (getUpdates, sendMessage endpoints)

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "Configuration Store"
        C1["etc/config/telegramopenwrt"]:::config
        C2["etc/telegramopenwrt/macaddr.ignore"]:::config
    end

    subgraph "Init System (OpenWrt init.d)"
        I1["etc/init.d/lanports"]:::service
        I2["etc/init.d/hosts_scan"]:::service
        I3["etc/init.d/telegram_bot"]:::service
    end

    subgraph "Event Watcher Services"
        W1["sbin/lanports"]:::service
        W2["sbin/hosts_scan"]:::service
    end

    subgraph "Command Poller Service"
        P1["sbin/telegram_bot"]:::service
        P2["sbin/telebot (framework)"]:::util
        P3["sbin/telegram_sender"]:::util
    end

    subgraph "Plugin Dispatcher & Plugins"
        PD["usr/lib/telegramopenwrt/plugins"]:::plugin
        subgraph "Actions"
            A["actions/"]:::plugin
        end
        subgraph "Context (ctx)"
            CTX["ctx/"]:::plugin
        end
        subgraph "Help Text"
            H["help/"]:::plugin
        end
        subgraph "Commands"
            CMD1["get_ip"]:::plugin
            CMD2["fw_add"]:::plugin
            CMD3["wifi_list"]:::plugin
        end
    end

    subgraph "Router Subsystems"
        RS1["UCI"]:::service
        RS2["iptables/firewall"]:::service
        RS3["Network Interfaces"]:::service
        RS4["Camera CLI"]:::service
        RS5["File System"]:::service
    end

    TELE["Telegram API (HTTPS)"]:::external

    %% Data Flows
    C1 --> I1
    C1 --> I2
    C1 --> I3
    C2 --> I1
    C2 --> I2
    C2 --> I3

    I1 --> W1
    I2 --> W2
    I3 --> P1

    W1 -->|"logread -f / DHCP"| P3
    W2 -->|"logread -f"| P3

    P1 -->|"getUpdates"| TELE
    TELE -->|"updates"| P1

    P1 -->|"dispatch"| PD

    PD --> A
    PD --> CTX
    PD --> H
    PD --> CMD1
    PD --> CMD2
    PD --> CMD3

    CMD1 --> RS1
    CMD1 --> RS3
    CMD2 --> RS2
    CMD3 --> RS3
    PD --> RS4
    PD --> RS5

    P1 -->|"response"| P3
    P3 -.->|"sendMessage"| TELE

    %% Click Events
    click C1 "https://github.com/alexwbaule/telegramopenwrt/tree/master/etc/config/telegramopenwrt"
    click C2 "https://github.com/alexwbaule/telegramopenwrt/blob/master/etc/telegramopenwrt/macaddr.ignore"
    click I1 "https://github.com/alexwbaule/telegramopenwrt/tree/master/etc/init.d/lanports"
    click I2 "https://github.com/alexwbaule/telegramopenwrt/tree/master/etc/init.d/hosts_scan"
    click I3 "https://github.com/alexwbaule/telegramopenwrt/tree/master/etc/init.d/telegram_bot"
    click W1 "https://github.com/alexwbaule/telegramopenwrt/tree/master/sbin/lanports"
    click W2 "https://github.com/alexwbaule/telegramopenwrt/tree/master/sbin/hosts_scan"
    click P1 "https://github.com/alexwbaule/telegramopenwrt/tree/master/sbin/telegram_bot"
    click P2 "https://github.com/alexwbaule/telegramopenwrt/tree/master/sbin/telebot"
    click P3 "https://github.com/alexwbaule/telegramopenwrt/tree/master/sbin/telegram_sender"
    click PD "https://github.com/alexwbaule/telegramopenwrt/tree/master/usr/lib/telegramopenwrt/plugins"
    click A "https://github.com/alexwbaule/telegramopenwrt/tree/master/usr/lib/telegramopenwrt/plugins/actions/"
    click CTX "https://github.com/alexwbaule/telegramopenwrt/tree/master/usr/lib/telegramopenwrt/plugins/ctx/"
    click H "https://github.com/alexwbaule/telegramopenwrt/tree/master/usr/lib/telegramopenwrt/plugins/help/"
    click CMD1 "https://github.com/alexwbaule/telegramopenwrt/tree/master/usr/lib/telegramopenwrt/plugins/get_ip"
    click CMD2 "https://github.com/alexwbaule/telegramopenwrt/tree/master/usr/lib/telegramopenwrt/plugins/fw_add"
    click CMD3 "https://github.com/alexwbaule/telegramopenwrt/tree/master/usr/lib/telegramopenwrt/plugins/wifi_list"

    %% Styles
    classDef config fill:#f9f,stroke:#333,stroke-width:1px
    classDef service fill:#bbf,stroke:#333,stroke-width:1px
    classDef plugin fill:#bfb,stroke:#333,stroke-width:1px
    classDef external fill:#fb3,stroke:#333,stroke-width:1px
    classDef util fill:#ccc,stroke:#333,stroke-width:1px
```
