Back to blog

MikroTik / RouterOS  ·  Monitoring  ·  November 2026

MikroTik
Telegram Alerts

A RouterOS box can send its own alerts to your phone — no collector, no middleware. /tool fetch POSTs to the Telegram Bot API, and from there any netwatch, scheduler, or script fires a message. The trick is to write the notifier once, as a reusable function, then wire triggers to it. The three things that quietly stop it working aren't code — they're device-mode, TLS trust, and a token you must keep out of your configs. For the durable, filtered record see Logging & Remote Syslog; for graphs and thresholds over time, MikroTik + Prometheus.

MikroTik Telegram Alerting Netwatch RouterOS

On-device alerting answers a narrow question: something just changed on this router and no one is watching the dashboard. A dropped WAN at 02:00, a CPU pegged at 100%, a temperature climbing in a hot cabinet. You don't need a monitoring stack for that — RouterOS can reach out to Telegram itself with one HTTPS request.

This is complementary to a real collector, not a replacement. Telegram push is for the handful of events a human should see now; syslog and metrics are for the full record and the trends. Think of the router as the fallback alarm when the operator box is unreachable — or when there is no operator box at all.

Prerequisites
EVENT netwatch · poll $tgSend function /tool fetch HTTPS POST TELEGRAM Bot API → phone JSON body · chat_id + text

01

Write the Notifier Once

Register a global function $tgSend. Every trigger calls it with a string. It builds the Bot API request, escapes the body, and sends it — so the Telegram plumbing lives in exactly one place.

RouterOS — a reusable $tgSend, armed by a script you run once
# Secrets come from globals you set elsewhere (see §2) — never hardcode them.
[admin@edge] > /system/script/add name=tg-init dont-require-permissions=yes \
   policy=read,write,test source={
    :global tgToken; :global tgChat
    :global tgSend do={
      :global tgToken; :global tgChat
      :local m [:tostr $1]
      # JSON-escape the message body: backslash, double-quote, newline
      :local e ""
      :for i from=0 to=([:len $m] - 1) do={
        :local c [:pick $m $i]
        :if ($c = "\\") do={ :set e ($e . "\\\\") } else={
        :if ($c = "\"") do={ :set e ($e . "\\\"") } else={
        :if ($c = "\n") do={ :set e ($e . "\\n")  } else={ :set e ($e . $c) }}}
      }
      :local url ("https://api.telegram.org/bot" . $tgToken . "/sendMessage")
      :local body ("{\"chat_id\":\"" . $tgChat . "\",\"text\":\"" . $e . "\"}")
      :do {
        /tool/fetch http-method=post url=$url check-certificate=yes-without-crl \
          http-header-field="Content-Type: application/json" \
          http-data=$body output=none as-value
      } on-error={ :log warning "tgSend: fetch failed" }
    }
  }
[admin@edge] > /system/script/run tg-init   # arm it (globals live until reboot)
💡 Why a function, and why POST

Globals in RouterOS are lost on reboot, so $tgSend has to be re-registered at startup — a one-line /system/scheduler with start-time=startup running tg-init does it. And prefer POST with a JSON body over a GET query string: the message rides in the body, so you escape for JSON (three characters) instead of URL-encoding every space and symbol in the text. The small :for loop above is the entire escaper.

02

The Three Things That Silently Break It

None of these throw a clear error. The fetch just fails, or never runs. Clear all three before you wire any trigger.

RouterOS — device-mode, the clock, and the secret
# (1) DEVICE-MODE: fetch + scheduler are gated off by default on RouterOS 7.13+.
#     This prints a prompt that needs a COLD power-cycle / reset-button press
#     physically at the device — by design, so no one arms it remotely.
[admin@edge] > /system/device-mode/update fetch=yes scheduler=yes

# (2) CLOCK: TLS cert validation fails on a wrong date. NTP by IP so it
#     doesn't depend on DNS. Confirm status: synchronized before testing fetch.
[admin@edge] > /system/ntp/client/set enabled=yes servers=10.0.0.1

# (3) SECRET: set the token/chat as globals from a file you DON'T commit,
#     not inline in your exported config. Keep them out of version control.
[admin@edge] > :global tgToken "123456:AA...bot-token..."
[admin@edge] > :global tgChat "111111111"
⚠ Gotcha — RouterOS Ships No Root CAs

Out of the box there is no trusted CA store, so an HTTPS /tool/fetch to api.telegram.org fails certificate verification and your alert silently never sends. Two honest choices: import the CA chain (/certificate import) and keep check-certificate=yes-without-crl — the right answer for anything production — or set check-certificate=no, which works instantly but sends your bot token over a connection whose server identity you never verified. Don't ship no to a client site; it's a lab shortcut. Either way, a wrong clock defeats a valid cert, so fix NTP first.

03

Trigger 1 — WAN Up/Down via Netwatch

Netwatch pings a host and runs a script on each state change — not on every probe. That edge-triggering is exactly what you want: one message when the link drops, one when it comes back.

RouterOS — alert only on the transition
# host = what "WAN up" means to you. 1.1.1.1 tests real reachability;
# your ISP gateway tests the link only. up/down scripts call the function.
[admin@edge] > /tool/netwatch/add name=wan-tg host=1.1.1.1 type=simple \
   interval=10s timeout=2s \
   up-script=":global tgSend; \$tgSend (\"WAN UP - 1.1.1.1 reachable\")" \
   down-script=":global tgSend; \$tgSend (\"WAN DOWN - 1.1.1.1 unreachable\")"
💡 Pro Tip — Watch the Internet, Not Your Own Gateway

If you point host at the next-hop gateway, you'll catch a dead cable but miss the far more common failure: the link is up but the internet is gone (upstream outage, PPPoE down, CGNAT black hole). Ping something past your ISP — a public resolver — to alert on reachability. For a dual-WAN box, set src-address to pin the probe to the uplink you actually want to test, so a healthy backup doesn't mask a failed primary.

04

Trigger 2 — CPU & Temperature, Polled

There's no event for "CPU got busy," so you poll on a scheduler. The discipline that makes it usable is hysteresis: alert once when it crosses high, once when it recovers — never a message per tick.

RouterOS — threshold crossing with a per-metric flag
# A script the scheduler runs every minute. The flag turns a level into an edge.
[admin@edge] > /system/script/add name=tg-health dont-require-permissions=yes \
   policy=read,test source={
    :global tgSend; :global tgCpuHot
    :local cpu [/system/resource/get cpu-load]
    :if ($cpu > 90) do={
      :if ($tgCpuHot != true) do={
        $tgSend ("CPU HIGH - " . $cpu . "%"); :set tgCpuHot true }
    } else={
      :if ($cpu < 70 and $tgCpuHot = true) do={
        $tgSend ("CPU recovered - " . $cpu . "%"); :set tgCpuHot false }
    }
    # Temperature only if the board has a sensor (many small hAPs don't).
    :if ([:len [/system/health/find where name=temperature]] > 0) do={
      :local t [/system/health/get [find where name=temperature] value]
      :if ($t > 70) do={ $tgSend ("TEMP HIGH - " . $t . "C") }
    }
  }
[admin@edge] > /system/scheduler/add name=tg-health interval=1m \
   on-event="/system/script/run tg-health" policy=read,test
⚠ Gotcha — A Bare Threshold Floods You at the Boundary

Without the flag, a CPU hovering at 89–91% sends an alert every single poll as it jitters across the line. The fix is two thresholds: fire at > 90, and only re-arm once it drops back under 70 (tgClear). That gap is the hysteresis band — the same idea as a thermostat's deadband. It's also why temperature needs its own flag, not a shared one: a hot CPU and a hot board are different alarms. And guard the temperature read with find where name=temperature — calling get on a board with no sensor errors out and kills the whole script.

05

Verify Safe to Run

RouterOS — prove each layer before you trust it
# 1. The function itself — you should get a Telegram message instantly.
[admin@edge] > :global tgSend; $tgSend ("test alert from edge")

# 2. Device-mode actually enabled (fetch: yes, scheduler: yes).
[admin@edge] > /system/device-mode/print

# 3. Force a threshold low to confirm the health trigger fires, then restore.
[admin@edge] > /system/script/run tg-health   # temporarily drop 90→1 while testing

# 4. Watch for failures — a failed fetch logs, it doesn't shout.
[admin@edge] > /log/print where message~"tgSend"

Takeaways

  1. Write the notifier once as a global $tgSend function; wire every trigger to it. Re-register it at startup, because globals don't survive a reboot.
  2. POST a JSON body, not a GET query string — you escape three characters instead of URL-encoding the whole message.
  3. Device-mode gates fetch and scheduler on RouterOS 7.13+; enabling them needs a physical power-cycle. It can't be fully provisioned over SSH alone.
  4. RouterOS trusts no CA by default and a wrong clock breaks TLS — import the CA and sync NTP, or accept an unverified link only in a lab.
  5. Edge-trigger, don't level-trigger: netwatch already fires on transitions; for polled CPU/temp add a hysteresis flag so a value jittering at the threshold doesn't flood you.
  6. Keep the bot token out of your configs — set it as a global from a file you never commit.

Routers that tell you when something's wrong?

A dropped WAN or a dying link shouldn't wait for someone to notice. I wire RouterOS fleets to alert on the events that matter — uplink, load, config change, auth — pushed to Telegram and backed by proper logging and metrics, so small sites stay watched without a NOC. Let's set yours up.

Book a Discovery Call →