ื”ืคืขืœื” ืžืจื—ื•ืง ืฉืœ ืกืงืจื™ืคื˜ื™ื ืฉืœ Mikrotik ืžื˜ืœื’ืจื

ืืœื›ืกื ื“ืจ ืงื•ืจื™ื•ืงื™ืŸ ื“ื—ืฃ ืื•ืชื™ ืœื™ื™ืฉื•ื ื”ื–ื” GeXoGeN ืขื ื”ืคืจืกื•ื ืฉืœื•ื”ืคืขืœืช ื”ืžื—ืฉื‘ ืžืจื—ื•ืง ื‘ื—ื™ื ื, ืœืœื SMS ื•ืœืœื ืขื ื ื™ื, ื‘ืืžืฆืขื•ืช Mikrotik".

ื•ื”ืขืจื” ื‘ืื—ืช ืžืงื‘ื•ืฆื•ืช VK ืžืืช ืงื™ืจื™ืœ ืงื–ืงื•ื‘:

ื›ืŸ, ื–ื” ืœื ืžืื•ื‘ื˜ื— ื‘ื›ืœืœ. ืื ื™ ืžืขื“ื™ืฃ ืœื›ืชื•ื‘ ื‘ื•ื˜ ื˜ืœื’ืจื ืฉืžืงื‘ืœ ืจืง ืคืงื•ื“ื•ืช ื”ืคืขืœื” ืžื”ื—ืฉื‘ื•ืŸ ืฉืœื™.

ื”ื—ืœื˜ืชื™ ืœื›ืชื•ื‘ ื‘ื•ื˜ ื›ื–ื”.

ืื–, ื”ื“ื‘ืจ ื”ืจืืฉื•ืŸ ืฉืฆืจื™ืš ืœืขืฉื•ืช ื”ื•ื ืœื™ืฆื•ืจ ื‘ื•ื˜ ื‘ื˜ืœื’ืจื.

  • ืื ื• ืžื•ืฆืื™ื ื‘ื—ื™ืคื•ืฉ ื—ืฉื‘ื•ืŸ ื‘ืฉื @botfather
  • ืœื—ืฅ ืขืœ ื›ืคืชื•ืจ ื”ืชื—ืœ ื‘ืชื—ืชื™ืช ื”ืžืกืš
  • ื•ืื– ื ื›ืชื•ื‘ ืœื• ืืช ื”ืคืงื•ื“ื” / newbot

ืœืื—ืจ ืžื›ืŸ ื ืขื ื” ืขืœ 2 ืฉืืœื•ืช ืคืฉื•ื˜ื•ืช:

  • ื”ืฉืืœื” ื”ืจืืฉื•ื ื” ื”ื™ื ืฉื ื”ื‘ื•ื˜ ืฉื™ื™ื•ื•ืฆืจ. MyMikrotikROuter
  • ื”ืฉืืœื” ื”ืฉื ื™ื™ื” ื”ื™ื ื”ื›ื™ื ื•ื™ ืฉืœ ื”ื‘ื•ื˜ ืฉื ื•ืฆืจ (ืฆืจื™ืš ืœื”ืกืชื™ื™ื ื‘ื‘ื•ื˜) MikrotikROuter_bot

ื‘ืชื’ื•ื‘ื”, ื ืงื‘ืœ ืืช ื”ืืกื™ืžื•ืŸ ืฉืœ ื”ื‘ื•ื˜ ืฉืœื ื•, ื‘ืžืงืจื” ืฉืœื™ ื”ื•ื:

ื”ืฉืชืžืฉ ื‘ืืกื™ืžื•ืŸ ื”ื–ื” ื›ื“ื™ ืœื’ืฉืช ืืœ ื”-API ืฉืœ HTTP: 265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4

ื”ืคืขืœื” ืžืจื—ื•ืง ืฉืœ ืกืงืจื™ืคื˜ื™ื ืฉืœ Mikrotik ืžื˜ืœื’ืจื
ืœืื—ืจ ืžื›ืŸ, ืขืœื™ืš ืœืžืฆื•ื ืืช ื”ื‘ื•ื˜ ืฉืœื ื• ื‘ื—ื™ืคื•ืฉ ืœืคื™ ืฉื @MikrotikROuter_bot ื•ืœื—ืฅ ืขืœ ืœื—ืฆืŸ ื”ืชื—ืœ.

ืœืื—ืจ ืžื›ืŸ, ืขืœื™ืš ืœืคืชื•ื— ืืช ื”ื“ืคื“ืคืŸ ื•ืœื”ื–ื™ืŸ ืืช ื”ืฉื•ืจื” ื”ื‘ืื”:

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

ื›ืืฉืจ XXXXXXXXXXXXXXXXXX ื”ื•ื ื”ืืกื™ืžื•ืŸ ืฉืœ ื”ื‘ื•ื˜ ืฉืœืš.

ื™ื™ืคืชื— ื“ืฃ ื“ื•ืžื” ืœื–ื”:

ื”ืคืขืœื” ืžืจื—ื•ืง ืฉืœ ืกืงืจื™ืคื˜ื™ื ืฉืœ Mikrotik ืžื˜ืœื’ืจื

ืื ื• ืžื•ืฆืื™ื ื‘ื• ืืช ื”ื˜ืงืกื˜ ื”ื‘ื:

"chat":{"id":631290,

ืื–, ื™ืฉ ืœื ื• ืืช ื›ืœ ื”ืžื™ื“ืข ื”ื“ืจื•ืฉ ืœื›ืชื™ื‘ืช ืกืงืจื™ืคื˜ื™ื ืขื‘ื•ืจ Mikrotik, ื›ืœื•ืžืจ:

ืืกื™ืžื•ืŸ ื‘ื•ื˜: 265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4

ื–ื™ื”ื•ื™ ืฆ'ืื˜ ืฉื‘ื• ืขืœื™ื• ืœื›ืชื•ื‘: 631290

ื›ื“ื™ ืœื‘ื“ื•ืง, ื ื•ื›ืœ ืœืขื‘ื•ืจ ื“ืจืš ื”ื“ืคื“ืคืŸ:

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

ืืžื•ืจ ืœืงื‘ืœ ืืช ื”ืชื•ืฆืื”:

ื”ืคืขืœื” ืžืจื—ื•ืง ืฉืœ ืกืงืจื™ืคื˜ื™ื ืฉืœ Mikrotik ืžื˜ืœื’ืจื

ืœื ื•ื—ื™ื•ืชื ื•, ื ื•ืกื™ืฃ ืžื™ื“ ืคืงื•ื“ื•ืช ืขื‘ื•ืจ ื”ื‘ื•ื˜:

ืžืฆื™ืืช ื—ืฉื‘ื•ืŸ ืขื ืฉื @botfather
ื•ืื– ื ื›ืชื•ื‘ ืœื• ืคืงื•ื“ื” / ืงื‘ื•ืฆื•ืช ืคืงื•ื“ื•ืช

  • ื”ื•ื ื™ืฉืืœ ืื™ื–ื” ื‘ื•ื˜

ืื ื—ื ื• ื›ื•ืชื‘ื™ื:
@MikrotikROuter_bot

ื”ื•ืกืฃ ืคืงื•ื“ื•ืช:

  • helloworld< โ€” ื”ื•ื“ืขืช ื‘ื“ื™ืงื” ื‘ืฆ'ืื˜ 1
  • itsworking- ื”ื•ื“ืขืช ื‘ื“ื™ืงื” ื‘ืฆ'ืื˜ 2
  • wolmypc-ืœื”ืขื™ืจ ืืช ื”ืžื—ืฉื‘ ืฉืœื™

ื›ืขืช ืื ืชืงืœื™ื“ "/" ื‘ืฆ'ืื˜, ืืชื” ืืžื•ืจ ืœืงื‘ืœ:

ื”ืคืขืœื” ืžืจื—ื•ืง ืฉืœ ืกืงืจื™ืคื˜ื™ื ืฉืœ Mikrotik ืžื˜ืœื’ืจื

ืขื›ืฉื™ื• ื‘ื•ืื• ื ืขื‘ื•ืจ ืœ-MikroTik.

ืœ- RouterOS ื™ืฉ ื›ืœื™ ืขื–ืจ ืœืžืกื•ืฃ ืœื”ืขืชืงืช ืงื‘ืฆื™ื ื“ืจืš ftp ืื• http / https, ื”ื›ืœื™ ื ืงืจื fetch, ื•ื–ื” ืžื” ืฉื ืฉืชืžืฉ ื‘ื•.

ืคืชื•ื— ืžืกื•ืฃ ื•ื”ื–ืŸ:

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

ืฉื™ืžื• ืœื‘ ืฉืžื™ืงืจื•ื˜ื™ืง ืฆืจื™ืš "ยป ืœื‘ืจื•ื— ืžื”ืฉืœื˜ ยซ?' ื‘ื›ืชื•ื‘ืช ื”ืืชืจ.

ืืžื•ืจ ืœืงื‘ืœ ืืช ื”ืชื•ืฆืื”:

ื”ืคืขืœื” ืžืจื—ื•ืง ืฉืœ ืกืงืจื™ืคื˜ื™ื ืฉืœ Mikrotik ืžื˜ืœื’ืจื

ื›ืขืช ื ืขื‘ื•ืจ ืœืชืกืจื™ื˜ื™ื:

ืฉืœื•ื ืขื•ืœื

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}

ื–ื” ืขื•ื‘ื“

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"

ืืœ ืชืฉื›ื— ืœืฆื™ื™ืŸ ืืช ืฉื ื”-mac ื•ื”ืžืžืฉืง ื”ื ื›ื•ืŸ, ื›ืžื• ื’ื bot-token ื•-chat_id.

ืขื›ืฉื™ื• ืื ื™ ืืกื‘ื™ืจ ืงืฆืช ืžื” ื”ื ืขื•ืฉื™ื:

ื”ืชืกืจื™ื˜ "helloworld" ืฉื•ืœื— ื”ื•ื“ืขื”: "ืฉืœื•ื, ืขื•ืœื!" ืœืฆ'ืื˜ ืฉืœื ื• ืขื ื”ื‘ื•ื˜.
ื”ืชืกืจื™ื˜ "itsworking" ืฉื•ืœื— ื”ื•ื“ืขื”: "ื‘ื“ื•ืง ื‘ืกื“ืจ, ื–ื” ืขื•ื‘ื“!" ืœืฆ'ืื˜ ืฉืœื ื• ืขื ื”ื‘ื•ื˜.
ืกืงืจื™ืคื˜ื™ื ืืœื• ืžื™ื•ืขื“ื™ื ืœืžื˜ืจื•ืช ื”ื“ื’ืžื”.
ื”ื•ืกืคืชื™ ืืช ื”ืกืงืจื™ืคื˜ "wolmypc" ื›ืื—ื“ ื”ืžื™ืžื•ืฉื™ื ื”ืืคืฉืจื™ื™ื.
ืขื ื‘ื™ืฆื•ืข ื”ืกืงืจื™ืคื˜, ื”ื‘ื•ื˜ ื™ื›ืชื•ื‘ "wol OK" ืœืฆ'ืื˜.
ืœืžืขืฉื”, ืืชื” ื™ื›ื•ืœ ืœื”ืจื™ืฅ ื›ืœ ืกืงืจื™ืคื˜ ืœื—ืœื•ื˜ื™ืŸ.

ืฆื•ืจ ืžืฉื™ืžื”:

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

ืชืฆื•ื’ื” ื ื™ืชื ืช ืœืงืจื™ืื”ืœื ื‘ืจื•ืจ ืœืžื”, ืื‘ืœ ืžื”ืกืงืจื™ืคื˜ ื”ืคื•ืขืœ ื”ื•ื ืœื ื—ื•ืฉืฃ ื ืชื•ื ื™ื ื’ืœื•ื‘ืœื™ื™ื, ื”ื•ืกื™ืฃ ืืช ื”ืกืงืจื™ืคื˜ ื›ืฉื”ืžืขืจื›ืช ืžืืชื—ืœืช.
ื”ืคืขืœื” ืฉืœ ื˜ืœื’ืจื

: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") ;

ืžื‘ืจืง

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

ืึตื™ืš ืžึทืคืขึดื™ืœึดื™ื ืึถืช ื–ึถื”

ื”ื•ืจื“ ืืช ื”ื•ื“ืขื•ืช "getUpdates" ืฉืœื ื• ื›ืœ 30 ืฉื ื™ื•ืช, ื•ืื– ื ืชื— ื›ื“ื™ ืœื’ืœื•ืช update_id (ืžืกืคืจ ื”ื•ื“ืขื”) ื• ื˜ึถืงืกื˜ (ื”ืฆื•ื•ืชื™ื ืฉืœื ื•) ื• chat_id . ื›ื‘ืจื™ืจืช ืžื—ื“ืœ, getUpdates ืžืฆื™ื’ ืž-1 ืขื“ 100 ื”ื•ื“ืขื•ืช, ืžื˜ืขืžื™ ื ื•ื—ื•ืช, ืœืื—ืจ ืงืจื™ืืช ื”ืคืงื•ื“ื”, ืื ื• ืžื•ื—ืงื™ื ืืช ื”ื”ื•ื“ืขื”. Telegram api ืื•ืžืจ ืฉื›ื“ื™ ืœืงืจื•ื ื”ื•ื“ืขื” ืืชื” ืฆืจื™ืš ืืช ืžืกืคืจ ื”ื”ื•ื“ืขื” + 1

/getUpdates?offset=update_id + 1

ื”ื›ืœ ื ื‘ื“ืง ื‘-Mikrotik rb915 RouterOS 6.37.1
ืื ืชืฉืœื— ืคืงื•ื“ื•ืช ืจื‘ื•ืช ื‘ื‘ืช ืื—ืช, ื›ื•ืœืŸ ื™ื‘ื•ืฆืขื• ื‘ืชื•ืจืŸ ื‘ืžืจื•ื•ื— ืฉืœ 30 ืฉื ื™ื•ืช.

ื .ื‘ ืชื•ื“ื” ืจื‘ื” ืœืงื™ืจื™ืœ ืงื–ืงื•ื‘ ืขืœ ื”ืจืขื™ื•ืŸ ื•ืœื—ื‘ืจ ืฉืœื™ ืืœื›ืกื ื“ืจ ืขืœ ื”ืขื–ืจื” ื‘ืชืกืจื™ื˜ื™ื.

ืชื–ื›ื•ืจ

habrahabr.ru/post/313794
1spla.ru/index.php/blog/telegram_bot_for_mikrotik
core.telegram.org/bots/api
wiki.mikrotik.com/wiki/Manual:ืชืกืจื™ื˜

UPD:

03:11:16

ืกืงืจื™ืคื˜ื™ื ืžืฉื•ืคืจื™ื:

ื ื•ืกืฃ ื‘ื“ื™ืงื” ืขื‘ื•ืจ chat_id
ื‘ื•ื“ืงื™ื ืื ื™ืฉ ื˜ื™ืคืฉ, ืื ืžื™ืฉื”ื• ื™ื›ืชื•ื‘ ืœื‘ื•ื˜ ืฉืœื ื•, ื”ื•ื ื™ืขื ื” ืœื•: "ืื ื™ ืœื ื™ื›ื•ืœ ืœื“ื‘ืจ ืื™ืชืš. ", ื™ืขื ื” ืœื ื• ื‘ืื•ืคืŸ ื“ื•ืžื” ืื ื”ื•ื ืœื ื™ื–ื”ื” ืืช ื”ืคืงื•ื“ื”.
ืœืื—ืจ ื‘ื™ืฆื•ืข ื”ืคืงื•ื“ื”, ื”ื‘ื•ื˜ ืžื‘ื˜ืœ ืืช ื”ืžื ื•ื™ ืœืฆ'ืื˜ (ืจืื” ืกืงืจื™ืคื˜ wolmypc)

UPD

ื ืžืฆื ืขื 7 ืคืขืœื•ืœืŸ7 ืฉืงื•ื‘ืฅ ืขื ืœืžืขืœื” ืž-14 ื”ื•ื“ืขื•ืช ื›ื‘ืจ ืœื ืžืขื•ื‘ื“ ืขืœ ื™ื“ื™ ื”ืคืงื•ื“ื” find (ืžื’ื‘ืœื•ืช ืžื™ืงืจื•ื˜ื™ืง). ืœื›ืŸ, ื‘ืขืชื™ื“, ืืฉื ื” ืืช ื”ืชืกืจื™ื˜ ืœ-lua, ืชื•ื“ื” 7 ืคืขืœื•ืœืŸ7 ื‘ืฉื‘ื™ืœ ื–ื”, ืœื ื™ื“ืขืชื™ ืขืœ ืœื•ืื”.

ืขื“ื›ื•ืŸ 08.12.2016

ื‘ื˜ืœื’ืจื, ื›ื›ืœ ื”ื ืจืื”, ืฉื™ื ื• ืžืขื˜ ืืช ื”"ืžืคืœื˜" ืฉืœ getUpdate. ื›ืขืช ื‘ืกืงืจื™ืคื˜ ื”ืจืืฉื™ ืขืœื™ืš ืœืชืงืŸ ืืช ื”ื™ืกื˜ ื”ื”ื•ื“ืขื” ืž-2 ืœ-1

ืฉื™ื ื•ื™ื™ื

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

ะทะฐะผะตะฝะธั‚ัŒ ะฝะฐ :

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

ืžืงื•ืจ: www.habr.com