Sign inSign up

enesilhaydin/mt-telegram-bot

By enesilhaydin

โ€ขUpdated 4 months ago

Telegram bot for MikroTik: WireGuard, WoL, device & resource monitoring (TR/EN)

Image
0

1.9K

enesilhaydin/mt-telegram-bot repository overview

โ ๐Ÿค– MikroTik Telegram Bot

Manage your MikroTik router from Telegram โ€” WireGuard peers, Wake-on-LAN, live device & resource monitoring, firewall, all from your phone.

Docker Pulls Docker Image Size Docker Version Python Platform RouterOS License i18n


โ โœจ Highlights

  • ๐Ÿ›ก๏ธ WireGuard โ€” spin up peers with auto-generated keys; get the .conf and a scannable QR right in chat
  • โšก Wake-on-LAN โ€” wake any device; pings first so it never WoLs an already-on machine
  • ๐Ÿ“ฒ Device radar โ€” new-device alerts with one-tap Trust ยท Blacklist ยท Name buttons (zero spam)
  • ๐Ÿ”ฌ Diagnostics โ€” /diag, /find, /active, per-interface traffic, one-shot device health
  • ๐Ÿšจ Resource alerts โ€” CPU/RAM/disk thresholds with cause attribution (which subsystem is hogging it)
  • โ›” Blacklist โ€” block a MAC via bridge filter + DHCP block in a single command
  • ๐Ÿท๏ธ Notes โ€” label devices; stored in the bot and the router's DHCP lease comment (WinBox-visible)
  • ๐ŸŒ Bilingual โ€” Turkish & English, hot-switch with /lang
  • ๐Ÿ”‘ Single-admin โ€” locked to one Telegram user ID

โ ๐Ÿš€ Quick Start

โ 1. Get your credentials
NeedHow
BOT_TOKENTelegram โ†’ @BotFather โ†’ /newbot (or /mybots โ†’ API Token)
ADMIN_IDTelegram โ†’ @userinfobot โ†’ start โ†’ copy your numeric Id
โ 2a. Run with Docker (any host that can reach the router API)
docker run -d --name mt-bot --restart unless-stopped \
  -e BOT_TOKEN="123456:AA..." \
  -e ADMIN_ID="123456789" \
  -e ROUTER_IP="192.168.88.1" \
  -e ROUTER_USER="api-user" \
  -e ROUTER_PASS="api-password" \
  -e ROUTER_PORT="8728" \
  -e TZ="Europe/Istanbul" \
  -v mt-bot-data:/app/data \
  enesilhaydin/mt-telegram-bot:latest
โ 2b. Docker Compose
services:
  mt-bot:
    image: enesilhaydin/mt-telegram-bot:latest
    restart: unless-stopped
    environment:
      BOT_TOKEN: "123456:AA..."
      ADMIN_ID: "123456789"
      ROUTER_IP: "192.168.88.1"
      ROUTER_USER: "api-user"
      ROUTER_PASS: "api-password"
      ROUTER_PORT: "8728"
      TZ: "Europe/Istanbul"
    volumes:
      - bot-data:/app/data
volumes:
  bot-data:
โ 2c. Run on the MikroTik itself (RouterOS container)

See MikroTik Container Deploymentโ  below โ€” the bot runs as a container on the router, reaching the API over the internal container network.

Then open Telegram and send /start.


โ ๐ŸŽฎ Commands

โ ๐Ÿ–ฅ๏ธ System
CommandDescription
/systemRouter status (CPU, RAM, uptime, health, version)
/healthInstant resource snapshot (CPU per-core, RAM, HDD, uptime)
/profile2-second CPU profile โ€” which subsystem uses CPU
/jobsCurrently running RouterOS script jobs
/firewallFirewall rules summary
/dnsDNS info (servers, DoH, cache)
/backupCreate router backup
/run <api/path>Run any RouterOS API command
โ ๐Ÿ” WireGuard
CommandDescription
/wg_add <name>Create a peer (auto keys/IP/endpoint) โ†’ sends .conf + QR
/wg_listList all peers
/wg_del [name]Delete a peer (no arg โ†’ buttons)
/qr [name]Resend config + QR for a peer (no arg โ†’ buttons)
/peersPeer status (handshake, RX/TX, endpoint)
โ ๐Ÿ“ก Devices
CommandDescription
/clientsDHCP clients (๐Ÿ“Œ static ยท โœ… trusted ยท ๐Ÿšซ blacklist ยท ๐Ÿ“ note)
/staticDHCP static reservations only
/activeDevices currently on the network (ARP + DHCP). /active scan runs an ARP scan to find devices with manual static IPs
/find <q>Search hostname/IP/MAC/comment
/diag <ip|mac|name>One-shot diagnosis: ping + ARP + lease + flags
/forget <mac>Clean device traces (DHCP lease + ARP + history)
/trafficPer-interface RX/TX
/scanReset new-device alerts
/notify [on|off]Toggle new-device alerts
/note <mac> <text>Name/note a device (saved to bot DB and DHCP lease comment)
/notesList device notes
โ ๐Ÿ›ก๏ธ Trust / Blacklist
CommandDescription
/trust <mac> [name]Mark trusted (suppress alerts)
/untrust <mac> ยท /trust_listRemove / list
/blacklist <mac> [name]Block: bridge filter drop + DHCP block
/unblacklist <mac> ยท /blacklist_listUnblock / list
โ โšก Wake on LAN
CommandDescription
/wol [name]Wake a device (no arg โ†’ buttons). Pings first; skips if already on
/wol_add <name> <mac> [interface]Register a device (no arg โ†’ lists interfaces)
/wol_del <name> ยท /wol_listRemove / list
โ ๐ŸŽ›๏ธ Settings
CommandDescription
/langSwitch language (Turkish / English)
โ ๐Ÿ”” Background monitoring
  • New device detection โ€” scans ARP every 60s; first sighting of a MAC triggers an alert with โœ… Trust ยท ๐Ÿšซ Blacklist ยท โœ๏ธ Name/Note buttons. The Name/Note button starts a reply flow and saves to the bot DB and the router's DHCP lease comment. Reported only once per MAC. Toggle with /notify off.
  • Resource alerts โ€” samples CPU/RAM/HDD; on threshold breach (2 consecutive samples) sends an alert with cause attribution (one-shot /tool profile) + running jobs, 10-minute cooldown, plus a recovery notice.

โ โš™๏ธ Environment Variables

VariableRequiredDefaultDescription
BOT_TOKENโœ…โ€”Telegram bot token (@BotFather)
ADMIN_IDโœ…โ€”Your Telegram user ID (@userinfobot)
ROUTER_PASSโœ…โ€”RouterOS API password
ROUTER_IP172.17.0.1RouterOS API host
ROUTER_USERadminRouterOS API user (read,write,api)
ROUTER_PORT8728RouterOS API port
TZUTCTimezone
WG_INTERFACEwg1WireGuard interface name
WG_SUBNET192.168.2WireGuard subnet prefix
WG_DNSautoDNS pushed to WG clients (set to your LAN gateway)
WG_ENDPOINTautoWAN IP/host for WG endpoint
LAN_SUBNET192.168.1.0/24LAN subnet
MONITOR_INTERVAL60Device scan interval (s)
DB_PATH/app/data/bot.dbSQLite path
CPU_THRESHOLD85CPU % alert threshold
RAM_FREE_MIN_MB50Free RAM (MB) alert floor
HDD_FREE_MIN_MB10Free disk (MB) alert floor
RESOURCE_INTERVAL60Resource sample interval (s)
RESOURCE_COOLDOWN600Min seconds between repeat alerts

A RouterOS API user with only read,write,api (no shell/ssh) is recommended:

/user group add name=botapi policy=read,write,api,test
/user add name=api-user group=botapi password=strong-password

โ ๐Ÿณ MikroTik Container Deployment

โ Prerequisites
  • RouterOS 7.6+ with container support enabled
  • USB/onboard storage for the container
  • API user with read,write,api
  • WireGuard interface configured (for WG features)
โ Setup
# 1. Environment
/container envs add list=mt-bot key=BOT_TOKEN  value="123456:AA..."
/container envs add list=mt-bot key=ADMIN_ID   value="123456789"
/container envs add list=mt-bot key=ROUTER_IP  value="172.17.0.1"
/container envs add list=mt-bot key=ROUTER_USER value="api-user"
/container envs add list=mt-bot key=ROUTER_PASS value="api-password"
/container envs add list=mt-bot key=ROUTER_PORT value="8728"
/container envs add list=mt-bot key=TZ value="Europe/Istanbul"
/container envs add list=mt-bot key=WG_DNS value="192.168.88.1"   # your LAN gateway

# 2. Mounts: persistent DB + (optional) live-code mount โ€” see "Updating code"
/container mounts add name=mt-bot-data src=usb1/container/data/mt-bot dst=/app/data
/container mounts add name=mt-bot-code src=usb1/container/data/mt-bot-code dst=/app/app

# 3. Container (interface = your container veth)
/container add remote-image=enesilhaydin/mt-telegram-bot:latest \
  interface=veth1 envlist=mt-bot mounts=mt-bot-data,mt-bot-code \
  root-dir=usb1/container/mt-bot start-on-boot=yes \
  hostname=mt-bot logging=yes dns=172.17.0.1

# 4. Allow API + internet for the container subnet
/ip service set api address+=172.17.0.0/24
/interface list add name=CONTAINER
/interface list member add list=CONTAINER interface=containers
/ip firewall filter add chain=input action=accept in-interface-list=CONTAINER protocol=udp dst-port=53  comment="IN: CONTAINER DNS"
/ip firewall filter add chain=input action=accept in-interface-list=CONTAINER protocol=tcp dst-port=8728 comment="IN: CONTAINER API"
/ip firewall filter add chain=input action=drop   in-interface-list=CONTAINER comment="IN: CONTAINER drop rest"
/ip firewall filter add chain=forward action=accept src-address=172.17.0.0/24 out-interface-list=WAN comment="FWD: Container -> WAN"

# 5. Start
/container start [find name~"mt-telegram"]

Security: keep the container in its own CONTAINER interface list (not your LAN list) so a compromised container can't reach every router service. Keep 172.17.0.0/24 in the API service's allowed addresses.


โ ๐Ÿงฑ Architecture: volume-mounted code (low-end routers)

On low-power routers (dual-core ARM, USB storage), re-pulling and re-extracting the full image on every code change is slow and can wedge the RouterOS container daemon. Solution: the Python package (app/) is volume-mounted from USB into /app/app, so the image only carries the runtime + dependencies (which rarely change). Updating code = copy .py files to USB + restart the container (~10s). No pull, no extract, no reboot.

  • mt-bot-data โ†’ /app/data (SQLite, persists)
  • mt-bot-code โ†’ /app/app (Python code, overrides the image copy)
โ Updating code (no re-pull)

Copy deploy.sh.example โ†’ deploy.sh, fill in your credentials, then:

./deploy.sh

It uploads app/*.py over FTP (LAN-only, auto-disabled) and restarts the container.

โ Updating dependencies (rare โ€” image rebuild)

Only when requirements.txt changes:

docker buildx build --platform linux/arm/v5 -t <repo>/mt-telegram-bot:latest --push .

Then remove + re-add the container (full re-pull). That's why code lives in a volume mount, not the image.


โ ๐Ÿงช Testing

# Import + i18n integrity (no router needed)
python3 test_smoke.py

# + live read-only router checks
ROUTER_IP=192.168.88.1 ROUTER_USER=api-user ROUTER_PASS=... python3 test_smoke.py

deploy.sh also runs a syntax check on every deploy.


โ ๐Ÿ”จ Build

# ARM/v5 (hEX S, hAP acยฒ)      ARM64 (RB5009, CCR2xxx)        x86_64 (CHR, x86)
docker buildx build --platform linux/arm/v5  -t <repo>/mt-telegram-bot:latest --push .
docker buildx build --platform linux/arm64   -t <repo>/mt-telegram-bot:latest --push .
docker buildx build --platform linux/amd64   -t <repo>/mt-telegram-bot:latest --push .

โ ๐ŸŒ i18n

Turkish & English, toggled with /lang. Strings live in app/i18n.py (TR/EN key parity is verified by test_smoke.py).

โ ๐Ÿ“„ License

CC BY-NC-SA 4.0 โ€” Creative Commons Attribution-NonCommercial-ShareAlike 4.0.

  • โœ… Free to use, modify and share for non-commercial purposes
  • โœ๏ธ Must give credit (attribution)
  • ๐Ÿ” Derivatives must stay under the same license (open)
  • โ›” Commercial / paid use is not permitted without separate written permission

This is source-available, non-commercial software โ€” not OSI "open source" (which requires permitting commercial use). See LICENSEโ  for the full text.

Tag summary

Content type

Image

Digest

sha256:e3361d2aaโ€ฆ

Size

52.7 MB

Last updated

4 months ago

docker pull enesilhaydin/mt-telegram-bot