Aktivasyon adistans nan scripts Mikrotik soti nan Telegram

Alexander Koryukin pouse m 'nan aplikasyon sa a GeXoGeN ak piblikasyon liRemote vire sou òdinatè a pou gratis, san SMS ak san nwaj, lè l sèvi avèk Mikrotik".

Ak yon kòmantè nan youn nan gwoup yo VK pa Kirill Kazakov:

Wi, li pa an sekirite ditou. Mwen ta pito ekri yon bot telegram ki sèlman aksepte kòmandman aktivasyon nan kont mwen an.

Mwen deside ekri yon bot konsa.

Se konsa, premye bagay yo fè se kreye yon bot nan telegram.

  • Nou jwenn nan rechèch la yon kont ki rele @botfather
  • Klike sou bouton Kòmanse ki anba ekran an
  • Lè sa a, nou ekri l 'kòmand la / newbot

Apre sa, nou reponn 2 kesyon senp:

  • Premye kesyon an se non bot pou kreye. MyMikrotikROuter
  • Dezyèm kesyon an se tinon bot yo te kreye a (ta dwe fini ak bot) MikrotikROuter_bot

Kòm repons, nou pral resevwa siy bot nou an, nan ka mwen an li se:

Sèvi ak siy sa a pou jwenn aksè nan API HTTP: 265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4

Aktivasyon adistans nan scripts Mikrotik soti nan Telegram
Lè sa a, ou bezwen jwenn bot nou an nan rechèch la pa non @MikrotikROuter_bot epi peze bouton an Kòmanse.

Apre sa, ou bezwen louvri navigatè a epi antre nan liy sa a:

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

Kote XXXXXXXXXXXXXXXXXX se siy bot ou a.

Yon paj ki sanble ak sa ki annapre yo ap louvri:

Aktivasyon adistans nan scripts Mikrotik soti nan Telegram

Nou jwenn tèks sa a sou li:

"chat":{"id":631290,

Se konsa, nou gen tout enfòmasyon ki nesesè pou ekri scripts pou Mikrotik, sètadi:

Jeton bot: 265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4

ID chat kote li ta dwe ekri: 631290

Pou tcheke, nou ka ale nan navigatè a:

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

Ta dwe jwenn rezilta a:

Aktivasyon adistans nan scripts Mikrotik soti nan Telegram

Pou konvenyans nou an, nou pral imedyatman ajoute kòmandman pou bot la:

Jwenn yon kont ak yon non @botfather
Lè sa a, nou ekri l 'yon lòd / setcommands

  • Li pral mande ki bot

Nou ekri:
@MikrotikROuter_bot

Ajoute kòmandman:

  • helloworld< — Tès mesaj sou chat 1
  • itsworking-Test Mesaj sou chat 2
  • wolmypc-reveye PC mwen an

Koulye a, si ou tape "/" nan chat la, ou ta dwe jwenn:

Aktivasyon adistans nan scripts Mikrotik soti nan Telegram

Koulye a, ann ale nan MikroTik.

RouterOS gen yon sèvis piblik konsole pou kopye dosye atravè ftp oswa http / https, sèvis piblik la rele chache, ki se sa nou pral itilize.

Louvri tèminal epi antre:

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

Tanpri sonje ke MikroTik bezwen "» pou chape siy «?' nan URL la.

Ta dwe jwenn rezilta a:

Aktivasyon adistans nan scripts Mikrotik soti nan Telegram

Koulye a, ann ale nan scripts:

bonjou mond

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}

travay li

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"

Pa bliye presize non mac ak koòdone ki kòrèk la, ansanm ak bot-token ak chat_id.

Koulye a, mwen pral eksplike yon ti kras sa yo fè:

Script "helloworld" la voye yon mesaj: "Bonjou, mond!" nan chat nou an ak bot la.
Script "itsworking" la voye yon mesaj: "Tès OK, li ap travay!" nan chat nou an ak bot la.
Scripts sa yo se pou rezon demonstrasyon.
Mwen te ajoute script "wolmypc" kòm youn nan aplikasyon posib yo.
Lè yo fin egzekite script la, bot la pral ekri "wol OK" nan chat la.
An reyalite, ou ka kouri absoliman nenpòt script.

Kreye yon travay:

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

View lizibli pa klè poukisa, men soti nan script nan travay li pa divilge done mondyal, te ajoute script la lè sistèm nan bòt.
Telegram demaraj

: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; 
} 

Kijan travay sa a

Telechaje mesaj "getUpdates" nou yo chak 30 segonn, apresa analize yo pou chèche konnen update_id (nimewo mesaj) ak tèks (ekip nou yo) ak chat_id . Pa default, getUpdates montre soti nan 1 a 100 mesaj, pou konvenyans, apre li fin li kòmandman an, nou efase mesaj la. Telegram API di pou li yon mesaj ou bezwen nimewo mesaj la + 1

/getUpdates?offset=update_id + 1

Tout teste sou Mikrotik rb915 RouterOS 6.37.1
Si ou voye anpil kòmandman an menm tan, yo tout pral egzekite nan vire ak yon entèval 30 segonn.

PS Anpil mèsi a Kirill Kazakov pou lide a ak zanmi mwen Alexander pou èd ak scripts yo.

Referans

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

mete ajou:

03:11:16

Scripts amelyore:

Te ajoute chèk pou chat_id
Tcheke pou yon moun sòt, si yon moun ekri bot nou an, li pral reponn li: "Mwen pa ka pale avèk ou. ", pral menm jan an reponn nou si li pa rekonèt kòmandman an.
Apre egzekite kòmandman an, bot la dezabòne nan chat la (gade script wolmypc)

DUP

Jwenn ak 7 kaskadeur7 ke yon dosye ki gen pi wo a ~ 14 mesaj yo pa trete pa kòmandman an jwenn (Limitasyon Mikrotik). Se poutèt sa, nan tan kap vini an, mwen pral chanje script la nan lua, mèsi 7 kaskadeur7 pou sa, mwen pa t 'konnen sou lua.

MIZAJOU 08.12.2016

nan Telegram, aparamman, yo yon ti kras chanje "echapman" nan getUpdate. kounye a nan script prensipal la ou bezwen korije mesaj la konpanse soti nan 2 a 1

chanjman

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

заменить на :

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

Sous: www.habr.com