├── .gitattributes ├── LICENSE ├── README.md ├── script text ├── func_fetch ├── func_lowercase ├── reboot-report ├── tg_cmd_cpu ├── tg_cmd_dhcp ├── tg_cmd_disablehotspot ├── tg_cmd_enablehotspot ├── tg_cmd_forceupdateddns ├── tg_cmd_help ├── tg_cmd_hotspot ├── tg_cmd_interface ├── tg_cmd_monitoring ├── tg_cmd_ping ├── tg_cmd_public ├── tg_cmd_reboot ├── tg_cmd_stop ├── tg_config ├── tg_getUpdates ├── tg_getkey └── tg_sendMessage └── telegram_bot.rsc /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Dwi Candra Permana 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | abcdefg 23 | 24 | hijklmnvufiauboiwbdibad 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # MikroTik Script: Telegram Bot 3 | 4 | _Script_ ini digunakan untuk mengontrol MikroTik Anda hanya dengan menggunakan sosial media Telegram. Terdapat banyak perintah untuk memantau, mengubah _hotspot_, menghapus _user hotspot_, menambahkan akun _hotspot_ baru, mengubah _password user hotspot_, dan lain-lain. 5 | 6 | # Daftar Isi 7 | - [Disclaimer](#disclaimer) 8 | - [Riwayat Versi](#riwayat-versi) 9 | - [Instalasi](#instalasi) 10 | - [Perintah-Perintah, Parameter, dan Fungsinya](#perintah-perintah-parameter-dan-fungsinya) 11 | - [Sumber](#sumber) 12 | 13 | # Disclaimer 14 | _Script_ ini bersifat _open-source_. Anda dapat memodifikasi, menambah, ataupun mengurangi isi dari _script_ ini selama tidak melanggar ketentuan yang berlaku dalam lisensi _MIT_. _Script_ ini **TIDAK ADA GARANSI** selama Anda menggunakannya. Jika Anda mengalami kendala saat instalasi atau penggunaan _script_ ini, harap didiskusikan dan jelaskan bagaimana masalah tersebut terjadi melalui fitur [**Issues**](https://github.com/dwichan0905/telegram_bot/issues). 15 | 16 | # Kontribusi 17 | Kontribusi pada _repository_ ini hanya terbatas pada MikroTik Script dan Dokumentasi saja. Anda dapat berkontribusi dengan cara _Fork repository ini_, membuat _branch_ baru, lakukan perubahan, dan lakukan _Pull Request_ ke _repository_ ini. Deskripsikan apa saja yang Anda tambahkan dan apa yang Anda ubah di dalam _repository_ ini. Jangan lupa untuk menuliskan script bantuan di ```tg_cmd_help``` untuk membantu apabila pengguna script ini lupa dengan perintah yang harus ia tuliskan.. 18 | 19 | # Riwayat Versi 20 | #### 1.3.1 (27 Oktober 2020) 21 | - menambahkan perintah ``/monitoring`` yang berfungsi untuk mengamati transfer rate dan received rate dari interface, juga bisa untuk mengamati penggunaan CPU serta RAM 22 | - menambah perintah ``/stop`` untuk menghentikan proses monitoring yang sedang berjalan di telegram 23 | video: [YouTube](https://www.youtube.com/watch?v=0HEuqgMZVp4) 24 | 25 | #### 1.3 (8 Oktober 2020) 26 | - mengeluarkan setiap script ke dalam versi teks agar bisa dibaca langsung tanpa harus melakukan import ke mikrotik 27 | - menambah fungsi lowercase `func_lowercase` 28 | - modifikasi perintah hotspot, menambahkan fungsi lowercase supaya jika ada parameter yang menggunakan huruf kapital tetap bisa terbaca (misalkan ada user yang mengetikkan `/hotspot SesSion CoUnT` maka akan bisa terbaca dan bot akan mengirimkan balasan) 29 | - menghapus `tg_cmd_start` dan `tg_cmd_hi` (penjelasan ada dibawah) 30 | - menambah alternative command pada tg_getUpdates, apabila user mengetikkan: `/hi`, `/start`, `/hai`, `/halo`, `/hello`, `/bantuan`. Maka, akan menjalankan script `tg_cmd_help`. Dengan kata lain `tg_cmd_help` juga menangani perintah perintah tersebut 31 | - berlaku juga pada `/hotspot`. Apabila user mengetikkan `/hs` maka akan diarahkan ke `tg_cmd_hotspot` 32 | - modifikasi tg_cmd_dhcp agar menggunakan fungsi lowercase dan menghapus parameter yang tidak perlu 33 | - menambah perintah /dhcp lease 34 | - menambahkan perintah /interface show all 35 | 36 | #### 1.2 (11 Agustus 2019) 37 | 1. Perbaikan _bug_ saat _import script_ (error ``invalid default argument``) 38 | 2. Pembaruan perintah pada hotspot: 39 | - Mengganti perintah ``/hotspot users`` menjadi ``/hotspot session count`` 40 | - Mengganti perintah ``/hotspot showall`` menjadi ``/hotspot session showall`` 41 | - Menambah perintah baru: ``/hotspot session deauth-by-mac``, ``/hotspot session deauth-by-ip``, dan ``/hotspot session deauth-by-user`` 42 | 3. Perbaikan perintah: 43 | - ``/reboot`` kini dapat digunakan untuk menghidupkan ulang _router_ (_delay_ 30 detik) 44 | 4. Penambahan kondisi baru: 45 | - Setelah _reboot_, _Router_ akan mengirimkan laporan via Telegram bahwa dirinya telah melakukan _reboot_ dan mencatat semua kasus mengapa ia melakukan itu ke dalam "Critical Log" (jeda 30 detik setelah router selesai _reboot_). 46 | 47 | #### 1.1 (8 Agustus 2019) 48 | 1. Versi pertama 49 | 50 | ## Instalasi 51 | Sebelum mulai instalasi, Anda harus memiliki _Access Token_ untuk Bot Telegram dan ChatID nya. Ikuti [link ini (labkom.co.id)](https://labkom.co.id/mikrotik/mikrotik-netwach-monitoring-status-access-point-hotspot-dengan-menggunakan-telegram) untuk paduan cara membuat bot telegram 52 | Untuk cara menginstalnya, silahkan _clone_ atau _download repository_ ini, lalu: 53 | 54 | 1. Ekstrak file ZIP yang sudah Anda _download_ (lewati jika anda _clone repository_ ini) 55 | 2. _Upload_ file ``telegram_bot.rsc`` ke dalam MikroTik Anda (bisa lewat FileZilla FTP, bisa juga lewat WinBox) dan simpan ke folder utama (root atau /) di MikroTik Anda. 56 | 3. Setelah itu, buka _Terminal_ MikroTik dan ketikkan perintah berikut: 57 | ``import file-name=telegram_bot.rsc`` 58 | 4. Konfigurasikan pengaturan _bot_ nya di ``System > Scripts > tg_config`` dengan mengubah perintah berikut: 59 | > Isi dengan Access Token Bot Telegram Anda: 60 | ``"botAPI"="xxxxxx:xxxxxxxx-xxxxxxx"`` 61 | 62 | > Isi dengan ChatID Telegram Anda: 63 | ``defaultChatID"="xxxxxxxxxx"`` 64 | 65 | > Isi dengan beberapa ChatID Anda, bisa personal, bisa grup. Pisahkan dengan tanda koma: 66 | ``"trusted"="xxxxxxxxxx, xxxxxxxxx, -xxxxxxxxx"`` 67 | 68 | Lalu simpan konfigurasinya. 69 | 70 | 5. Selesai! 71 | 72 | ## Perintah-Perintah, Parameter, dan Fungsinya 73 | Ketikkan perintah berikut pada kolom _chatting_ Anda dengan _bot_ Telegram Anda. Setiap _parameter_ yang dimasukkan, dipisahkan dengan menggunakan spasi, misalnya ``/interface show``. 74 | 75 | | Perintah | Parameter | Fungsi | Contoh | 76 | |-----------|--------------|-------|-----| 77 | | ``/help`` | | Menampilkan daftar fungsi yang dapat dieksekusi | | 78 | | ``/start`` | | Menampilkan daftar fungsi yang dapat dieksekusi | | 79 | | ``/cpu`` | | Menampilkan Router ID, Load CPU, Uptime, dan total RAM yang terpakai | | 80 | | ``/dhcp`` | ``lease`` | menampilkan seluruh detail pada DHCP Lease| ``/dhcp lease`` | 81 | | ``/dhcp`` | ``client release `` | Merelease dhcp client pada interface tertentu| ``/dhcp client release ether1`` | 82 | | ``/interface`` | ``show`` | Menampilkan status terhubungnya antar port Ethernet di MikroTik | ``/interface show`` | 83 | | ``/interface`` | ``show all`` | Menampilkan status terhubungnya seluruh interface di MikroTik | ``/interface show all`` | 84 | | ``/hotspot`` | ``help`` | Menampilkan detail bantuan untuk perintah `/hotspot` | ``/hotspot help`` | 85 | | ``/hotspot`` | ``session count`` | Menampilkan jumlah user yang sedang aktif | ``/hotspot session count`` | 86 | | ``/hotspot`` | ``session showall`` | Menampilkan seluruh detail user yang sedang aktif mulai dari Username sampai Uptime (kecuali password) | ``/hotspot session showall`` | 87 | | ``/hotspot`` | ``session deauth-by-user `` | Mencabut _session_ perangkat berdasarkan Username | ``/hotspot session deauth-by-user telecomadmin`` | 88 | | ``/hotspot`` | ``session deauth-by-ip `` | Mencabut _session_ perangkat berdasarkan Alamat IP | ``/hotspot session deauth-by-ip 192.168.1.2`` | 89 | | ``/hotspot`` | ``session deauth-by-mac `` | Mencabut _session_ perangkat berdasarkan Alamat MAC | ``/hotspot session deauth-by-mac AB:CD:EF:01:23:45`` | 90 | | ``/hotspot`` | ``add `` | Menambahkan user hotspot baru | ``/hotspot add telecomadmin admintelecom`` | 91 | | ``/hotspot`` | ``delete `` | Menghapus user hotspot secara permanen | ``/hotspot delete telecomadmin`` | 92 | | ``/hotspot`` | ``disable `` | Mematikan atau menonaktifkan user hotspot | ``/hotspot disable telecomadmin`` | 93 | | ``/hotspot`` | ``enable `` | Mengaktifkan user hotspot yang dinonaktifkan | ``/hotspot enable telecomadmin`` | 94 | | ``/hotspot`` | ``change-password `` | Mengubah password user hotspot | ``/hotspot change-password telecomadmin p4ssw0rdny4`` | 95 | | ``/ping`` | | Melakukan ping ke DNS Google | ``/ping`` | 96 | | ``/monitoring`` | ``interface `` | Melakukan monitoring terhadap interface | ``/monitoring interface wlan1`` | 97 | | ``/monitoring`` | ``cpu`` | Melakukan monitoring terhadap penggunaan Cpu pada router | ``/monitoring cpu`` | 98 | | ``/monitoring`` | ``ram`` | Melakukan monitoring terhadap penggunaan ram/memory pada router | ``/monitoring ram`` | 99 | | ``/monitoring`` | ``memory`` | Melakukan monitoring terhadap penggunaan ram/memory pada router | ``/monitoring memory`` | 100 | | ``/ping`` | ``to `` | Melakukan ping ke alamat IP tertentu | ``/ping to 127.0.0.1`` | 101 | | ``/public`` | | Menampilkan Dynamic DNS dan Public IP pada MikroTik Anda | ``/public`` | 102 | | ``/enablehotspot`` | | Mengaktifkan seluruh fungsi hotspot | ``/enablehotspot`` | 103 | | ``/disablehotspot`` | | Menonaktifkan seluruh fungsi hotspot | ``/disablehotspot`` | 104 | | ``/forceupdateddns`` | | Memperbarui Dynamic DNS secara paksa | ``/forceupdateddns`` | 105 | | ``/reboot`` | | Menghidupkan ulang MikroTik (jeda 30 detik sebelum menghidupkan ulang) | ``/reboot`` | 106 | 107 | > **Catatan**: untuk dapat menjalankan perintah ``/disablehotspot``, ``/enablehotspot``, dan ``/interface show``, silakan Anda konfigurasikan sendiri hotspot mana yang akan di "otomatis" kan di script ``tg_cmd_disablehotspot``, ``tg_cmd_enablehotspot``, dan ethernet mana saja yang akan ditampilkan di ``tg_cmd_interface``. 108 | 109 | # Sumber 110 | 111 | - [Labkom](https://labkom.co.id) 112 | - [Forum MikroTik (EN)](https://forum.mikrotik.com) 113 | - [Telegram API (EN)](https://api.telegram.org) 114 | 115 | 116 | -------------------------------------------------------------------------------- /script text/func_fetch: -------------------------------------------------------------------------------- 1 | ######################################################### 2 | # Wrapper for /tools fetch 3 | # Input: 4 | # mode 5 | # upload=yes/no 6 | # user 7 | # password 8 | # address 9 | # host 10 | # httpdata 11 | # httpmethod 12 | # check-certificate 13 | # src-path 14 | # dst-path 15 | # ascii=yes/no 16 | # url 17 | # resfile 18 | 19 | :local res "fetchresult.txt" 20 | :if ([:len $resfile]>0) do={:set res $resfile} 21 | #:put $res 22 | 23 | :local cmd "/tool fetch" 24 | :if ([:len $mode]>0) do={:set cmd "$cmd mode=$mode"} 25 | :if ([:len $upload]>0) do={:set cmd "$cmd upload=$upload"} 26 | :if ([:len $user]>0) do={:set cmd "$cmd user=\"$user\""} 27 | :if ([:len $password]>0) do={:set cmd "$cmd password=\"$password\""} 28 | :if ([:len $address]>0) do={:set cmd "$cmd address=\"$address\""} 29 | :if ([:len $host]>0) do={:set cmd "$cmd host=\"$host\""} 30 | :if ([:len $"http-data"]>0) do={:set cmd "$cmd http-data=\"$"http-data"\""} 31 | :if ([:len $"http-method"]>0) do={:set cmd "$cmd http-method=\"$"http-method"\""} 32 | :if ([:len $"check-certificate"]>0) do={:set cmd "$cmd check-certificate=\"$"check-certificate"\""} 33 | :if ([:len $"src-path"]>0) do={:set cmd "$cmd src-path=\"$"src-path"\""} 34 | :if ([:len $"dst-path"]>0) do={:set cmd "$cmd dst-path=\"$"dst-path"\""} 35 | :if ([:len $ascii]>0) do={:set cmd "$cmd ascii=\"$ascii\""} 36 | :if ([:len $url]>0) do={:set cmd "$cmd url=\"$url\""} 37 | 38 | :put ">> $cmd" 39 | 40 | :global FETCHRESULT 41 | :set FETCHRESULT "none" 42 | 43 | :local script "\ 44 | :global FETCHRESULT;\ 45 | :do {\ 46 | $cmd;\ 47 | :set FETCHRESULT \"success\";\ 48 | } on-error={\ 49 | :set FETCHRESULT \"failed\";\ 50 | }\ 51 | " 52 | :execute script=$script file=$res 53 | :local cnt 0 54 | #:put "$cnt -> $FETCHRESULT" 55 | :while ($cnt<100 and $FETCHRESULT="none") do={ 56 | :delay 1s 57 | :set $cnt ($cnt+1) 58 | #:put "$cnt -> $FETCHRESULT" 59 | } 60 | :local content [/file get [find name=$res] content] 61 | #:put $content 62 | if ($content~"finished") do={:return "success"} 63 | :return $FETCHRESULT -------------------------------------------------------------------------------- /script text/func_lowercase: -------------------------------------------------------------------------------- 1 | local alphabet {"A"="a";"B"="b";"C"="c";"D"="d";"E"="e";"F"="f";"G"="g";"H"="h";"I"="i";"J"="j";"K"="k";"L"="l";"M"="m";"N"="n";"O"="o";"P"="p";"Q"="q";"R"="r";"S"="s";"T"="t";"U"="u";"V"="v";"X"="x";"Z"="z";"Y"="y";"W"="w"}; 2 | :local result 3 | :local character 4 | :for strings from=0 to=([:len $1] - 1) do={ 5 | :local single [:pick $1 $strings] 6 | :set character ($alphabet->$single) 7 | :if ([:typeof $character] = "str") do={set single $character} 8 | :set result ($result.$single) 9 | } 10 | :return $result -------------------------------------------------------------------------------- /script text/reboot-report: -------------------------------------------------------------------------------- 1 | :local send [:parse [/system script get tg_sendMessage source]] 2 | :put $params 3 | :put $chatid 4 | :put $from 5 | 6 | :local reportBody "" 7 | 8 | :local deviceName [/system identity get name] 9 | :local deviceDate [/system clock get date] 10 | :local deviceTime [/system clock get time] 11 | :local hwModel [/system routerboard get model] 12 | :local rosVersion [/system package get system version] 13 | :local currentFirmware [/system routerboard get current-firmware] 14 | :local upgradeFirmware [/system routerboard get upgrade-firmware] 15 | 16 | 17 | :set reportBody ($reportBody . "Router Reboot Report for $deviceName%0A") 18 | :set reportBody ($reportBody . "Report generated on $deviceDate at $deviceTime%0A%0A") 19 | 20 | :set reportBody ($reportBody . "Hardware Model: $hwModel%0A") 21 | :set reportBody ($reportBody . "RouterOS Version: $rosVersion%0A") 22 | :set reportBody ($reportBody . "Current Firmware: $currentFirmware%0A") 23 | :set reportBody ($reportBody . "Upgrade Firmware: $upgradeFirmware") 24 | if ( $currentFirmware < $upgradeFirmware) do={ 25 | :set reportBody ($reportBody . "NOTE: You should upgrade the RouterBOARD firmware!%0A") 26 | } 27 | 28 | :set reportBody ($reportBody . "%0A%0A=== Critical Log Events ===%0A" ) 29 | 30 | :local x 31 | :local ts 32 | :local msg 33 | foreach i in=([/log find where topics~"critical"]) do={ 34 | :set $ts [/log get $i time] 35 | :set $msg [/log get $i message] 36 | :set $reportBody ($reportBody . $ts . " " . $msg . "%0A" ) 37 | } 38 | 39 | :set reportBody ($reportBody . "%0A=== end of report ===%0A") 40 | $send chat=$chatid text=$reportBody mode="Markdown" -------------------------------------------------------------------------------- /script text/tg_cmd_cpu: -------------------------------------------------------------------------------- 1 | :local send [:parse [/system script get tg_sendMessage source]] 2 | :local hotspot [:len [/ip hotspot active find]] 3 | 4 | :put $params 5 | :put $chatid 6 | :put $from 7 | 8 | :local text "Router ID:* $[/system identity get name] * %0A\ 9 | Uptime: _$[/system resource get uptime]_%0A\ 10 | CPU Load: _$[/system resource get cpu-load]%_%0A\ 11 | RAM: _$(([/system resource get total-memory]-[/system resource get free-memory])/(1024*1024))M/$([/system resource get total-memory]/(1024*1024))M_" 12 | 13 | $send chat=$chatid text=$text mode="Markdown" 14 | :return true -------------------------------------------------------------------------------- /script text/tg_cmd_dhcp: -------------------------------------------------------------------------------- 1 | :local send [:parse [/system script get tg_sendMessage source]] 2 | :local tolower [:parse [/system script get func_lowercase source]] 3 | :local param1 [:pick $params 0 [:find $params " "]] 4 | :local param2 [:pick $params ([:find $params " "]+1) [:len $params]] 5 | :local param3 [:pick [:pick $params ([:find $params " "]+1) [:len $params]] ([:find [:pick $params ([:find $params " "]+1) [:len $params]] " "]+1) [:len [:pick $params ([:find $params " "]+1) [:len $params]]]] 6 | :if ([:len [:find $param2 " "]]>0) do={ 7 | :set param2 [:pick [:pick $params ([:find $params " "]+1) [:len $params]] 0 [:find [:pick $params ([:find $params " "]+1) [:len $params]] " "]] 8 | } else={ 9 | :set param3 "" 10 | } 11 | 12 | :local paramsLower [$tolower $params] 13 | :local param1Lower [$tolower $param1] 14 | :local param2Lower [$tolower $param2] 15 | :local param3Lower [$tolower $param3] 16 | 17 | :local getVendor false 18 | :if ($paramsLower = "lease") do={ 19 | local GetMacVendor do={ 20 | :do { 21 | return ([/tool fetch mode=https http-method=get url=("https://api.macvendors.com/".[:pick [:tostr $1] 0 8 ]) as-value output=user ]->"data") 22 | } on-error={ 23 | return "unknown vendor" 24 | } 25 | } 26 | :local text 27 | :local number (0) 28 | :set text ("Router ID: $[/system identity get value-name=name]") 29 | :foreach lease in=[/ip dhcp-server lease find] do={ 30 | :set number ($number + 1) 31 | set text ($text."====== \F0\9F\97\82 DHCP LEASE ($number) \F0\9F\97\82 ======%0A") 32 | :local addr [/ip dhcp-server lease get value-name=address $lease] 33 | :set text ($text."IP Address: $addr%0A") 34 | :local mac [/ip dhcp-server lease get value-name=mac-address $lease] 35 | :set text ($text."Mac Address: $mac%0A") 36 | :if ($getVendor = "true") do={ 37 | :local macvendor [$GetMacVendor $mac] 38 | :set text ($text."Mac Vendor: $macvendor%0A") 39 | } 40 | :local host [/ip dhcp-server lease get value-name=host-name $lease] 41 | :if ([:typeof $host] = "nil") do={set host ("Unknown Hostname")} 42 | :set text ($text."Hostname: $$host%0A") 43 | :local lastseen [/ip dhcp-server lease get value-name=last-seen $lease] 44 | :set text ($text."Uptime: $lastseen%0A") 45 | :local server [/ip dhcp-server lease get value-name=server $lease] 46 | :set text ($text."Server: $server%0A") 47 | } 48 | :set text ($text."=========== END REPORT ===========%0A") 49 | $send chat=$chatid text=("$text") mode="Markdown" 50 | } 51 | :if ($param1Lower = "client" && $param2Lower = "release") do={ 52 | :if ([:len $param3] > 0) do={ 53 | :if ([:len [/ip dhcp-client/ find where interface=$param3]] > 0) do={ 54 | $send chat=$chatid text="DHCP Client akan di release dalam 5 detik" mode="Markdown"; 55 | :delay 5s 56 | /ip dhcp-client release $param3 57 | } else={ 58 | $send chat=$chatid text=("Maaf, DHCP client dengan interface $param3 tidak ditemukan") mode="Markdown"; 59 | } 60 | } else={ 61 | $send chat=$chatid text="Silahkan masukkan interface yang ingin di release dhcpclient-nya" mode="Markdown"; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /script text/tg_cmd_disablehotspot: -------------------------------------------------------------------------------- 1 | :local send [:parse [/system script get tg_sendMessage source]] 2 | :put $params 3 | :put $chatid 4 | :put $from 5 | 6 | :local text "All hotspots disabled" 7 | 8 | $send chat=$chatid text=$text mode="Markdown" 9 | /ip hotspot disable lantai1 10 | /ip hotspot disable lantai2 -------------------------------------------------------------------------------- /script text/tg_cmd_enablehotspot: -------------------------------------------------------------------------------- 1 | :local send [:parse [/system script get tg_sendMessage source]] 2 | :put $params 3 | :put $chatid 4 | :put $from 5 | 6 | :local text "All hotspots enabled" 7 | 8 | $send chat=$chatid text=$text mode="Markdown" 9 | /ip hotspot enable lantai1 10 | /ip hotspot enable lantai2 -------------------------------------------------------------------------------- /script text/tg_cmd_forceupdateddns: -------------------------------------------------------------------------------- 1 | :local send [:parse [/system script get tg_sendMessage source]] 2 | :put $params 3 | :put $chatid 4 | :put $from 5 | 6 | :local text "Force Update DDNS" 7 | 8 | $send chat=$chatid text=$text mode="Markdown" 9 | /ip cloud force-update -------------------------------------------------------------------------------- /script text/tg_cmd_help: -------------------------------------------------------------------------------- 1 | :local send [:parse [/system script get tg_sendMessage source]] 2 | 3 | :put $params 4 | :put $chatid 5 | :put $from 6 | 7 | :local text "Router ID:* $[/system identity get name] * %0A\ 8 | ==================%0A\ 9 | MENU TERSEDIA%0A\ 10 | ==================%0A\ 11 | /help%0A\ 12 | /start%0A\ 13 | /cpu%0A\ 14 | /interface%0A\ 15 | - show%0A\ 16 | /hotspot%0A\ 17 | - session%0A\ 18 | > count%0A\ 19 | > showall%0A\ 20 | > deauth-by-user %0A\ 21 | > deauth-by-mac %0A\ 22 | > deauth-by-ip %0A\ 23 | - add %0A\ 24 | - delete %0A\ 25 | - disable %0A\ 26 | - enable %0A\ 27 | - setprofile %0A\ 28 | - change-password %0A\ 29 | /ping to %0A\ 30 | /public%0A\ 31 | /enablehotspot%0A\ 32 | /disablehotspot%0A\ 33 | /forceupdateddns%0A\ 34 | /reboot" 35 | 36 | $send chat=$chatid text=$text mode="Markdown" 37 | :return true -------------------------------------------------------------------------------- /script text/tg_cmd_hotspot: -------------------------------------------------------------------------------- 1 | :local send [:parse [/system script get tg_sendMessage source]] 2 | :local tolower [:parse [/system script get func_lowercase source]] 3 | :local param1 [:pick $params 0 [:find $params " "]] 4 | :local param2 [:pick $params ([:find $params " "]+1) [:len $params]] 5 | :local param3 [:pick [:pick $params ([:find $params " "]+1) [:len $params]] ([:find [:pick $params ([:find $params " "]+1) [:len $params]] " "]+1) [:len [:pick $params ([:find $params " "]+1) [:len $params]]]] 6 | :if ([:len [:find $param2 " "]]>0) do={ 7 | :set param2 [:pick [:pick $params ([:find $params " "]+1) [:len $params]] 0 [:find [:pick $params ([:find $params " "]+1) [:len $params]] " "]] 8 | } else={ 9 | :set param3 "" 10 | } 11 | 12 | :put $params 13 | :put $param1 14 | :put $param2 15 | :put $param3 16 | :put $chatid 17 | :put $from 18 | 19 | :local paramsLower [$tolower $params] 20 | :local param1Lower [$tolower $param1] 21 | :local param2Lower [$tolower $param2] 22 | :local param3Lower [$tolower $param3] 23 | 24 | :local sendHelp do={ 25 | :local send [:parse [/system script get tg_sendMessage source]] 26 | :local txt 27 | :set txt ("*Daftar parameter:*%0A%0A") 28 | :set txt ($txt."> session count - Menampilkan jumlah user yang sedang aktif %0ACth: _/hotspot session showall_%0A") 29 | :set txt ($txt."> session showall - Menampilkan seluruh detail user yang sedang aktif %0ACth: _/hotspot session showall_%0A") 30 | :set txt ($txt."> session deauth-by-user __ - Mencabut sesi perangkat berdasarkan Username. %0ACth: _/hotspot session deauth-by-user admin_%0A") 31 | :set txt ($txt."> session deauth-by-ip __ - Mencabut sesi perangkat berdasarkan Alamat IP. %0ACth: _/hotspot session deauth-by-ip 192.168.1.1_%0A") 32 | :set txt ($txt."> session deauth-by-mac __ - Mencabut sesi perangkat berdasarkan Alamat MAC. %0ACth: _/hotspot session deauth-by-mac AB:CD:EF:01:23:45_%0A") 33 | :set txt ($txt."> add _ _ - Menambah user baru. %0ACth: _/hotspot add admin admin123_%0A") 34 | :set txt ($txt."> delete __ - Menghapus user. %0ACth: _/hotspot delete admin _%0A") 35 | :set txt ($txt."> disable __ - menonaktifkan user. %0ACth: _/hotspot disable admin _%0A") 36 | :set txt ($txt."> enable __ - mengaktifkan user. %0ACth: _/hotspot enable admin _%0A") 37 | :set txt ($txt."> change-password _ _ - mengganti password user. %0ACth: _/hotspot change-password admin p4sSw_%0A%0A") 38 | $send chat=$chatid text=("$txt") mode="Markdown" 39 | } 40 | 41 | if ($paramsLower = "help") do={ 42 | $sendHelp 43 | } 44 | 45 | :if ($param1Lower="session") do={ 46 | :if ($param2Lower="count") do={ 47 | :local output 48 | :local hotspot [:len [/ip hotspot active find]] 49 | :local text "Router ID:* $[/system identity get name] * %0A\ 50 | Hotspot users: _$hotspot online_" 51 | $send chat=$chatid text=$text mode="Markdown" 52 | } 53 | :if ($param2Lower="showall") do={ 54 | :local output 55 | :foreach activeIndex in=[/ip hotspot active find] do={ 56 | :local activeUser ("*Username*: ".[/ip hotspot active get value-name="user" $activeIndex]."%0A") 57 | :local activeAddress ("*IP*: ".[/ip hotspot active get value-name="address" $activeIndex]."%0A") 58 | :local activeMACAddr ("*MAC*: ".[/ip hotspot active get value-name="mac-address" $activeIndex]."%0A") 59 | :local activeLoginBy ("*Login Method*: ".[/ip hotspot active get value-name="login-by" $activeIndex]."%0A") 60 | :local activeUptime ("*Uptime*: ".[/ip hotspot active get value-name="uptime" $activeIndex]."%0A") 61 | :local idleTime ("*Idle Time*: ".[/ip hotspot active get value-name="idle-time" $activeIndex]."%0A") 62 | :local serverIn ("*Server*: ".[/ip hotspot active get value-name="server" $activeIndex]."%0A") 63 | :set output ($output.$activeUser.$activeAddress.$activeMACAddr.$activeUptime.$idleTime.$activeLoginBy.$serverIn."%0A") 64 | } 65 | $send chat=$chatid text=("$output") mode="Markdown" 66 | } 67 | :if ($param2Lower="deauth-by-user") do={ 68 | /ip hotspot active remove [find user="$param3"] 69 | $send chat=$chatid text=("Sesi User $param3 berhasil dihapus") mode="Markdown" 70 | } 71 | :if ($param2Lower="deauth-by-mac") do={ 72 | /ip hotspot active remove [find mac-address="$param3"] 73 | $send chat=$chatid text=("Sesi MAC $param3 berhasil dihapus") mode="Markdown" 74 | } 75 | :if ($param2Lower="deauth-by-ip") do={ 76 | /ip hotspot active remove [find address="$param3"] 77 | $send chat=$chatid text=("Sesi IP $param3 berhasil dihapus") mode="Markdown" 78 | } 79 | } 80 | :if ($param1Lower="add") do={ 81 | if ([:len [ip hotspot user find where name=$param2]] = 0) do={ 82 | /ip hotspot user add name=$param2 password=$param3 profile=default 83 | $send chat=$chatid text=("Berhasil membuat user baru. Masuk ke hotspot dengan:%0A%0A*Username:* $param2%0A*Password:* $param3") mode="Markdown" 84 | } else={ 85 | $send chat=$chatid text=("User $param2 sudah terpakai, silahkan gunakan nama lain") mode="Markdown" 86 | } 87 | 88 | } 89 | :if ($param1Lower="delete") do={ 90 | if ([:len [ip hotspot user find where name=$param2]] > 0) do={ 91 | /ip hotspot user remove [find name=$param2] 92 | $send chat=$chatid text=("Berhasil menghapus user $param2.") mode="Markdown" 93 | } else={ 94 | $send chat=$chatid text=("User $param2 tidak ditemukan") mode="Markdown" 95 | } 96 | } 97 | :if ($param1Lower="disable") do={ 98 | if ([:len [ip hotspot user find where name=$param2]] > 0) do={ 99 | /ip hotspot user disable [find name=$param2] 100 | $send chat=$chatid text=("$param2 kini telah dinonaktifkan") mode="Markdown" 101 | } else={ 102 | $send chat=$chatid text=("User $param2 tidak ditemukan") mode="Markdown" 103 | } 104 | } 105 | :if ($param1Lower="enable") do={ 106 | if ([:len [ip hotspot user find where name=$param2]] > 0) do={ 107 | /ip hotspot user enable [find name=$param2] 108 | $send chat=$chatid text=("$param2 kini telah diaktifkan") mode="Markdown" 109 | } else={ 110 | $send chat=$chatid text=("User $param2 tidak ditemukan") mode="Markdown" 111 | } 112 | } 113 | :if ($param1Lower="setprofile") do={ 114 | if ([:len [ip hotspot user find where name=$param2]] > 0) do={ 115 | /ip hotspot user set password=$param3 [find name=$param2] 116 | /ip hotspot active remove [find name=$param2] 117 | $send chat=$chatid text=("Berhasil mengganti profile menjadi $param3.") mode="Markdown" 118 | } else={ 119 | $send chat=$chatid text=("User $param2 tidak ditemukan") mode="Markdown" 120 | } 121 | } 122 | :if ($param1Lower="change-password") do={ 123 | if ([:len [ip hotspot user find where name=$param2]] > 0) do={ 124 | /ip hotspot user set password=$param3 [find name=$param2] 125 | /ip hotspot active remove [find name=$param2] 126 | $send chat=$chatid text=("Berhasil mengganti password untuk $param2.") mode="Markdown" 127 | } else={ 128 | $send chat=$chatid text=("User $param2 tidak ditemukan") mode="Markdown" 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /script text/tg_cmd_interface: -------------------------------------------------------------------------------- 1 | :local send [:parse [/system script get tg_sendMessage source]] 2 | :local param1 [:pick $params 0 [:find $params " "]] 3 | :local param2 [:pick $params ([:find $params " "]+1) [:len $params]] 4 | :local param3 [:pick [:pick $params ([:find $params " "]+1) [:len $params]] ([:find [:pick $params ([:find $params " "]+1) [:len $params]] " "]+1) [:len [:pick $params ([:find $params " "]+1) [:len $params]]]] 5 | :if ([:len [:find $param2 " "]]>0) do={ 6 | :set param2 [:pick [:pick $params ([:find $params " "]+1) [:len $params]] 0 [:find [:pick $params ([:find $params " "]+1) [:len $params]] " "]] 7 | } else={ 8 | :set param3 "" 9 | } 10 | 11 | :put $params 12 | :put $param1 13 | :put $param2 14 | :put $param3 15 | :put $chatid 16 | :put $from 17 | 18 | :if ($params="show") do={ 19 | :local output "Router ID:* $[/system identity get name] * %0A%0A" 20 | :local eth01status 21 | :local eth03status 22 | :local eth04status 23 | :local eth05status 24 | 25 | :if ([/interface ethernet get eth01-router running]=true) do={ 26 | :set eth01status ("Internet is *CONNECTED*%0A") 27 | } else={ 28 | :set eth01status ("Internet is *DISCONNECTED*%0A") 29 | } 30 | 31 | :if ([/interface ethernet get eth03-lantai-1 running]=true) do={ 32 | :set eth03status ("Lantai 1 is *CONNECTED*%0A") 33 | } else={ 34 | :set eth03status ("Lantai 1 is *DISCONNECTED*%0A") 35 | } 36 | 37 | :if ([/interface ethernet get eth04-lantai-2 running]=true) do={ 38 | :set eth04status ("Lantai 2 is *CONNECTED*%0A") 39 | } else={ 40 | :set eth04status ("Lantai 2 is *DISCONNECTED*%0A") 41 | } 42 | 43 | :if ([/interface ethernet get eth05-configurator running]=true) do={ 44 | :set eth05status ("Config is *CONNECTED*%0A") 45 | } else={ 46 | :set eth05status ("Config is *DISCONNECTED*%0A") 47 | } 48 | :set output ($output.$eth01status.$eth03status.$eth04status.$eth05status) 49 | $send chat=$chatid text=("$output") mode="Markdown" 50 | } 51 | :if (($param1="show") and ($param2="all")) do={ 52 | local status 53 | local name 54 | foreach i in=[interface find] do={ 55 | set status ($status,[/interface get value-name=running $i]) 56 | set name ($name,[/interface get value-name=name $i]) 57 | } 58 | put $status 59 | put $name 60 | local text 61 | for e from=0 to=([:len [interface find]] - 1) do={ 62 | local change {"true"="Connected";"false"="Disconnected"} 63 | local newstatus ($change->[:pick $status $e]) 64 | local before ("%0AInterface ".[:pick $name $e]." - Status: ".$newstatus) 65 | put $before 66 | set text ($text.$before) 67 | } 68 | put $text 69 | $send chat=$chatid text=("$text") mode="Markdown" 70 | } 71 | -------------------------------------------------------------------------------- /script text/tg_cmd_monitoring: -------------------------------------------------------------------------------- 1 | :local send [:parse [/system script get tg_sendMessage source]] 2 | :local tolower [:parse [/system script get func_lowercase source]] 3 | :local param1 [:pick $params 0 [:find $params " "]] 4 | :local param2 [:pick $params ([:find $params " "]+1) [:len $params]] 5 | :local param3 [:pick [:pick $params ([:find $params " "]+1) [:len $params]] ([:find [:pick $params ([:find $params " "]+1) [:len $params]] " "]+1) [:len [:pick $params ([:find $params " "]+1) [:len $params]]]] 6 | :if ([:len [:find $param2 " "]]>0) do={ 7 | :set param2 [:pick [:pick $params ([:find $params " "]+1) [:len $params]] 0 [:find [:pick $params ([:find $params " "]+1) [:len $params]] " "]] 8 | } else={ 9 | :set param3 "" 10 | } 11 | 12 | :local fconfig [:parse [/system script get tg_config source]] 13 | :local config [$fconfig] 14 | :local botID ($config->"botAPI") 15 | 16 | :local counting 0; 17 | 18 | #======================================================= 19 | #monitoring akan dipaksa berhenti setelah x detik (default 50) 20 | :local forceStopAfter 50; 21 | #======================================================= 22 | 23 | :local paramsLower [$tolower $params] 24 | :local param1Lower [$tolower $param1] 25 | :local param2Lower [$tolower $param2] 26 | :local param3Lower [$tolower $param3] 27 | 28 | :local forceStop do={ 29 | :global monitoring; 30 | :set monitoring false; 31 | :delay 2s; 32 | /system script environment remove monitoring; 33 | /system script job remove [find where script=tg_getUpdates]; 34 | } 35 | 36 | :local prepare do={ 37 | :local gkey [:parse [/system script get tg_getkey source]] 38 | :local fconfig [:parse [/system script get tg_config source]] 39 | :local config [$fconfig] 40 | :local botID ($config->"botAPI") 41 | :local url ("https://api.telegram.org/bot".$botID."/sendMessage\?chat_id=$chatid&text=Preparing...") 42 | :put $url 43 | :local fetching ([/tool fetch url=$url output=user as-value]->"data") 44 | :local msgID [$gkey key="message_id" text=$fetching] 45 | :return $msgID 46 | } 47 | 48 | :global monitoring true; 49 | 50 | :if ($param1Lower = "interface") do={ 51 | :if ([:len [/interface find where name=$param2]] > 0) do={ 52 | :local msgid [$prepare chatid=$chatid] 53 | :put $msgid 54 | :while ($monitoring) do={ 55 | :local rxData ((([/interface monitor-traffic $param2 once as-value]->"rx-bits-per-second")/1000)/1); 56 | :local txData ((([/interface monitor-traffic $param2 once as-value]->"tx-bits-per-second")/1000)/1); 57 | :local text ("$param2%0ARX: $rxData kb/s%0ATX: $txData kb/s"); 58 | /tool fetch url="https://api.telegram.org/bot$botID/editMessageText\?chat_id=$chatid&message_id=$msgid&text=$text" output=none 59 | :set counting ($counting + 1); 60 | :if ($counting > $forceStopAfter) do={$forceStop;} 61 | } 62 | :return true; 63 | 64 | } else={ 65 | $send chat=$chatid text=("Interface $param2 tidak ditemukan") 66 | } 67 | } 68 | 69 | :if ($paramsLower = "cpu") do={ 70 | :local msgid [$prepare chatid=$chatid] 71 | :put $msgid 72 | :local identity ([/system identity get name]) 73 | :while ($monitoring) do={ 74 | :local cpuLoad [/system resource get cpu-load] 75 | :local text ("Router Id: ".$identity."%0ACPU-LOAD: ".$cpuLoad."%25") 76 | /tool fetch url="https://api.telegram.org/bot$botID/editMessageText\?chat_id=$chatid&message_id=$msgid&text=$text" output=none 77 | :set counting ($counting + 1); 78 | :if ($counting > $forceStopAfter) do={$forceStop;} 79 | } 80 | } 81 | 82 | :if (($paramsLower = "ram") or ($paramsLower = "memory")) do={ 83 | :local msgid [$prepare chatid=$chatid] 84 | :put $msgid 85 | :local totalMemory ([/system resource get total-memory] / 1024 / 1024) 86 | :local identity ([/system identity get name]) 87 | :while ($monitoring) do={ 88 | :local clockTime [/system clock get time] 89 | :local freeMemory ([/system resource get free-memory] / 1024 / 1024) 90 | :local text ("Router Id: ".$identity."%0ATime: $clockTime%0AFree Memory: $freeMemory/$totalMemory MiB") 91 | /tool fetch url="https://api.telegram.org/bot$botID/editMessageText\?chat_id=$chatid&message_id=$msgid&text=$text" output=none 92 | :set counting ($counting + 1); 93 | :if ($counting > $forceStopAfter) do={$forceStop;} 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /script text/tg_cmd_ping: -------------------------------------------------------------------------------- 1 | :local send [:parse [/system script get tg_sendMessage source]] 2 | :local param1 [:pick $params 0 [:find $params " "]] 3 | :local param2 [:pick $params ([:find $params " "]+1) [:len $params]] 4 | 5 | :put $params 6 | :put $param1 7 | :put $param2 8 | :put $chatid 9 | :put $from 10 | 11 | :if ($param1="to") do={ 12 | #Ping Variables 13 | :local avgRtt; 14 | :local pin 15 | :local pout 16 | :local datetime "$[/system clock get date] $[/system clock get time]" 17 | #Ping it real good 18 | /tool flood-ping $param2 count=10 do={ 19 | 20 | :if ($sent = 10) do={ 21 | 22 | :set avgRtt $"avg-rtt" 23 | 24 | :set pout $sent 25 | 26 | :set pin $received 27 | } 28 | 29 | } 30 | 31 | :local ploss (100 - (($pin * 100) / $pout)) 32 | 33 | :local logmsg ("Ping Average for $param2 - ".[:tostr $avgRtt]."ms - packet loss: ".[:tostr $ploss]."%") 34 | 35 | :log info $logmsg 36 | 37 | :local text "Router ID:* $[/system identity get name] * %0A\ 38 | Date : _$datetime_%0A\ 39 | IP: _$param2_%0A\ 40 | Result:%0A_$logmsg_" 41 | $send chat=$chatid text=$text mode="Markdown" 42 | :return true 43 | } else={ 44 | #Ping Variables 45 | :local avgRtt; 46 | :local pin 47 | :local pout 48 | :local datetime "$[/system clock get date] $[/system clock get time]" 49 | #Ping it real good 50 | /tool flood-ping 8.8.8.8 count=10 do={ 51 | 52 | :if ($sent = 10) do={ 53 | 54 | :set avgRtt $"avg-rtt" 55 | 56 | :set pout $sent 57 | 58 | :set pin $received 59 | } 60 | 61 | } 62 | 63 | :local ploss (100 - (($pin * 100) / $pout)) 64 | 65 | :local logmsg ("Ping Average for 8.8.8.8 - ".[:tostr $avgRtt]."ms - packet loss: ".[:tostr $ploss]."%") 66 | 67 | :log info $logmsg 68 | 69 | :local text "Router ID:* $[/system identity get name] * %0A\ 70 | Date : _$datetime_%0A\ 71 | IP: _8.8.8.8_%0A\ 72 | Result:%0A_$logmsg_" 73 | $send chat=$chatid text=$text mode="Markdown" 74 | :return true 75 | } -------------------------------------------------------------------------------- /script text/tg_cmd_public: -------------------------------------------------------------------------------- 1 | :local send [:parse [/system script get tg_sendMessage source]] 2 | :put $params 3 | :put $chatid 4 | :put $from 5 | 6 | :local public; 7 | 8 | :local ddns; 9 | 10 | :set public [/ip cloud get public-address]; 11 | 12 | :set ddns [/ip cloud get dns-name]; 13 | 14 | :local text "DDNS : $ddns : IP Public : $public" 15 | 16 | 17 | $send chat=$chatid text=$text mode="Markdown" -------------------------------------------------------------------------------- /script text/tg_cmd_reboot: -------------------------------------------------------------------------------- 1 | :local send [:parse [/system script get tg_sendMessage source]] 2 | :put $params 3 | :put $chatid 4 | :put $from 5 | 6 | :local text "Menghidupkan ulang router dalam 30 detik..." 7 | 8 | $send chat=$chatid text=$text mode="Markdown" 9 | 10 | :delay 30 11 | system reboot -------------------------------------------------------------------------------- /script text/tg_cmd_stop: -------------------------------------------------------------------------------- 1 | :global monitoring; 2 | :set monitoring false; 3 | :delay 5s; 4 | /system script environment remove monitoring; 5 | -------------------------------------------------------------------------------- /script text/tg_config: -------------------------------------------------------------------------------- 1 | ###################################### 2 | # Telegram bot API, VVS/BlackVS 2017 3 | # Config file 4 | ###################################### 5 | #:log info "telegram configuration file has been loaded"; 6 | 7 | # to use config insert next lines: 8 | #:local fconfig [:parse [/system script get tg_config source]] 9 | #:local config [$fconfig] 10 | #:put $config 11 | 12 | ###################################### 13 | # Common parameters 14 | ###################################### 15 | 16 | :local config { 17 | "Command"="telegram"; 18 | "botAPI"="xxxxxxxxxx:xxxxxxxxxxxxxxx-xxxxxxxxxxx"; 19 | "defaultChatID"="xxxxxxxxx"; 20 | "trusted"="xxxxxxxxx, -xxxxxxxxx"; 21 | "storage"=""; 22 | "timeout"=5; 23 | "refresh_active"=15; 24 | "refresh_standby"=300; 25 | } 26 | return $config -------------------------------------------------------------------------------- /script text/tg_getUpdates: -------------------------------------------------------------------------------- 1 | :global TGLASTMSGID 2 | :global TGLASTUPDID 3 | 4 | :local fconfig [:parse [/system script get tg_config source]] 5 | :local http [:parse [/system script get func_fetch source]] 6 | :local gkey [:parse [/system script get tg_getkey source]] 7 | :local send [:parse [/system script get tg_sendMessage source]] 8 | 9 | :local cfg [$fconfig] 10 | :local trusted [:toarray ($cfg->"trusted")] 11 | :local botID ($cfg->"botAPI") 12 | :local storage ($cfg->"storage") 13 | :local timeout ($cfg->"timeout") 14 | 15 | :put "cfg=$cfg" 16 | :put "trusted=$trusted" 17 | :put "botID=$botID" 18 | :put "storage=$storage" 19 | :put "timeout=$timeout" 20 | 21 | :local file ($storage."tg_get_updates.txt") 22 | :local logfile ($storage."tg_fetch_log.txt") 23 | #get 1 message per time 24 | :local url ("https://api.telegram.org/bot".$botID."/getUpdates?timeout=$timeout&limit=1") 25 | :if ([:len $TGLASTUPDID]>0) do={ 26 | :set url "$url&offset=$($TGLASTUPDID+1)" 27 | } 28 | 29 | :put "Reading updates..." 30 | :local res [$http dst-path=$file url=$url resfile=$logfile] 31 | :if ($res!="success") do={ 32 | :put "Error getting updates" 33 | return "Failed get updates" 34 | } 35 | :put "Finished to read updates." 36 | 37 | :local content [/file get [/file find name=$file] contents] 38 | 39 | :local msgid [$gkey key="message_id" text=$content] 40 | :if ($msgid="") do={ 41 | :put "No new updates" 42 | :return 0 43 | } 44 | :set TGLASTMSGID $msgid 45 | 46 | :local updid [$gkey key="update_id" text=$content] 47 | :set TGLASTUPDID $updid 48 | 49 | :local fromid [$gkey block="from" key="id" text=$content] 50 | :local username [$gkey block="from" key="username" text=$content] 51 | :local firstname [$gkey block="from" key="first_name" text=$content] 52 | :local lastname [$gkey block="from" key="last_name" text=$content] 53 | :local chatid [$gkey block="chat" key="id" text=$content] 54 | :local chattext [$gkey block="chat" key="text" text=$content] 55 | 56 | :put "message id=$msgid" 57 | :put "update id=$updid" 58 | :put "from id=$fromid" 59 | :put "first name=$firstname" 60 | :put "last name=$lastname" 61 | :put "username=$username" 62 | :local name "$firstname $lastname" 63 | :if ([:len $name]<2) do { 64 | :set name $username 65 | } 66 | 67 | :put "in chat=$chatid" 68 | :put "command=$chattext" 69 | 70 | :local allowed ( [:type [:find $trusted $fromid]]!="nil" or [:type [:find $trusted $chatid]]!="nil") 71 | :if (!$allowed) do={ 72 | :put "Unknown sender, keep silence" 73 | :return -1 74 | } 75 | 76 | :local cmd "" 77 | :local params "" 78 | :local ltext [:len $chattext] 79 | 80 | :local pos [:find $chattext " "] 81 | :if ([:type $pos]="nil") do={ 82 | :set cmd [:pick $chattext 1 $ltext] 83 | } else={ 84 | :set cmd [:pick $chattext 1 $pos] 85 | :set params [:pick $chattext ($pos+1) $ltext] 86 | } 87 | 88 | :local pos [:find $cmd "@"] 89 | :if ([:type $pos]!="nil") do={ 90 | :set cmd [:pick $cmd 0 $pos] 91 | } 92 | 93 | 94 | :put "cmd=<$cmd>" 95 | 96 | :local alternativeCommand {"hi"="help"; "start"="help"; "bantuan"="help"; "hello"="help"; "halo"="help"; "hai"="help"; "hs"="hotspot"; "iface"="interface";\ 97 | "hotspotenable"="enablehotspot"; "hotspotdisable"="disablehotspot"} 98 | :if ([:typeof ($alternativeCommand -> $cmd)] = "str") do={:set cmd ($alternativeCommand -> $cmd); :put "cmd=<$cmd>"} 99 | 100 | :put "params=<$params>" 101 | 102 | :global TGLASTCMD $cmd 103 | 104 | :put "Try to invoke external script tg_cmd_$cmd" 105 | :local script [:parse [/system script get "tg_cmd_$cmd" source]] 106 | $script params=$params chatid=$chatid from=$name -------------------------------------------------------------------------------- /script text/tg_getkey: -------------------------------------------------------------------------------- 1 | :local cur 0 2 | :local lkey [:len $key] 3 | :local res "" 4 | :local p 5 | 6 | :if ([:len $block]>0) do={ 7 | :set p [:find $text $block $cur] 8 | :if ([:type $p]="nil") do={ 9 | :return $res 10 | } 11 | :set cur ($p+[:len $block]+2) 12 | } 13 | 14 | :set p [:find $text $key $cur] 15 | :if ([:type $p]!="nil") do={ 16 | :set cur ($p+lkey+2) 17 | :set p [:find $text "," $cur] 18 | :if ([:type $p]!="nil") do={ 19 | if ([:pick $text $cur]="\"") do={ 20 | :set res [:pick $text ($cur+1) ($p-1)] 21 | } else={ 22 | :set res [:pick $text $cur $p] 23 | } 24 | } 25 | } 26 | :return $res -------------------------------------------------------------------------------- /script text/tg_sendMessage: -------------------------------------------------------------------------------- 1 | :local fconfig [:parse [/system script get tg_config source]] 2 | 3 | :local cfg [$fconfig] 4 | :local chatID ($cfg->"defaultChatID") 5 | :local botID ($cfg->"botAPI") 6 | :local storage ($cfg->"storage") 7 | 8 | :if ([:len $chat]>0) do={:set chatID $chat} 9 | 10 | :local url "https://api.telegram.org/bot$botID/sendmessage?chat_id=$chatID&text=$text" 11 | :if ([:len $mode]>0) do={:set url ($url."&parse_mode=$mode")} 12 | 13 | :local file ($tgStorage."tg_get_updates.txt") 14 | :local logfile ($tgStorage."tg_fetch_log.txt") 15 | 16 | /tool fetch url=$url keep-result=no -------------------------------------------------------------------------------- /telegram_bot.rsc: -------------------------------------------------------------------------------- 1 | # Custom Script v1.2 by DwiChan0905 2 | # MikroTik Script for Telegram Bot 3 | # Tested on MikroTik RB750 with RouterOS ver. 6.45.5 4 | # just upload this file to your MikroTik's FTP. Then, import this file to apply the scripts. 5 | # Telegram Bot Configurations is in tg_config. 6 | 7 | /system scheduler 8 | add interval=10s name=Telegram on-event="/system script run tg_getUpdates" \ 9 | policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon \ 10 | start-date=sep/04/2019 start-time=18:28:04 11 | add name="Reboot Report" on-event=\ 12 | ":delay 30\r\ 13 | \n/system script run reboot-report" policy=\ 14 | ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon \ 15 | start-time=startup 16 | /system script 17 | add name=tg_getUpdates policy=read \ 18 | source=":global TGLASTMSGID\r\ 19 | \n:global TGLASTUPDID\r\ 20 | \n\r\ 21 | \n:local fconfig [:parse [/system script get tg_config source]]\r\ 22 | \n:local http [:parse [/system script get func_fetch source]]\r\ 23 | \n:local gkey [:parse [/system script get tg_getkey source]]\r\ 24 | \n:local send [:parse [/system script get tg_sendMessage source]]\r\ 25 | \n\r\ 26 | \n:local cfg [\$fconfig]\r\ 27 | \n:local trusted [:toarray (\$cfg->\"trusted\")]\r\ 28 | \n:local botID (\$cfg->\"botAPI\")\r\ 29 | \n:local storage (\$cfg->\"storage\")\r\ 30 | \n:local timeout (\$cfg->\"timeout\")\r\ 31 | \n\r\ 32 | \n:put \"cfg=\$cfg\"\r\ 33 | \n:put \"trusted=\$trusted\"\r\ 34 | \n:put \"botID=\$botID\"\r\ 35 | \n:put \"storage=\$storage\"\r\ 36 | \n:put \"timeout=\$timeout\"\r\ 37 | \n\r\ 38 | \n:local file (\$storage.\"tg_get_updates.txt\")\r\ 39 | \n:local logfile (\$storage.\"tg_fetch_log.txt\")\r\ 40 | \n#get 1 message per time\r\ 41 | \n:local url (\"https://api.telegram.org/bot\".\$botID.\"/getUpdates\?time\ 42 | out=\$timeout&limit=1\")\r\ 43 | \n:if ([:len \$TGLASTUPDID]>0) do={\r\ 44 | \n :set url \"\$url&offset=\$(\$TGLASTUPDID+1)\"\r\ 45 | \n}\r\ 46 | \n\r\ 47 | \n:put \"Reading updates...\"\r\ 48 | \n:local res [\$http dst-path=\$file url=\$url resfile=\$logfile]\r\ 49 | \n:if (\$res!=\"success\") do={\r\ 50 | \n :put \"Error getting updates\"\r\ 51 | \n return \"Failed get updates\"\r\ 52 | \n}\r\ 53 | \n:put \"Finished to read updates.\"\r\ 54 | \n\r\ 55 | \n:local content [/file get [/file find name=\$file] contents]\r\ 56 | \n\r\ 57 | \n:local msgid [\$gkey key=\"message_id\" text=\$content]\r\ 58 | \n:if (\$msgid=\"\") do={ \r\ 59 | \n :put \"No new updates\"\r\ 60 | \n :return 0 \r\ 61 | \n}\r\ 62 | \n:set TGLASTMSGID \$msgid\r\ 63 | \n\r\ 64 | \n:local updid [\$gkey key=\"update_id\" text=\$content]\r\ 65 | \n:set TGLASTUPDID \$updid\r\ 66 | \n\r\ 67 | \n:local fromid [\$gkey block=\"from\" key=\"id\" text=\$content]\r\ 68 | \n:local username [\$gkey block=\"from\" key=\"username\" text=\$content]\ 69 | \r\ 70 | \n:local firstname [\$gkey block=\"from\" key=\"first_name\" text=\$conten\ 71 | t]\r\ 72 | \n:local lastname [\$gkey block=\"from\" key=\"last_name\" text=\$content]\ 73 | \r\ 74 | \n:local chatid [\$gkey block=\"chat\" key=\"id\" text=\$content]\r\ 75 | \n:local chattext [\$gkey block=\"chat\" key=\"text\" text=\$content]\r\ 76 | \n\r\ 77 | \n:put \"message id=\$msgid\"\r\ 78 | \n:put \"update id=\$updid\"\r\ 79 | \n:put \"from id=\$fromid\"\r\ 80 | \n:put \"first name=\$firstname\"\r\ 81 | \n:put \"last name=\$lastname\"\r\ 82 | \n:put \"username=\$username\"\r\ 83 | \n:local name \"\$firstname \$lastname\"\r\ 84 | \n:if ([:len \$name]<2) do {\r\ 85 | \n :set name \$username\r\ 86 | \n}\r\ 87 | \n\r\ 88 | \n:put \"in chat=\$chatid\"\r\ 89 | \n:put \"command=\$chattext\"\r\ 90 | \n\r\ 91 | \n:local allowed ( [:type [:find \$trusted \$fromid]]!=\"nil\" or [:type [\ 92 | :find \$trusted \$chatid]]!=\"nil\")\r\ 93 | \n:if (!\$allowed) do={\r\ 94 | \n :put \"Unknown sender, keep silence\"\r\ 95 | \n :return -1\r\ 96 | \n}\r\ 97 | \n\r\ 98 | \n:local cmd \"\"\r\ 99 | \n:local params \"\"\r\ 100 | \n:local ltext [:len \$chattext]\r\ 101 | \n\r\ 102 | \n:local pos [:find \$chattext \" \"]\r\ 103 | \n:if ([:type \$pos]=\"nil\") do={\r\ 104 | \n :set cmd [:pick \$chattext 1 \$ltext]\r\ 105 | \n} else={\r\ 106 | \n :set cmd [:pick \$chattext 1 \$pos]\r\ 107 | \n :set params [:pick \$chattext (\$pos+1) \$ltext]\r\ 108 | \n}\r\ 109 | \n\r\ 110 | \n:local pos [:find \$cmd \"@\"]\r\ 111 | \n:if ([:type \$pos]!=\"nil\") do={\r\ 112 | \n :set cmd [:pick \$cmd 0 \$pos]\r\ 113 | \n}\r\ 114 | \n\r\ 115 | \n\r\ 116 | \n:put \"cmd=<\$cmd>\"\r\ 117 | \n\r\ 118 | \n:local alternativeCommand {\"hi\"=\"help\"; \"start\"=\"help\"; \"bantua\ 119 | n\"=\"help\"; \"hello\"=\"help\"; \"halo\"=\"help\"; \"hai\"=\"help\"; \"h\ 120 | s\"=\"hotspot\"; \"iface\"=\"interface\";\\\r\ 121 | \n \"hotspotenable\"=\"enablehotspot\"; \"hotspot\ 122 | disable\"=\"disablehotspot\"; \"monitor\"=\"monitoring\"; \"berhenti\"=\"s\ 123 | top\"; \"watch\"=\"monitoring\";\\\r\ 124 | \n \"restart\"=\"reboot\"}\r\ 125 | \n:if ([:typeof (\$alternativeCommand -> \$cmd)] = \"str\") do={:set cmd (\ 126 | \$alternativeCommand -> \$cmd); :put \"cmd=<\$cmd>\"}\r\ 127 | \n\r\ 128 | \n:put \"params=<\$params>\"\r\ 129 | \n\r\ 130 | \n:global TGLASTCMD \$cmd\r\ 131 | \n\r\ 132 | \n:put \"Try to invoke external script tg_cmd_\$cmd\"\r\ 133 | \n:local script [:parse [/system script get \"tg_cmd_\$cmd\" source]]\r\ 134 | \n\$script params=\$params chatid=\$chatid from=\$name" 135 | add name=func_fetch policy=ftp,read,write,policy,test source="####\ 136 | #####################################################\r\ 137 | \n# Wrapper for /tools fetch\r\ 138 | \n# Input:\r\ 139 | \n# mode\r\ 140 | \n# upload=yes/no\r\ 141 | \n# user\r\ 142 | \n# password\r\ 143 | \n# address\r\ 144 | \n# host\r\ 145 | \n# httpdata\r\ 146 | \n# httpmethod\r\ 147 | \n# check-certificate\r\ 148 | \n# src-path\r\ 149 | \n# dst-path\r\ 150 | \n# ascii=yes/no\r\ 151 | \n# url\r\ 152 | \n# resfile\r\ 153 | \n\r\ 154 | \n:local res \"fetchresult.txt\"\r\ 155 | \n:if ([:len \$resfile]>0) do={:set res \$resfile}\r\ 156 | \n#:put \$res\r\ 157 | \n\r\ 158 | \n:local cmd \"/tool fetch\"\r\ 159 | \n:if ([:len \$mode]>0) do={:set cmd \"\$cmd mode=\$mode\"}\r\ 160 | \n:if ([:len \$upload]>0) do={:set cmd \"\$cmd upload=\$upload\"}\r\ 161 | \n:if ([:len \$user]>0) do={:set cmd \"\$cmd user=\\\"\$user\\\"\"}\r\ 162 | \n:if ([:len \$password]>0) do={:set cmd \"\$cmd password=\\\"\$password\\\ 163 | \"\"}\r\ 164 | \n:if ([:len \$address]>0) do={:set cmd \"\$cmd address=\\\"\$address\\\"\ 165 | \"}\r\ 166 | \n:if ([:len \$host]>0) do={:set cmd \"\$cmd host=\\\"\$host\\\"\"}\r\ 167 | \n:if ([:len \$\"http-data\"]>0) do={:set cmd \"\$cmd http-data=\\\"\$\"ht\ 168 | tp-data\"\\\"\"}\r\ 169 | \n:if ([:len \$\"http-method\"]>0) do={:set cmd \"\$cmd http-method=\\\"\$\ 170 | \"http-method\"\\\"\"}\r\ 171 | \n:if ([:len \$\"check-certificate\"]>0) do={:set cmd \"\$cmd check-certif\ 172 | icate=\\\"\$\"check-certificate\"\\\"\"}\r\ 173 | \n:if ([:len \$\"src-path\"]>0) do={:set cmd \"\$cmd src-path=\\\"\$\"src-\ 174 | path\"\\\"\"}\r\ 175 | \n:if ([:len \$\"dst-path\"]>0) do={:set cmd \"\$cmd dst-path=\\\"\$\"dst-\ 176 | path\"\\\"\"}\r\ 177 | \n:if ([:len \$ascii]>0) do={:set cmd \"\$cmd ascii=\\\"\$ascii\\\"\"}\r\ 178 | \n:if ([:len \$url]>0) do={:set cmd \"\$cmd url=\\\"\$url\\\"\"}\r\ 179 | \n\r\ 180 | \n:put \">> \$cmd\"\r\ 181 | \n\r\ 182 | \n:global FETCHRESULT\r\ 183 | \n:set FETCHRESULT \"none\"\r\ 184 | \n\r\ 185 | \n:local script \"\\\r\ 186 | \n :global FETCHRESULT;\\\r\ 187 | \n :do {\\\r\ 188 | \n \$cmd;\\\r\ 189 | \n :set FETCHRESULT \\\"success\\\";\\\r\ 190 | \n } on-error={\\\r\ 191 | \n :set FETCHRESULT \\\"failed\\\";\\\r\ 192 | \n }\\\r\ 193 | \n\"\r\ 194 | \n:execute script=\$script file=\$res\r\ 195 | \n:local cnt 0\r\ 196 | \n#:put \"\$cnt -> \$FETCHRESULT\"\r\ 197 | \n:while (\$cnt<100 and \$FETCHRESULT=\"none\") do={ \r\ 198 | \n :delay 1s\r\ 199 | \n :set \$cnt (\$cnt+1)\r\ 200 | \n #:put \"\$cnt -> \$FETCHRESULT\"\r\ 201 | \n}\r\ 202 | \n:local content [/file get [find name=\$res] content]\r\ 203 | \n#:put \$content\r\ 204 | \nif (\$content~\"finished\") do={:return \"success\"}\r\ 205 | \n:return \$FETCHRESULT" 206 | add name=tg_getkey policy=read source=":local cur 0\r\ 207 | \n:local lkey [:len \$key]\r\ 208 | \n:local res \"\"\r\ 209 | \n:local p\r\ 210 | \n\r\ 211 | \n:if ([:len \$block]>0) do={\r\ 212 | \n :set p [:find \$text \$block \$cur]\r\ 213 | \n :if ([:type \$p]=\"nil\") do={\r\ 214 | \n :return \$res\r\ 215 | \n }\r\ 216 | \n :set cur (\$p+[:len \$block]+2)\r\ 217 | \n}\r\ 218 | \n\r\ 219 | \n:set p [:find \$text \$key \$cur]\r\ 220 | \n:if ([:type \$p]!=\"nil\") do={\r\ 221 | \n :set cur (\$p+lkey+2)\r\ 222 | \n :set p [:find \$text \",\" \$cur]\r\ 223 | \n :if ([:type \$p]!=\"nil\") do={\r\ 224 | \n if ([:pick \$text \$cur]=\"\\\"\") do={\r\ 225 | \n :set res [:pick \$text (\$cur+1) (\$p-1)]\r\ 226 | \n } else={\r\ 227 | \n :set res [:pick \$text \$cur \$p]\r\ 228 | \n }\r\ 229 | \n } \r\ 230 | \n}\r\ 231 | \n:return \$res" 232 | add name=tg_sendMessage policy=read source=":local fconfig [:parse\ 233 | \_[/system script get tg_config source]]\r\ 234 | \n\r\ 235 | \n:local cfg [\$fconfig]\r\ 236 | \n:local chatID (\$cfg->\"defaultChatID\")\r\ 237 | \n:local botID (\$cfg->\"botAPI\")\r\ 238 | \n:local storage (\$cfg->\"storage\")\r\ 239 | \n\r\ 240 | \n:if ([:len \$chat]>0) do={:set chatID \$chat}\r\ 241 | \n\r\ 242 | \n:local url \"https://api.telegram.org/bot\$botID/sendmessage\?chat_id=\$\ 243 | chatID&text=\$text\"\r\ 244 | \n:if ([:len \$mode]>0) do={:set url (\$url.\"&parse_mode=\$mode\")}\r\ 245 | \n\r\ 246 | \n:local file (\$tgStorage.\"tg_get_updates.txt\")\r\ 247 | \n:local logfile (\$tgStorage.\"tg_fetch_log.txt\")\r\ 248 | \n\r\ 249 | \n/tool fetch url=\$url keep-result=no" 250 | add name=tg_cmd_cpu policy=read source=":local send [:parse [/syst\ 251 | em script get tg_sendMessage source]]\r\ 252 | \n:local hotspot [:len [/ip hotspot active find]]\r\ 253 | \n\r\ 254 | \n:put \$params\r\ 255 | \n:put \$chatid\r\ 256 | \n:put \$from\r\ 257 | \n \r\ 258 | \n:local text \"Router ID:* \$[/system identity get name] * %0A\\\r\ 259 | \nUptime: _\$[/system resource get uptime]_%0A\\\r\ 260 | \nCPU Load: _\$[/system resource get cpu-load]%_%0A\\\r\ 261 | \nRAM: _\$(([/system resource get total-memory]-[/system resource get free\ 262 | -memory])/(1024*1024))M/\$([/system resource get total-memory]/(1024*1024)\ 263 | )M_\"\r\ 264 | \n \r\ 265 | \n\$send chat=\$chatid text=\$text mode=\"Markdown\"\r\ 266 | \n:return true" 267 | add name=tg_cmd_public policy=read source=":local send [:parse [/s\ 268 | ystem script get tg_sendMessage source]]\r\ 269 | \n:put \$params\r\ 270 | \n:put \$chatid\r\ 271 | \n:put \$from\r\ 272 | \n\r\ 273 | \n:local public;\r\ 274 | \n\ 275 | \n:local ddns;\r\ 276 | \n\ 277 | \n:set public [/ip cloud get public-address];\r\ 278 | \n\ 279 | \n:set ddns [/ip cloud get dns-name];\r\ 280 | \n\ 281 | \n:local text \"DDNS : \$ddns : IP Public : \$public\"\r\ 282 | \n\r\ 283 | \n\r\ 284 | \n\$send chat=\$chatid text=\$text mode=\"Markdown\"" 285 | add name=tg_cmd_ping policy=\ 286 | ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source=":\ 287 | local send [:parse [/system script get tg_sendMessage source]]\r\ 288 | \n:local param1 [:pick \$params 0 [:find \$params \" \"]]\r\ 289 | \n:local param2 [:pick \$params ([:find \$params \" \"]+1) [:len \$params]\ 290 | ]\r\ 291 | \n\r\ 292 | \n:put \$params\r\ 293 | \n:put \$param1\r\ 294 | \n:put \$param2\r\ 295 | \n:put \$chatid\r\ 296 | \n:put \$from\r\ 297 | \n\r\ 298 | \n:if (\$param1=\"to\") do={\r\ 299 | \n#Ping Variables\r\ 300 | \n:local avgRtt;\r\ 301 | \n:local pin\r\ 302 | \n:local pout\r\ 303 | \n:local datetime \"\$[/system clock get date] \$[/system clock get time]\ 304 | \"\r\ 305 | \n#Ping it real good\r\ 306 | \n/tool flood-ping \$param2 count=10 do={\r\ 307 | \n \r\ 308 | \n:if (\$sent = 10) do={\r\ 309 | \n \r\ 310 | \n:set avgRtt \$\"avg-rtt\"\r\ 311 | \n \r\ 312 | \n:set pout \$sent\r\ 313 | \n \r\ 314 | \n:set pin \$received\r\ 315 | \n }\r\ 316 | \n\r\ 317 | \n}\r\ 318 | \n\r\ 319 | \n:local ploss (100 - ((\$pin * 100) / \$pout))\r\ 320 | \n\r\ 321 | \n:local logmsg (\"Ping Average for \$param2 - \".[:tostr \$avgRtt].\"ms -\ 322 | \_packet loss: \".[:tostr \$ploss].\"%\")\r\ 323 | \n\r\ 324 | \n:log info \$logmsg\r\ 325 | \n\r\ 326 | \n:local text \"Router ID:* \$[/system identity get name] * %0A\\\r\ 327 | \nDate : _\$datetime_%0A\\\r\ 328 | \nIP: _\$param2_%0A\\\r\ 329 | \nResult:%0A_\$logmsg_\"\r\ 330 | \n\$send chat=\$chatid text=\$text mode=\"Markdown\"\r\ 331 | \n:return true\r\ 332 | \n} else={\r\ 333 | \n#Ping Variables\r\ 334 | \n:local avgRtt;\r\ 335 | \n:local pin\r\ 336 | \n:local pout\r\ 337 | \n:local datetime \"\$[/system clock get date] \$[/system clock get time]\ 338 | \"\r\ 339 | \n#Ping it real good\r\ 340 | \n/tool flood-ping 8.8.8.8 count=10 do={\r\ 341 | \n \r\ 342 | \n:if (\$sent = 10) do={\r\ 343 | \n \r\ 344 | \n:set avgRtt \$\"avg-rtt\"\r\ 345 | \n \r\ 346 | \n:set pout \$sent\r\ 347 | \n \r\ 348 | \n:set pin \$received\r\ 349 | \n }\r\ 350 | \n\r\ 351 | \n}\r\ 352 | \n\r\ 353 | \n:local ploss (100 - ((\$pin * 100) / \$pout))\r\ 354 | \n\r\ 355 | \n:local logmsg (\"Ping Average for 8.8.8.8 - \".[:tostr \$avgRtt].\"ms - \ 356 | packet loss: \".[:tostr \$ploss].\"%\")\r\ 357 | \n\r\ 358 | \n:log info \$logmsg\r\ 359 | \n\r\ 360 | \n:local text \"Router ID:* \$[/system identity get name] * %0A\\\r\ 361 | \nDate : _\$datetime_%0A\\\r\ 362 | \nIP: _8.8.8.8_%0A\\\r\ 363 | \nResult:%0A_\$logmsg_\"\r\ 364 | \n\$send chat=\$chatid text=\$text mode=\"Markdown\"\r\ 365 | \n:return true\r\ 366 | \n}" 367 | add name=tg_cmd_disablehotspot policy=\ 368 | ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source=":\ 369 | local send [:parse [/system script get tg_sendMessage source]]\r\ 370 | \n:put \$params\r\ 371 | \n:put \$chatid\r\ 372 | \n:put \$from\r\ 373 | \n\r\ 374 | \n:local text \"All hotspots disabled\"\r\ 375 | \n\r\ 376 | \n\$send chat=\$chatid text=\$text mode=\"Markdown\"\r\ 377 | \n/ip hotspot disable lantai1\r\ 378 | \n/ip hotspot disable lantai2" 379 | add name=tg_cmd_enablehotspot policy=\ 380 | ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source=":\ 381 | local send [:parse [/system script get tg_sendMessage source]]\r\ 382 | \n:put \$params\r\ 383 | \n:put \$chatid\r\ 384 | \n:put \$from\r\ 385 | \n\r\ 386 | \n:local text \"All hotspots enabled\"\r\ 387 | \n\r\ 388 | \n\$send chat=\$chatid text=\$text mode=\"Markdown\"\r\ 389 | \n/ip hotspot enable lantai1\r\ 390 | \n/ip hotspot enable lantai2" 391 | add name=tg_cmd_forceupdateddns policy=\ 392 | ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source=":\ 393 | local send [:parse [/system script get tg_sendMessage source]]\r\ 394 | \n:put \$params\r\ 395 | \n:put \$chatid\r\ 396 | \n:put \$from\r\ 397 | \n\r\ 398 | \n:local text \"Force Update DDNS\"\r\ 399 | \n\r\ 400 | \n\$send chat=\$chatid text=\$text mode=\"Markdown\"\r\ 401 | \n/ip cloud force-update" 402 | add name=tg_config policy=\ 403 | ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source="#\ 404 | #####################################\r\ 405 | \n# Telegram bot API, VVS/BlackVS 2017\r\ 406 | \n# Config file\r\ 407 | \n######################################\r\ 408 | \n:log info \"telegram configuration file has been loaded\";\r\ 409 | \n\r\ 410 | \n# to use config insert next lines:\r\ 411 | \n#:local fconfig [:parse [/system script get tg_config source]]\r\ 412 | \n#:local config [\$fconfig]\r\ 413 | \n#:put \$config\r\ 414 | \n\r\ 415 | \n######################################\r\ 416 | \n# Common parameters\r\ 417 | \n######################################\r\ 418 | \n\r\ 419 | \n:local config {\r\ 420 | \n\"Command\"=\"telegram\";\r\ 421 | \n\t\"botAPI\"=\"xxxxxxxxxx:xxxxxxxxxxxxxxx-xxxxxxxxxxx\";\r\ 422 | \n\t\"defaultChatID\"=\"xxxxxxxxx\";\r\ 423 | \n\t\"trusted\"=\"xxxxxxxxx, -xxxxxxxxx\";\r\ 424 | \n\t\"storage\"=\"\";\r\ 425 | \n\t\"timeout\"=5;\r\ 426 | \n\t\"refresh_active\"=15;\r\ 427 | \n\t\"refresh_standby\"=300;\r\ 428 | \n}\r\ 429 | \nreturn \$config" 430 | add name=tg_cmd_hotspot policy=read \ 431 | source=":local send [:parse [/system script get tg_sendMessage source]]\r\ 432 | \n:local tolower [:parse [/system script get func_lowercase source]]\r\ 433 | \n:local param1 [:pick \$params 0 [:find \$params \" \"]]\r\ 434 | \n:local param2 [:pick \$params ([:find \$params \" \"]+1) [:len \$params]\ 435 | ]\r\ 436 | \n:local param3 [:pick [:pick \$params ([:find \$params \" \"]+1) [:len \$\ 437 | params]] ([:find [:pick \$params ([:find \$params \" \"]+1) [:len \$params\ 438 | ]] \" \"]+1) [:len [:pick \$params ([:find \$params \" \"]+1) [:len \$para\ 439 | ms]]]]\r\ 440 | \n:if ([:len [:find \$param2 \" \"]]>0) do={\r\ 441 | \n\t:set param2 [:pick [:pick \$params ([:find \$params \" \"]+1) [:len \$\ 442 | params]] 0 [:find [:pick \$params ([:find \$params \" \"]+1) [:len \$param\ 443 | s]] \" \"]]\r\ 444 | \n} else={\r\ 445 | \n\t:set param3 \"\"\r\ 446 | \n}\r\ 447 | \n\r\ 448 | \n:put \$params\r\ 449 | \n:put \$param1\r\ 450 | \n:put \$param2\r\ 451 | \n:put \$param3\r\ 452 | \n:put \$chatid\r\ 453 | \n:put \$from\r\ 454 | \n\r\ 455 | \n:local paramsLower [\$tolower \$params]\r\ 456 | \n:local param1Lower [\$tolower \$param1]\r\ 457 | \n:local param2Lower [\$tolower \$param2]\r\ 458 | \n:local param3Lower [\$tolower \$param3]\r\ 459 | \n\r\ 460 | \n:local sendHelp do={\r\ 461 | \n\t:local send [:parse [/system script get tg_sendMessage source]]\r\ 462 | \n\t:local txt \r\ 463 | \n\t:set txt (\"*Daftar parameter:*%0A%0A\")\r\ 464 | \n\t:set txt (\$txt.\"> session count - Menampilkan jumlah user yang seda\ 465 | ng aktif %0ACth: _/hotspot session showall_%0A\")\r\ 466 | \n\t:set txt (\$txt.\"> session showall - Menampilkan seluruh detail user\ 467 | \_yang sedang aktif %0ACth: _/hotspot session showall_%0A\")\r\ 468 | \n\t:set txt (\$txt.\"> session deauth-by-user __ - Mencabut se\ 469 | si perangkat berdasarkan Username. %0ACth: _/hotspot session deauth-by-use\ 470 | r admin_%0A\")\r\ 471 | \n\t:set txt (\$txt.\"> session deauth-by-ip __ - Mencabut sesi peran\ 472 | gkat berdasarkan Alamat IP. %0ACth: _/hotspot session deauth-by-ip 192.168\ 473 | .1.1_%0A\")\r\ 474 | \n\t:set txt (\$txt.\"> session deauth-by-mac __ - Mencabut sesi per\ 475 | angkat berdasarkan Alamat MAC. %0ACth: _/hotspot session deauth-by-mac AB:\ 476 | CD:EF:01:23:45_%0A\")\r\ 477 | \n\t:set txt (\$txt.\"> add _ _ - Menambah user baru. %0A\ 478 | Cth: _/hotspot add admin admin123_%0A\")\r\ 479 | \n\t:set txt (\$txt.\"> delete __ - Menghapus user. %0ACth: _/hotsp\ 480 | ot delete admin _%0A\")\r\ 481 | \n\t:set txt (\$txt.\"> disable __ - menonaktifkan user. %0ACth: _/\ 482 | hotspot disable admin _%0A\")\r\ 483 | \n\t:set txt (\$txt.\"> enable __ - mengaktifkan user. %0ACth: _/ho\ 484 | tspot enable admin _%0A\")\r\ 485 | \n\t:set txt (\$txt.\"> change-password _ _ - mengganti p\ 486 | assword user. %0ACth: _/hotspot change-password admin p4sSw_%0A%0A\")\r\ 487 | \n\t\$send chat=\$chatid text=(\"\$txt\") mode=\"Markdown\"\r\ 488 | \n}\r\ 489 | \n\r\ 490 | \nif (\$paramsLower = \"help\") do={\r\ 491 | \n\t\$sendHelp\r\ 492 | \n}\r\ 493 | \n\r\ 494 | \n:if (\$param1Lower=\"session\") do={\r\ 495 | \n\t:if (\$param2Lower=\"count\") do={\r\ 496 | \n\t\t:local output\r\ 497 | \n\t\t:local hotspot [:len [/ip hotspot active find]]\r\ 498 | \n\t\t:local text \"Router ID:* \$[/system identity get name] * %0A\\\r\ 499 | \n\t\tHotspot users: _\$hotspot online_\"\r\ 500 | \n\t\t\$send chat=\$chatid text=\$text mode=\"Markdown\"\r\ 501 | \n\t}\r\ 502 | \n\t:if (\$param2Lower=\"showall\") do={\r\ 503 | \n\t\t:local output\r\ 504 | \n\t\t:foreach activeIndex in=[/ip hotspot active find] do={\r\ 505 | \n\t\t\t:local activeUser (\"*Username*: \".[/ip hotspot active get value-\ 506 | name=\"user\" \$activeIndex].\"%0A\")\r\ 507 | \n\t\t\t:local activeAddress (\"*IP*: \".[/ip hotspot active get value-nam\ 508 | e=\"address\" \$activeIndex].\"%0A\")\r\ 509 | \n\t\t\t:local activeMACAddr (\"*MAC*: \".[/ip hotspot active get value-na\ 510 | me=\"mac-address\" \$activeIndex].\"%0A\")\r\ 511 | \n\t\t\t:local activeLoginBy (\"*Login Method*: \".[/ip hotspot active get\ 512 | \_value-name=\"login-by\" \$activeIndex].\"%0A\")\r\ 513 | \n\t\t\t:local activeUptime (\"*Uptime*: \".[/ip hotspot active get value-\ 514 | name=\"uptime\" \$activeIndex].\"%0A\")\r\ 515 | \n\t\t\t:local idleTime (\"*Idle Time*: \".[/ip hotspot active get value-n\ 516 | ame=\"idle-time\" \$activeIndex].\"%0A\")\r\ 517 | \n\t\t\t:local serverIn (\"*Server*: \".[/ip hotspot active get value-name\ 518 | =\"server\" \$activeIndex].\"%0A\")\r\ 519 | \n\t\t\t:set output (\$output.\$activeUser.\$activeAddress.\$activeMACAddr\ 520 | .\$activeUptime.\$idleTime.\$activeLoginBy.\$serverIn.\"%0A\")\r\ 521 | \n\t\t}\r\ 522 | \n\t\t\$send chat=\$chatid text=(\"\$output\") mode=\"Markdown\"\r\ 523 | \n\t}\r\ 524 | \n\t:if (\$param2Lower=\"deauth-by-user\") do={\r\ 525 | \n\t\t/ip hotspot active remove [find user=\"\$param3\"]\r\ 526 | \n\t\t\$send chat=\$chatid text=(\"Sesi User \$param3 berhasil dihapus\") \ 527 | mode=\"Markdown\"\r\ 528 | \n\t}\r\ 529 | \n\t:if (\$param2Lower=\"deauth-by-mac\") do={\r\ 530 | \n\t\t/ip hotspot active remove [find mac-address=\"\$param3\"]\r\ 531 | \n\t\t\$send chat=\$chatid text=(\"Sesi MAC \$param3 berhasil dihapus\") m\ 532 | ode=\"Markdown\"\r\ 533 | \n\t}\r\ 534 | \n\t:if (\$param2Lower=\"deauth-by-ip\") do={\r\ 535 | \n\t\t/ip hotspot active remove [find address=\"\$param3\"]\r\ 536 | \n\t\t\$send chat=\$chatid text=(\"Sesi IP \$param3 berhasil dihapus\") mo\ 537 | de=\"Markdown\"\r\ 538 | \n\t}\r\ 539 | \n}\r\ 540 | \n:if (\$param1Lower=\"add\") do={\r\ 541 | \n\tif ([:len [ip hotspot user find where name=\$param2]] = 0) do={\r\ 542 | \n\t\t/ip hotspot user add name=\$param2 password=\$param3 profile=default\ 543 | \r\ 544 | \n\t\t\$send chat=\$chatid text=(\"Berhasil membuat user baru. Masuk ke ho\ 545 | tspot dengan:%0A%0A*Username:* \$param2%0A*Password:* \$param3\") mode=\"M\ 546 | arkdown\"\r\ 547 | \n\t} else={\r\ 548 | \n\t\t\$send chat=\$chatid text=(\"User \$param2 sudah terpakai, silahkan \ 549 | gunakan nama lain\") mode=\"Markdown\"\r\ 550 | \n\t}\r\ 551 | \n\t\r\ 552 | \n}\r\ 553 | \n:if (\$param1Lower=\"delete\") do={\r\ 554 | \n\tif ([:len [ip hotspot user find where name=\$param2]] > 0) do={\r\ 555 | \n\t\t/ip hotspot user remove [find name=\$param2]\r\ 556 | \n\t\t\$send chat=\$chatid text=(\"Berhasil menghapus user \$param2.\") mo\ 557 | de=\"Markdown\"\r\ 558 | \n\t} else={\r\ 559 | \n\t\t\$send chat=\$chatid text=(\"User \$param2 tidak ditemukan\") mode=\ 560 | \"Markdown\"\r\ 561 | \n\t}\r\ 562 | \n}\r\ 563 | \n:if (\$param1Lower=\"disable\") do={\r\ 564 | \n\tif ([:len [ip hotspot user find where name=\$param2]] > 0) do={\r\ 565 | \n\t\t/ip hotspot user disable [find name=\$param2]\r\ 566 | \n\t\t\$send chat=\$chatid text=(\"\$param2 kini telah dinonaktifkan\") mo\ 567 | de=\"Markdown\"\r\ 568 | \n\t} else={\r\ 569 | \n\t\t\$send chat=\$chatid text=(\"User \$param2 tidak ditemukan\") mode=\ 570 | \"Markdown\"\r\ 571 | \n\t}\r\ 572 | \n}\r\ 573 | \n:if (\$param1Lower=\"enable\") do={\r\ 574 | \n\tif ([:len [ip hotspot user find where name=\$param2]] > 0) do={\r\ 575 | \n\t/ip hotspot user enable [find name=\$param2]\r\ 576 | \n\t\$send chat=\$chatid text=(\"\$param2 kini telah diaktifkan\") mode=\"\ 577 | Markdown\"\r\ 578 | \n\t} else={\r\ 579 | \n\t\t\$send chat=\$chatid text=(\"User \$param2 tidak ditemukan\") mode=\ 580 | \"Markdown\"\r\ 581 | \n\t}\r\ 582 | \n}\r\ 583 | \n:if (\$param1Lower=\"setprofile\") do={\r\ 584 | \n\tif ([:len [ip hotspot user find where name=\$param2]] > 0) do={\r\ 585 | \n\t\t/ip hotspot user set password=\$param3 [find name=\$param2]\r\ 586 | \n\t\t/ip hotspot active remove [find name=\$param2]\r\ 587 | \n\t\t\$send chat=\$chatid text=(\"Berhasil mengganti profile menjadi \$pa\ 588 | ram3.\") mode=\"Markdown\"\r\ 589 | \n\t} else={\r\ 590 | \n\t\t\$send chat=\$chatid text=(\"User \$param2 tidak ditemukan\") mode=\ 591 | \"Markdown\"\r\ 592 | \n\t}\r\ 593 | \n}\r\ 594 | \n:if (\$param1Lower=\"change-password\") do={\r\ 595 | \n\tif ([:len [ip hotspot user find where name=\$param2]] > 0) do={\r\ 596 | \n\t\t/ip hotspot user set password=\$param3 [find name=\$param2]\r\ 597 | \n\t\t/ip hotspot active remove [find name=\$param2]\r\ 598 | \n\t\t\$send chat=\$chatid text=(\"Berhasil mengganti password untuk \$par\ 599 | am2.\") mode=\"Markdown\"\r\ 600 | \n\t} else={\r\ 601 | \n\t\t\$send chat=\$chatid text=(\"User \$param2 tidak ditemukan\") mode=\ 602 | \"Markdown\"\r\ 603 | \n\t}\r\ 604 | \n}\r\ 605 | \n" 606 | add name=tg_cmd_interface policy=read source=":local send [:parse [/system script get tg_sendMessage source]]\r\ 607 | \n:local param1 [:pick \$params 0 [:find \$params \" \"]]\r\ 608 | \n:local param2 [:pick \$params ([:find \$params \" \"]+1) [:len \$params]\ 609 | ]\r\ 610 | \n:local param3 [:pick [:pick \$params ([:find \$params \" \"]+1) [:len \$\ 611 | params]] ([:find [:pick \$params ([:find \$params \" \"]+1) [:len \$params\ 612 | ]] \" \"]+1) [:len [:pick \$params ([:find \$params \" \"]+1) [:len \$para\ 613 | ms]]]]\r\ 614 | \n:if ([:len [:find \$param2 \" \"]]>0) do={\r\ 615 | \n\t:set param2 [:pick [:pick \$params ([:find \$params \" \"]+1) [:len \$\ 616 | params]] 0 [:find [:pick \$params ([:find \$params \" \"]+1) [:len \$param\ 617 | s]] \" \"]]\r\ 618 | \n} else={\r\ 619 | \n\t:set param3 \"\"\r\ 620 | \n}\r\ 621 | \n\r\ 622 | \n:put \$params\r\ 623 | \n:put \$param1\r\ 624 | \n:put \$param2\r\ 625 | \n:put \$param3\r\ 626 | \n:put \$chatid\r\ 627 | \n:put \$from\r\ 628 | \n\r\ 629 | \n:if (\$params=\"show\") do={\r\ 630 | \n\t:local output \"Router ID:* \$[/system identity get name] * %0A%0A\"\r\ 631 | \n\t:local eth01status\r\ 632 | \n\t:local eth03status\r\ 633 | \n\t:local eth04status\r\ 634 | \n\t:local eth05status\r\ 635 | \n\r\ 636 | \n\t:if ([/interface ethernet get eth01-router running]=true) do={\r\ 637 | \n\t\t:set eth01status (\"Internet is *CONNECTED*%0A\")\r\ 638 | \n\t} else={\r\ 639 | \n\t\t:set eth01status (\"Internet is *DISCONNECTED*%0A\")\r\ 640 | \n\t}\r\ 641 | \n\r\ 642 | \n\t:if ([/interface ethernet get eth03-lantai-1 running]=true) do={\r\ 643 | \n\t\t:set eth03status (\"Lantai 1 is *CONNECTED*%0A\")\r\ 644 | \n\t} else={\r\ 645 | \n\t\t:set eth03status (\"Lantai 1 is *DISCONNECTED*%0A\")\r\ 646 | \n\t}\r\ 647 | \n\r\ 648 | \n\t:if ([/interface ethernet get eth04-lantai-2 running]=true) do={\r\ 649 | \n\t\t:set eth04status (\"Lantai 2 is *CONNECTED*%0A\")\r\ 650 | \n\t} else={\r\ 651 | \n\t\t:set eth04status (\"Lantai 2 is *DISCONNECTED*%0A\")\r\ 652 | \n\t}\r\ 653 | \n\r\ 654 | \n\t:if ([/interface ethernet get eth05-configurator running]=true) do={\r\ 655 | \n\t\t:set eth05status (\"Config is *CONNECTED*%0A\")\r\ 656 | \n\t} else={\r\ 657 | \n\t\t:set eth05status (\"Config is *DISCONNECTED*%0A\")\r\ 658 | \n\t}\r\ 659 | \n\t:set output (\$output.\$eth01status.\$eth03status.\$eth04status.\$eth0\ 660 | 5status)\r\ 661 | \n\t\$send chat=\$chatid text=(\"\$output\") mode=\"Markdown\"\r\ 662 | \n}\r\ 663 | \n:if ((\$param1=\"show\") and (\$param2=\"all\")) do={\r\ 664 | \n\tlocal status \r\ 665 | \n\tlocal name\r\ 666 | \n\tforeach i in=[interface find] do={\r\ 667 | \n\t\tset status (\$status,[/interface get value-name=running \$i])\r\ 668 | \n\t\tset name (\$name,[/interface get value-name=name \$i])\r\ 669 | \n\t}\r\ 670 | \n\tput \$status\r\ 671 | \n\tput \$name\r\ 672 | \n\tlocal text\r\ 673 | \n\tfor e from=0 to=([:len [interface find]] - 1) do={\r\ 674 | \n\t\tlocal change {\"true\"=\"Connected\";\"false\"=\"Disconnected\"}\r\ 675 | \n\t\tlocal newstatus (\$change->[:pick \$status \$e])\r\ 676 | \n\t\tlocal before (\"%0AInterface \".[:pick \$name \$e].\" - Status: \".\ 677 | \$newstatus)\r\ 678 | \n\t\tput \$before\r\ 679 | \n\t\tset text (\$text.\$before)\r\ 680 | \n\t}\r\ 681 | \n\tput \$text\r\ 682 | \n\t\$send chat=\$chatid text=(\"\$text\") mode=\"Markdown\"\r\ 683 | \n}\r\ 684 | \n" 685 | add name=tg_cmd_help policy=read source=":local send [:parse [/sys\ 686 | tem script get tg_sendMessage source]]\r\ 687 | \n\r\ 688 | \n:put \$params\r\ 689 | \n:put \$chatid\r\ 690 | \n:put \$from\r\ 691 | \n\r\ 692 | \n:local text \"Router ID:* \$[/system identity get name] * %0A\\\r\ 693 | \n==================%0A\\\r\ 694 | \nMENU TERSEDIA%0A\\\r\ 695 | \n==================%0A\\\r\ 696 | \n/help%0A\\\r\ 697 | \n/start%0A\\\r\ 698 | \n/cpu%0A\\\r\ 699 | \n/interface%0A\\\r\ 700 | \n - show%0A\\\r\ 701 | \n - show all%0A\\\r\ 702 | \n/dhcp%0A\\\r\ 703 | \n - lease%0A\\\r\ 704 | \n/hotspot%0A\\\r\ 705 | \n - help%0A\\\r\ 706 | \n - session%0A\\\r\ 707 | \n > count%0A\\\r\ 708 | \n > showall%0A\\\r\ 709 | \n > deauth-by-user %0A\\\r\ 710 | \n > deauth-by-mac %0A\\\r\ 711 | \n > deauth-by-ip %0A\\\r\ 712 | \n - add %0A\\\r\ 713 | \n - delete %0A\\\r\ 714 | \n - disable %0A\\\r\ 715 | \n - enable %0A\\\r\ 716 | \n - setprofile %0A\\\r\ 717 | \n - change-password %0A\\\r\ 718 | \n/ping to %0A\\\r\ 719 | \n/monitoring%0A\\\r\ 720 | \n - interface %0A\\\r\ 721 | \n - cpu%0A\\\r\ 722 | \n - ram%0A\\\r\ 723 | \n - memory%0A\\\r\ 724 | \n/public%0A\\\r\ 725 | \n/enablehotspot%0A\\\r\ 726 | \n/disablehotspot%0A\\\r\ 727 | \n/forceupdateddns%0A\\\r\ 728 | \n/reboot\"\r\ 729 | \n \r\ 730 | \n\$send chat=\$chatid text=\$text mode=\"Markdown\"\r\ 731 | \n:return true" 732 | add name=reboot-report policy=\ 733 | ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source=":\ 734 | local send [:parse [/system script get tg_sendMessage source]]\r\ 735 | \n:put \$params\r\ 736 | \n:put \$chatid\r\ 737 | \n:put \$from\r\ 738 | \n\r\ 739 | \n:local reportBody \"\"\r\ 740 | \n \r\ 741 | \n:local deviceName [/system identity get name]\r\ 742 | \n:local deviceDate [/system clock get date]\r\ 743 | \n:local deviceTime [/system clock get time]\r\ 744 | \n:local hwModel [/system routerboard get model]\r\ 745 | \n:local rosVersion [/system package get system version]\r\ 746 | \n:local currentFirmware [/system routerboard get current-firmware]\r\ 747 | \n:local upgradeFirmware [/system routerboard get upgrade-firmware]\r\ 748 | \n \r\ 749 | \n \r\ 750 | \n:set reportBody (\$reportBody . \"Router Reboot Report for \$deviceName%\ 751 | 0A\")\r\ 752 | \n:set reportBody (\$reportBody . \"Report generated on \$deviceDate at \$\ 753 | deviceTime%0A%0A\")\r\ 754 | \n \r\ 755 | \n:set reportBody (\$reportBody . \"Hardware Model: \$hwModel%0A\")\r\ 756 | \n:set reportBody (\$reportBody . \"RouterOS Version: \$rosVersion%0A\")\r\ 757 | \n:set reportBody (\$reportBody . \"Current Firmware: \$currentFirmware%0A\ 758 | \")\r\ 759 | \n:set reportBody (\$reportBody . \"Upgrade Firmware: \$upgradeFirmware\")\ 760 | \r\ 761 | \nif ( \$currentFirmware < \$upgradeFirmware) do={\r\ 762 | \n:set reportBody (\$reportBody . \"NOTE: You should upgrade the RouterBOA\ 763 | RD firmware!%0A\")\r\ 764 | \n}\r\ 765 | \n \r\ 766 | \n:set reportBody (\$reportBody . \"%0A%0A=== Critical Log Events ===%0A\"\ 767 | \_)\r\ 768 | \n \r\ 769 | \n:local x\r\ 770 | \n:local ts\r\ 771 | \n:local msg\r\ 772 | \nforeach i in=([/log find where topics~\"critical\"]) do={\r\ 773 | \n:set \$ts [/log get \$i time]\r\ 774 | \n:set \$msg [/log get \$i message]\r\ 775 | \n:set \$reportBody (\$reportBody . \$ts . \" \" . \$msg . \"%0A\" )\r\ 776 | \n}\r\ 777 | \n \r\ 778 | \n:set reportBody (\$reportBody . \"%0A=== end of report ===%0A\")\r\ 779 | \n\$send chat=\$chatid text=\$reportBody mode=\"Markdown\"" 780 | add name=tg_cmd_reboot policy=\ 781 | ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source=":\ 782 | local send [:parse [/system script get tg_sendMessage source]]\r\ 783 | \n:put \$params\r\ 784 | \n:put \$chatid\r\ 785 | \n:put \$from\r\ 786 | \n\r\ 787 | \n:local text \"Menghidupkan ulang router dalam 30 detik...\"\r\ 788 | \n\r\ 789 | \n\$send chat=\$chatid text=\$text mode=\"Markdown\"\r\ 790 | \n\r\ 791 | \n:delay 30\r\ 792 | \nsystem reboot" 793 | add name=tg_cmd_dhcp policy=\ 794 | ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source=":\ 795 | local send [:parse [/system script get tg_sendMessage source]]\r\ 796 | \n:local tolower [:parse [/system script get func_lowercase source]]\r\ 797 | \n:local param1 [:pick \$params 0 [:find \$params \" \"]]\r\ 798 | \n:local param2 [:pick \$params ([:find \$params \" \"]+1) [:len \$params]\ 799 | ]\r\ 800 | \n:local param3 [:pick [:pick \$params ([:find \$params \" \"]+1) [:len \$\ 801 | params]] ([:find [:pick \$params ([:find \$params \" \"]+1) [:len \$params\ 802 | ]] \" \"]+1) [:len [:pick \$params ([:find \$params \" \"]+1) [:len \$para\ 803 | ms]]]]\r\ 804 | \n:if ([:len [:find \$param2 \" \"]]>0) do={\r\ 805 | \n\t:set param2 [:pick [:pick \$params ([:find \$params \" \"]+1) [:len \$\ 806 | params]] 0 [:find [:pick \$params ([:find \$params \" \"]+1) [:len \$param\ 807 | s]] \" \"]]\r\ 808 | \n} else={\r\ 809 | \n\t:set param3 \"\"\r\ 810 | \n}\r\ 811 | \n\r\ 812 | \n:local paramsLower [\$tolower \$params]\r\ 813 | \n:local param1Lower [\$tolower \$param1]\r\ 814 | \n:local param2Lower [\$tolower \$param2]\r\ 815 | \n:local param3Lower [\$tolower \$param3]\r\ 816 | \n\r\ 817 | \n:local getVendor false\r\ 818 | \n:if (\$paramsLower = \"lease\") do={\r\ 819 | \n\tlocal GetMacVendor do={\r\ 820 | \n\t\t:do {\r\ 821 | \n\t\t\treturn ([/tool fetch mode=https http-method=get url=(\"https://api\ 822 | .macvendors.com/\".[:pick [:tostr \$1] 0 8 ]) as-value output=user ]->\"da\ 823 | ta\")\r\ 824 | \n\t\t} on-error={\r\ 825 | \n\t\t\treturn \"unknown vendor\"\r\ 826 | \n\t\t}\r\ 827 | \n\t}\r\ 828 | \n\t:local text\r\ 829 | \n\t:local number (0)\r\ 830 | \n\t:set text (\"Router ID: \$[/system identity get value-name=name]\")\r\ 831 | \n\t:foreach lease in=[/ip dhcp-server lease find] do={\r\ 832 | \n\t\t:set number (\$number + 1)\r\ 833 | \n\t\tset text (\$text.\"====== \\F0\\9F\\97\\82 DHCP LEASE (\$number) \\F\ 834 | 0\\9F\\97\\82 ======%0A\")\r\ 835 | \n\t\t:local addr [/ip dhcp-server lease get value-name=address \$lease]\r\ 836 | \n\t\t:set text (\$text.\"IP Address: \$addr%0A\")\r\ 837 | \n\t\t:local mac [/ip dhcp-server lease get value-name=mac-address \$lease\ 838 | ]\r\ 839 | \n\t\t:set text (\$text.\"Mac Address: \$mac%0A\")\r\ 840 | \n\t\t:if (\$getVendor = \"true\") do={\r\ 841 | \n\t\t\t:local macvendor [\$GetMacVendor \$mac]\r\ 842 | \n\t\t\t:set text (\$text.\"Mac Vendor: \$macvendor%0A\")\t\r\ 843 | \n\t\t}\r\ 844 | \n\t\t:local host [/ip dhcp-server lease get value-name=host-name \$lease]\ 845 | \r\ 846 | \n\t\t:if ([:typeof \$host] = \"nil\") do={set host (\"Unknown Hostname\")\ 847 | }\r\ 848 | \n\t\t:set text (\$text.\"Hostname: \$\$host%0A\")\r\ 849 | \n\t\t:local lastseen [/ip dhcp-server lease get value-name=last-seen \$le\ 850 | ase]\r\ 851 | \n\t\t:set text (\$text.\"Uptime: \$lastseen%0A\")\r\ 852 | \n\t\t:local server [/ip dhcp-server lease get value-name=server \$lease]\ 853 | \r\ 854 | \n\t\t:set text (\$text.\"Server: \$server%0A\")\r\ 855 | \n\t}\r\ 856 | \n\t:set text (\$text.\"=========== END REPORT ===========%0A\")\r\ 857 | \n\t\$send chat=\$chatid text=(\"\$text\") mode=\"Markdown\"\r\ 858 | \n}\r\ 859 | \n:if (\$param1Lower = \"client\" && \$param2Lower = \"release\") do={\r\ 860 | \n\t:if ([:len \$param3] > 0) do={\r\ 861 | \n\t\t:if ([:len [/ip dhcp-client/ find where interface=\$param3]] > 0) do\ 862 | ={\r\ 863 | \n\t\t\t\$send chat=\$chatid text=\"DHCP Client akan di release dalam 5 de\ 864 | tik\" mode=\"Markdown\";\r\ 865 | \n\t\t\t:delay 5s\r\ 866 | \n\t\t\t/ip dhcp-client release \$param3\r\ 867 | \n\t\t} else={\r\ 868 | \n\t\t\t\$send chat=\$chatid text=(\"Maaf, DHCP client dengan interface \$\ 869 | param3 tidak ditemukan\") mode=\"Markdown\";\r\ 870 | \n\t\t}\r\ 871 | \n\t} else={\r\ 872 | \n\t\t\$send chat=\$chatid text=\"Silahkan masukkan interface yang ingin d\ 873 | i release dhcpclient-nya\" mode=\"Markdown\";\r\ 874 | \n\t}\r\ 875 | \n}" 876 | add name=func_lowercase policy=read \ 877 | source="local alphabet {\"A\"=\"a\";\"B\"=\"b\";\"C\"=\"c\";\"D\"=\"d\";\"\ 878 | E\"=\"e\";\"F\"=\"f\";\"G\"=\"g\";\"H\"=\"h\";\"I\"=\"i\";\"J\"=\"j\";\"K\ 879 | \"=\"k\";\"L\"=\"l\";\"M\"=\"m\";\"N\"=\"n\";\"O\"=\"o\";\"P\"=\"p\";\"Q\"\ 880 | =\"q\";\"R\"=\"r\";\"S\"=\"s\";\"T\"=\"t\";\"U\"=\"u\";\"V\"=\"v\";\"X\"=\ 881 | \"x\";\"Z\"=\"z\";\"Y\"=\"y\";\"W\"=\"w\"};\r\ 882 | \n:local result\r\ 883 | \n:local character\r\ 884 | \n:for strings from=0 to=([:len \$1] - 1) do={\r\ 885 | \n\t:local single [:pick \$1 \$strings]\r\ 886 | \n\t:set character (\$alphabet->\$single)\r\ 887 | \n\t:if ([:typeof \$character] = \"str\") do={set single \$character}\r\ 888 | \n\t:set result (\$result.\$single)\r\ 889 | \n}\r\ 890 | \n:return \$result" 891 | add name=tg_cmd_monitoring policy=read\ 892 | source=":local send [:parse [/system script get tg_sendMessage source\ 893 | ]]\r\ 894 | \n:local tolower [:parse [/system script get func_lowercase source]]\r\ 895 | \n:local param1 [:pick \$params 0 [:find \$params \" \"]]\r\ 896 | \n:local param2 [:pick \$params ([:find \$params \" \"]+1) [:len \$params]\ 897 | ]\r\ 898 | \n:local param3 [:pick [:pick \$params ([:find \$params \" \"]+1) [:len \$\ 899 | params]] ([:find [:pick \$params ([:find \$params \" \"]+1) [:len \$params\ 900 | ]] \" \"]+1) [:len [:pick \$params ([:find \$params \" \"]+1) [:len \$para\ 901 | ms]]]]\r\ 902 | \n:if ([:len [:find \$param2 \" \"]]>0) do={\r\ 903 | \n\t:set param2 [:pick [:pick \$params ([:find \$params \" \"]+1) [:len \$\ 904 | params]] 0 [:find [:pick \$params ([:find \$params \" \"]+1) [:len \$param\ 905 | s]] \" \"]]\r\ 906 | \n} else={\r\ 907 | \n\t:set param3 \"\"\r\ 908 | \n}\r\ 909 | \n\r\ 910 | \n:local fconfig [:parse [/system script get tg_config source]]\r\ 911 | \n:local config [\$fconfig]\r\ 912 | \n:local botID (\$config->\"botAPI\")\r\ 913 | \n\r\ 914 | \n:local counting 0;\r\ 915 | \n\r\ 916 | \n#=======================================================\r\ 917 | \n#monitoring akan dipaksa berhenti setelah x detik (default 50)\r\ 918 | \n:local forceStopAfter 50;\r\ 919 | \n#=======================================================\r\ 920 | \n\r\ 921 | \n:local paramsLower [\$tolower \$params]\r\ 922 | \n:local param1Lower [\$tolower \$param1]\r\ 923 | \n:local param2Lower [\$tolower \$param2]\r\ 924 | \n:local param3Lower [\$tolower \$param3]\r\ 925 | \n\r\ 926 | \n:local forceStop do={\r\ 927 | \n :global monitoring;\r\ 928 | \n :set monitoring false;\r\ 929 | \n :delay 2s;\r\ 930 | \n /system script environment remove monitoring;\r\ 931 | \n /system script job remove [find where script=tg_getUpdates];\r\ 932 | \n}\r\ 933 | \n\r\ 934 | \n:local prepare do={\r\ 935 | \n :local gkey [:parse [/system script get tg_getkey source]]\r\ 936 | \n :local fconfig [:parse [/system script get tg_config source]]\r\ 937 | \n :local config [\$fconfig]\r\ 938 | \n :local botID (\$config->\"botAPI\")\r\ 939 | \n :local url (\"https://api.telegram.org/bot\".\$botID.\"/sendMessage\ 940 | \\\?chat_id=\$chatid&text=Preparing...\")\r\ 941 | \n :put \$url\r\ 942 | \n :local fetching ([/tool fetch url=\$url output=user as-value]->\"dat\ 943 | a\")\r\ 944 | \n :local msgID [\$gkey key=\"message_id\" text=\$fetching]\r\ 945 | \n :return \$msgID\r\ 946 | \n}\r\ 947 | \n\r\ 948 | \n:global monitoring true;\r\ 949 | \n\r\ 950 | \n:if (\$param1Lower = \"interface\") do={\r\ 951 | \n :if ([:len [/interface find where name=\$param2]] > 0) do={\r\ 952 | \n :local msgid [\$prepare chatid=\$chatid]\r\ 953 | \n :put \$msgid\r\ 954 | \n :while (\$monitoring) do={\r\ 955 | \n :local rxData ((([/interface monitor-traffic \$param2 once a\ 956 | s-value]->\"rx-bits-per-second\")/1000)/1);\r\ 957 | \n :local txData ((([/interface monitor-traffic \$param2 once a\ 958 | s-value]->\"tx-bits-per-second\")/1000)/1);\r\ 959 | \n :local text (\"\$param2%0ARX: \$rxData kb/s%0ATX: \$txData k\ 960 | b/s\");\r\ 961 | \n /tool fetch url=\"https://api.telegram.org/bot\$botID/editMe\ 962 | ssageText\\\?chat_id=\$chatid&message_id=\$msgid&text=\$text\" output=none\ 963 | \r\ 964 | \n :set counting (\$counting + 1);\r\ 965 | \n :if (\$counting > \$forceStopAfter) do={\$forceStop;}\r\ 966 | \n }\r\ 967 | \n :return true;\r\ 968 | \n \r\ 969 | \n } else={\r\ 970 | \n \$send chat=\$chatid text=(\"Interface \$param2 tidak ditemukan\ 971 | \")\r\ 972 | \n }\r\ 973 | \n}\r\ 974 | \n\r\ 975 | \n:if (\$paramsLower = \"cpu\") do={\r\ 976 | \n :local msgid [\$prepare chatid=\$chatid]\r\ 977 | \n :put \$msgid\r\ 978 | \n :local identity ([/system identity get name])\r\ 979 | \n :while (\$monitoring) do={\r\ 980 | \n :local cpuLoad [/system resource get cpu-load]\r\ 981 | \n :local text (\"Router Id: \".\$identity.\"%0ACPU-LOAD: \".\$cpuL\ 982 | oad.\"%25\")\r\ 983 | \n /tool fetch url=\"https://api.telegram.org/bot\$botID/editMessag\ 984 | eText\\\?chat_id=\$chatid&message_id=\$msgid&text=\$text\" output=none\r\ 985 | \n :set counting (\$counting + 1);\r\ 986 | \n :if (\$counting > \$forceStopAfter) do={\$forceStop;}\r\ 987 | \n }\r\ 988 | \n}\r\ 989 | \n\r\ 990 | \n:if ((\$paramsLower = \"ram\") or (\$paramsLower = \"memory\")) do={\r\ 991 | \n :local msgid [\$prepare chatid=\$chatid]\r\ 992 | \n :put \$msgid\r\ 993 | \n :local totalMemory ([/system resource get total-memory] / 1024 / 10\ 994 | 24)\r\ 995 | \n :local identity ([/system identity get name])\r\ 996 | \n :while (\$monitoring) do={\r\ 997 | \n :local clockTime [/system clock get time]\r\ 998 | \n :local freeMemory ([/system resource get free-memory] / 1024 / \ 999 | 1024)\r\ 1000 | \n :local text (\"Router Id: \".\$identity.\"%0ATime: \$clockTime%0\ 1001 | AFree Memory: \$freeMemory/\$totalMemory MiB\")\r\ 1002 | \n /tool fetch url=\"https://api.telegram.org/bot\$botID/editMessag\ 1003 | eText\\\?chat_id=\$chatid&message_id=\$msgid&text=\$text\" output=none\r\ 1004 | \n :set counting (\$counting + 1);\r\ 1005 | \n :if (\$counting > \$forceStopAfter) do={\$forceStop;}\r\ 1006 | \n }\r\ 1007 | \n}\r\ 1008 | \n" 1009 | add name=tg_cmd_stop policy=read source=":global monitoring;\r\ 1010 | \n:set monitoring false;\r\ 1011 | \n:delay 5s;\r\ 1012 | \n/system script environment remove monitoring;\r\ 1013 | \n" 1014 | --------------------------------------------------------------------------------