Telegram'dan Mikrotik skriptlarini masofadan faollashtirish

Aleksandr Koryukin meni ushbu amaliyotga undadi GeXoGeN nashri bilanMikrotik yordamida kompyuterni masofadan turib, SMS va bulutlarsiz bepul yoqish".

Kirill Kazakovning VK guruhlaridan biridagi sharhi:

Ha, bu umuman xavfsiz emas. Men akkauntimdan faqat faollashtirish buyruqlarini qabul qiladigan telegram bot yozishni afzal ko'raman.

Men shunday bot yozishga qaror qildim.

Demak, birinchi navbatda telegramda bot yaratish kerak.

  • Biz qidiruvda @botfather nomli hisobni topamiz
  • Ekranning pastki qismidagi Start tugmasini bosing
  • Keyin unga buyruq / newbot yozamiz

Keyin ikkita oddiy savolga javob beramiz:

  • Birinchi savol - yaratiladigan botning nomi. MyMikrotikROuter
  • Ikkinchi savol - yaratilayotgan botning taxallusi (bot bilan tugashi kerak) MikrotikROuter_bot

Bunga javoban biz botimizning tokenini olamiz, mening holimda bu:

HTTP API ga kirish uchun ushbu tokendan foydalaning: 265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4

Telegram'dan Mikrotik skriptlarini masofadan faollashtirish
Keyin, qidiruvda bizning botimizni nomi bo'yicha topishingiz kerak @MikrotikROuter_bot va Start tugmasini bosing.

Shundan so'ng siz brauzerni ochishingiz va quyidagi qatorni kiritishingiz kerak:

 https://api.telegram.org/botXXXXXXXXXXXXXXXXXX/getUpdates

Bu yerda XXXXXXXXXXXXXXXXXX bot tokenidir.

Quyidagiga o'xshash sahifa ochiladi:

Telegram'dan Mikrotik skriptlarini masofadan faollashtirish

Unda biz quyidagi matnni topamiz:

"chat":{"id":631290,

Shunday qilib, bizda Mikrotik uchun skriptlarni yozish uchun barcha kerakli ma'lumotlar mavjud, xususan:

Bot tokeni: 265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4

U yozishi kerak bo'lgan chat ID: 631290

Tekshirish uchun biz brauzer orqali o'tishimiz mumkin:

https://api.telegram.org/bot265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4/sendmessage?chat_id=631290&text=test

Natijani olish kerak:

Telegram'dan Mikrotik skriptlarini masofadan faollashtirish

Bizning qulayligimiz uchun biz darhol bot uchun buyruqlar qo'shamiz:

Ism bilan hisobni topish @botfather
Keyin unga buyruq yozamiz / setcommands

  • U qaysi botni so'raydi

Biz yozamiz:
@MikrotikROuter_bot

Buyruqlar qo'shing:

  • helloworld< — Chatdagi test xabari 1
  • itsworking-Chatdagi sinov xabari 2
  • wolmypc-kompyuterimni uyg'ot

Endi chatda "/" ni yozsangiz, quyidagilarni olishingiz kerak:

Telegram'dan Mikrotik skriptlarini masofadan faollashtirish

Endi MikroTik-ga o'tamiz.

RouterOS-da ftp yoki http / https orqali fayllarni nusxalash uchun konsol yordam dasturi mavjud, yordamchi dastur fetch deb ataladi, biz undan foydalanamiz.

Biz ochamiz Terminal va kiriting:

/tool fetch url="https://api.telegram.org/bot265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4/sendmessage?chat_id=631290&text=test " keep-result=no

E'tibor bering, MikroTik kerak "» belgisidan qochish uchun?' URL manzilida.

Natijani olish kerak:

Telegram'dan Mikrotik skriptlarini masofadan faollashtirish

Endi skriptlarga o'tamiz:

Salom Dunyo

system script add name="helloworld" policy=read source={/tool fetch url="https://api.telegram.org/bot265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4/sendmessage?chat_id=631290&text=Hello,world! " keep-result=no}

uning ishlashi

system script add name="itsworking" policy=read source={/tool fetch url="https://api.telegram.org/bot265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4/sendmessage?chat_id=631290&text=Test OK, it's Working " keep-result=no}

wolmypc

system script add name="wolmypc" policy=read source="/tool wol mac=XX:XX:XX:XX:XX:XX interface=ifnamer
    n/tool fetch url="https://api.telegram.org/boXXXXXXXXXXXXXXXXXXX?chat_id=631290&text=wol OK" keep-resul
    t=no"

To'g'ri mac va interfeys nomini, shuningdek, bot-token va chat_id-ni ko'rsatishni unutmang.

Endi men ular nima qilishlarini bir oz tushuntiraman:

"Helloworld" skripti xabar yuboradi: "Salom, dunyo!" bot bilan suhbatimizga.
"Uning ishlayotgan" skripti xabar yuboradi: "Test OK, u ishlayapti!" bot bilan suhbatimizga.
Ushbu skriptlar namoyish qilish uchun mo'ljallangan.
Men "wolmypc" skriptini mumkin bo'lgan ilovalardan biri sifatida qo'shdim.
Skript bajarilgandan so'ng, bot chatga "wol OK" deb yozadi.
Aslida, siz mutlaqo har qanday skriptni ishga tushirishingiz mumkin.

Vazifa yarating:

Telegram.src

/system scheduler
add interval=30s name=Telegram on-event=":tool fetch url=("https://api.telegr
    am.org/".$botID."/getUpdates") ;r
    n:global content [/file get [/file find name=getUpdates] contents] ;r
    n:global startLoc 0;r
    n:global endLoc 0;r
    nr
    n:if ( [/file get [/file find name=getUpdates] size] > 50 ) do={r
    nr
    n:set startLoc  [:find $content "update_id" $lastEnd ] ;r
    n:set startLoc ( $startLoc + 11 ) ;r
    n:local endLoc [:find $content "," $startLoc] ;r
    n:local messageId ([:pick $content $startLoc $endLoc] + (1));r
    n:put [$messageId] ;r
    n:#log info message="updateID $messageId" ;r
    nr
    n:set startLoc  [:find $content "text" $lastEnd ] ;r
    n:set startLoc ( $startLoc  + 7 ) ;r
    n:local endLoc [:find $content "," ($startLoc)] ;r
    n:set endLoc ( $endLoc - 1 ) ;r
    n:local message [:pick $content ($startLoc + 2) $endLoc] ;r
    n:put [$message] ;r
    n:#log info message="message $message ";r
    nr
    n:set startLoc  [:find $content "chat" $lastEnd ] ;r
    n:set startLoc ( $startLoc + 12 ) ;r
    n:local endLoc [:find $content "," $startLoc] ;r
    n:local chatId ([:pick $content $startLoc $endLoc]);r
    n:put [$chatId] ;r
    n:#log info message="chatID $chatId ";r
    nr
    n:if (($chatId = $myChatID) and (:put [/system script find name=$messa
    ge] != "")) do={r
    n:system script run $message} else={:tool fetch url=("https://api.teleg
    ram.org/".$botID."/sendmessage?chat_id=".$chatId."&text=I can't t
    alk with you. ") keep-result=no} ;r
    n:tool fetch url=("https://api.telegram.org/".$botID."/getUpdates?
    offset=$messageId") keep-result=no; r
    n} r
    n" policy=
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon 
    start-date=nov/02/2010 start-time=00:00:00
	
add name=Telegram-startup on-event=":delay 5r
    n:global botID "botXXXXXXXXXXXXXXXXXX" ;r
    n:global myChatID "631290" ;r
    n:global startLoc 0;r
    n:global endLoc 0;r
    n:tool fetch url=("https://api.telegram.org/".$botID."/getUpdates") 
    ;" policy=
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon 
    start-time=startup

O'qilishi mumkin bo'lgan ko'rinishnima uchun aniq emas, lekin ishchi skriptdan u global ma'lumotlarni oshkor qilmaydi, tizim yuklanganda skript qo'shiladi.
Telegramni ishga tushirish

:delay 5
:global botID "botXXXXXXXXXXXXXXXXXX" ;   token bot
:global myChatID "xxxxxx" ;                               chat_id
:global startLoc 0;
:global endLoc 0;
:tool fetch url=("https://api.telegram.org/".$botID."/getUpdates") ;

Telegram

:tool fetch url=("https://api.telegram.org/".$botID."/getUpdates") ;
:global content [/file get [/file find name=getUpdates] contents] ;
:global startLoc 0;
:global endLoc 0;

:if ( [/file get [/file find name=getUpdates] size] > 50 ) do={

:set startLoc  [:find $content "update_id" $lastEnd ] ;
:set startLoc ( $startLoc + 11 ) ;
:local endLoc [:find $content "," $startLoc] ;
:local messageId ([:pick $content $startLoc $endLoc] + (1));
:put [$messageId] ;
#:log info message="updateID $messageId" ;

:set startLoc  [:find $content "text" $lastEnd ] ;
:set startLoc ( $startLoc  + 7 ) ;
:local endLoc [:find $content "," ($startLoc)] ;
:set endLoc ( $endLoc - 1 ) ;
:local message [:pick $content ($startLoc + 2) $endLoc] ;
:put [$message] ;
#:log info message="message $message ";

:set startLoc  [:find $content "chat" $lastEnd ] ;
:set startLoc ( $startLoc + 12 ) ;
:local endLoc [:find $content "," $startLoc] ;
:local chatId ([:pick $content $startLoc $endLoc]);
:put [$chatId] ;
#:log info message="chatID $chatId ";

:if (($chatId = $myChatID) and (:put [/system script find name=$message] != "")) do={
:system script run $message} else={:tool fetch url=("https://api.telegram.org/".$botID."/sendmessage?chat_id=".$chatId."&text=I can't talk with you. ") keep-result=no} ;
:tool fetch url=("https://api.telegram.org/".$botID."/getUpdates?offset=$messageId") keep-result=no; 
} 

U qanday ishlaydi

Bizning "getUpdates" xabarlarimizni har 30 soniyada yuklab oling, so'ng bilish uchun tahlil qiling update_id (xabar raqami) va matn (bizning jamoalarimiz) va chat_id . Odatiy bo'lib, getUpdates 1 dan 100 gacha xabarlarni ko'rsatadi, qulaylik uchun buyruqni o'qib bo'lgach, biz xabarni o'chirib tashlaymiz. Telegram api xabarni o'qish uchun xabar raqami + 1 kerakligini aytadi

/getUpdates?offset=update_id + 1

Hammasi Mikrotik rb915 RouterOS 6.37.1 da sinovdan o'tgan
Agar siz bir vaqtning o'zida bir nechta buyruqlarni yuborsangiz, ularning barchasi 30 soniya oralig'ida navbat bilan bajariladi.

PS Kirill Kazakovga g'oya uchun va do'stim Aleksandrga skriptlar bilan yordam bergani uchun katta rahmat.

Manbalar

habrahabr.ru/post/313794
1spla.ru/index.php/blog/telegram_bot_for_mikrotik
core.telegram.org/bots/api
wiki.mikrotik.com/wiki/Manual: Skript yaratish

yangilash:

03:11:16

Yaxshilangan skriptlar:

Chat_id uchun tekshirish qo‘shildi
Ahmoqni tekshirib, agar kimdir bizning botimizga yozsa, u javob beradi: "Men siz bilan gaplasha olmayman. ", agar u buyruqni tanimasa, bizga xuddi shunday javob beradi.
Buyruqni bajargandan so'ng, bot chatga obunani bekor qiladi (wolmypc skriptiga qarang)

DUP

bilan topilgan 7Kakadyor7 ~14 ta xabardan yuqori bo'lgan fayl endi find buyrug'i bilan qayta ishlanmaydi (Mikrotik cheklovlari). Shuning uchun, kelajakda men skriptni lua ga o'zgartiraman, rahmat 7Kakadyor7 Buning uchun men Lua haqida bilmasdim.

UPD 08.12.2016/XNUMX/XNUMX

Telegram-da ular getUpdate-ning "egzozini" biroz o'zgartirganlar. endi asosiy skriptda siz xabar ofsetini 2 dan 1 gacha tuzatishingiz kerak

o'zgartirish

:local message [:pick $content ($startLoc + 2) $endLoc] ;

заменить на :

:local message [:pick $content ($startLoc + 1) $endLoc] ;

Manba: www.habr.com