Kev ua kom cov chaw taws teeb ntawm Mikrotik scripts los ntawm Telegram

Alexander Koryukin thawb kuv mus rau qhov kev siv no GeXoGeN nrog nws cov ntawv tshaj tawmChaw taws teeb tig rau hauv lub computer dawb, tsis muaj SMS thiab tsis muaj huab, siv Mikrotik".

Thiab cov lus hais hauv ib pawg VK los ntawm Kirill Kazakov:

Yog, nws tsis ruaj ntseg kiag li. Kuv xav sau ib lub telegram bot uas tsuas yog lees txais cov lus txib kom ua los ntawm kuv tus account.

Kuv txiav txim siab sau li no bot.

Yog li, thawj qhov uas yuav tsum tau ua yog tsim bot hauv telegram.

  • Peb pom hauv kev tshawb nrhiav tus account npe @botfather
  • Nyem rau ntawm Start khawm nyob hauv qab ntawm qhov screen
  • Tom qab ntawd peb sau nws cov lus txib / newbot

Tom qab ntawd peb teb 2 cov lus nug yooj yim:

  • Thawj lo lus nug yog lub npe ntawm bot yuav tsum tau tsim. MyMikrotikROuter
  • Cov lus nug thib ob yog lub npe menyuam yaus ntawm bot tau tsim (yuav tsum xaus nrog bot) MikrotikROuter_bot

Hauv kev teb, peb yuav tau txais lub cim ntawm peb lub bot, hauv kuv rooj plaub nws yog:

Siv qhov token no kom nkag mus rau HTTP API: 265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4

Kev ua kom cov chaw taws teeb ntawm Mikrotik scripts los ntawm Telegram
Tom qab ntawd, koj yuav tsum nrhiav peb bot hauv kev tshawb nrhiav los ntawm lub npe @MikrotikROuter_bot thiab nias lub pob pib.

Tom qab ntawd, koj yuav tsum qhib qhov browser thiab nkag mus rau kab hauv qab no:

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

Qhov twg XXXXXXXXXXXXXXXXXX yog koj tus bot's token.

Ib nplooj ntawv zoo ib yam li cov hauv qab no yuav qhib:

Kev ua kom cov chaw taws teeb ntawm Mikrotik scripts los ntawm Telegram

Peb pom cov ntawv hauv qab no ntawm nws:

"chat":{"id":631290,

Yog li, peb muaj tag nrho cov ntaub ntawv tsim nyog rau kev sau ntawv rau Mikrotik, uas yog:

Bot token: 265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4

Tham ID qhov twg nws yuav tsum sau: 631290

Txhawm rau txheeb xyuas, peb tuaj yeem mus dhau ntawm qhov browser:

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

Yuav tsum tau txais qhov tshwm sim:

Kev ua kom cov chaw taws teeb ntawm Mikrotik scripts los ntawm Telegram

Rau peb txoj kev yooj yim, peb yuav tam sim ntxiv cov lus txib rau bot:

Nrhiav ib tus account nrog lub npe @botfather
Ces peb sau nws ib lo lus txib / setcommands

  • Nws yuav nug tus bot twg

Peb sau:
@MikrotikROuter_bot

Ntxiv cov lus txib:

  • helloworld< β€” Cov lus xeem ntawm kev sib tham 1
  • itsworking-Test Message ntawm kev sib tham 2
  • wolmypc-tsim kuv lub PC

Tam sim no yog tias koj ntaus "/" hauv kev sib tham, koj yuav tsum tau txais:

Kev ua kom cov chaw taws teeb ntawm Mikrotik scripts los ntawm Telegram

Tam sim no cia peb tsiv mus rau MikroTik.

RouterOS muaj cov khoom siv hluav taws xob rau kev luam cov ntaub ntawv ntawm ftp lossis http / https, qhov hluav taws xob hu ua fetch, uas yog qhov peb yuav siv.

Qhib davhlau ya nyob twg thiab nkag mus:

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

Thov nco ntsoov tias MikroTik xav tau "Β» kom dim lub cim Β«?' hauv URL.

Yuav tsum tau txais qhov tshwm sim:

Kev ua kom cov chaw taws teeb ntawm Mikrotik scripts los ntawm Telegram

Tam sim no cia peb mus rau scripts:

helloworld

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}

nws ua haujlwm

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 ua

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"

Tsis txhob hnov ​​​​qab qhia qhov tseeb mac thiab lub npe interface, nrog rau bot-token thiab chat_id.

Tam sim no kuv yuav piav me ntsis txog qhov lawv ua:

Tsab ntawv "helloworld" xa lus: "Nyob zoo, ntiaj teb!" rau peb tham nrog bot.
Tsab ntawv "itsworking" xa lus: "Kuaj OK, nws ua haujlwm!" rau peb tham nrog bot.
Cov ntawv no yog rau kev ua qauv qhia.
Kuv ntxiv tsab ntawv "wolmypc" ua ib qho kev siv tau.
Thaum ua tiav ntawm tsab ntawv, bot yuav sau "wol OK" rau kev sib tham.
Qhov tseeb, koj tuaj yeem khiav txhua tsab ntawv.

Tsim ib txoj haujlwm:

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

Nyeem tau saibnws tsis paub meej vim li cas, tab sis los ntawm cov ntawv ua haujlwm nws tsis nthuav tawm cov ntaub ntawv thoob ntiaj teb, ntxiv tsab ntawv thaum lub kaw lus khau raj.
Telegram pib

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

Yuav ua li cas li no ua hauj lwm

Rub tawm peb cov lus "getUpdates" txhua 30 vib nas this, tom qab ntawd txheeb xyuas kom paub update_id (tus xov tooj) thiab ntawv nyeem (peb pab pawg) thiab chat_id . Los ntawm lub neej ntawd, getUpdates qhia los ntawm 1 txog 100 cov lus, kom yooj yim, tom qab nyeem cov lus txib, peb tshem cov lus. Telegram api hais kom nyeem cov lus koj xav tau cov xov tooj + 1

/getUpdates?offset=update_id + 1

Txhua qhov kev sim ntawm Mikrotik rb915 RouterOS 6.37.1
Yog tias koj xa ntau cov lus txib ib zaug, lawv txhua tus yuav raug tua nrog lub sijhawm ntawm 30 vib nas this.

PS Ntau ua tsaug rau Kirill Kazakov rau lub tswv yim thiab kuv tus phooj ywg Alexander rau kev pab nrog cov ntawv sau.

ua tim khawv

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

upd:

03:11:16

Txhim kho scripts:

Ntxiv kos rau chat_id
Txheeb xyuas tus neeg ruam, yog tias ib tug neeg sau rau peb lub bot, nws yuav teb nws: "Kuv tham tsis tau koj. ", yuav zoo ib yam teb rau peb yog tias nws tsis paub txog cov lus txib.
Tom qab ua tiav cov lus txib, bot unsubscribes rau kev sib tham (saib wolmypc tsab ntawv)

DUP

Pom nrog 7 Stuntman 7 tias cov ntaub ntawv nrog saum toj no ~ 14 cov lus tsis ua tiav los ntawm kev nrhiav cov lus txib (Mikrotik txwv). Yog li ntawd, yav tom ntej kuv yuav hloov tsab ntawv rau lua, ua tsaug 7 Stuntman 7 rau qhov no kuv tsis paub lua.

PAB 08.12.2016

hauv Telegram, pom tau tias, lawv hloov me ntsis "shaust" ntawm getUpdate. tam sim no nyob rau hauv tsab ntawv tseem ceeb koj yuav tsum tau kho cov lus offset los ntawm 2 mus rau 1

hloov

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

Π·Π°ΠΌΠ΅Π½ΠΈΡ‚ΡŒ Π½Π° :

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

Tau qhov twg los: www.hab.com