Telegram

Router Status Report to Telegram — 7

Sends a status message to a Telegram chat: public IP, time, uptime, board, temperature, active PPP sessions, neighbors and the live rate / total traffic of each interface. Run it from the Scheduler or on boot.

MikroTik Version

RouterOS 7

# EN: Telegram chat ID that receives the report | AR: رقم الشات (Chat ID) الذي سيستقبل التقرير
:local chatId "change here";
# EN: Bot token from @BotFather | AR: توكن البوت من @BotFather
:local botToken "change here";

# EN: Safety check | AR: حماية من عدم تعديل المتغيرات
:if ($chatId = "change here" || $botToken = "change here") do={ :error "Edit the variables first" }

# EN: "%0A" = new line, "+" = space inside the Telegram URL | AR: %0A سطر جديد و + مسافة داخل رابط تيليجرام
:local nl "%0A";

# EN: Public IP returned directly (no temp file in v7) | AR: الـ IP العام يرجع مباشرة (بدون ملف مؤقت في v7)
:local ip "unknown";
:do { :set ip ([/tool fetch url="<https://api.ipify.org>" output=user as-value]->"data") } on-error={ :log warning "telegram report: cannot get public IP" }

# EN: In v7 /system health is a list of name/value items | AR: في v7 قائمة /system health عبارة عن عناصر (الاسم/القيمة)
:local temp "n/a";
:do { :set temp [/system health get [find where name=temperature] value] } on-error={}

# EN: Live rate (kbps) and total traffic (MB) of running ether/bridge/vlan interfaces
# AR: السرعة اللحظية (كيلوبت) وإجمالي الترافيك (ميجا) للواجهات الشغالة
:local traffic "";
:do {
    :foreach i in=[/interface find where running=yes] do={
        :local t [/interface get $i type];
        :if ($t = "ether" || $t = "bridge" || $t = "vlan") do={
            :local n [/interface get $i name];
            :local rate [/interface monitor-traffic $n once as-value];
            :local rx (($rate->"rx-bits-per-second") / 1024);
            :local tx (($rate->"tx-bits-per-second") / 1024);
            :local mb ((([/interface get $i rx-byte] + [/interface get $i tx-byte]) / 1024) / 1024);
            :set traffic ($traffic . $n . ":+RX+" . $rx . "+/+TX+" . $tx . "+kbps+/+" . $mb . "+MB" . $nl);
        }
    }
} on-error={ :log warning "telegram report: cannot read interface traffic" }

# EN: Build the message | AR: تجهيز الرسالة
:local text ("MikroTik+Report" . $nl . "Public+IP:+" . $ip . $nl . "Time:+" . [/system clock get date] . "+" . [/system clock get time] . $nl . "Uptime:+" . [/system resource get uptime] . $nl . "Board:+" . [/system resource get board-name] . $nl . "Temperature:+" . $temp . "+C" . $nl . "Active+PPP:+" . [:len [/ppp active find]] . $nl . "Neighbors:+" . [:len [/ip neighbor find]] . $nl . $traffic);

# EN: Send it | AR: إرسال الرسالة
/tool fetch url=("<https://api.telegram.org/bot>" . $botToken . "/sendMessage?chat_id=" . $chatId . "&text=" . $text) keep-result=no;