Ts'ebetso e hole ea mengolo ea Mikrotik ho tsoa ho Telegraph

Alexander Koryukin o ile a ntšusumelletsa ts'ebetsong ena GeXoGeN ka khatiso ea haeHo bulela komporo mahala mahala, ntle le SMS le ntle le maru, u sebelisa Mikrotik".

'Me maikutlo ho se seng sa lihlopha tsa VK ka Kirill Kazakov:

E, ha e sireletsehe ho hang. Nka mpa ka ngola bot ea thelekramo e amohelang feela litaelo tsa ts'ebetso ho tsoa akhaonteng ea ka.

Ke ile ka etsa qeto ea ho ngola bot e joalo.

Kahoo, ntho ea pele eo u lokelang ho e etsa ke ho theha bot ho thelekramo.

  • Ha re batlisisa re fumana ak'haonte e bitsoang @botfather
  • Tobetsa konopo ea Qala ka tlaase ho skrine
  • Ebe re mo ngolla taelo / newbot

Ebe re araba lipotso tse 2 tse bonolo:

  • Potso ea pele ke lebitso la bot le lokelang ho etsoa. MyMikrotikROuter
  • Potso ea bobeli ke lebitso la bosoasoi la bot e ntseng e bōptjoa (e lokela ho fela ka bot) MikrotikROuter_bot

Ha re araba, re tla amohela letšoao la bot ea rona, molemong oa ka ke:

Sebelisa tokeneng ena ho fihlella HTTP API: 265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4

Ts'ebetso e hole ea mengolo ea Mikrotik ho tsoa ho Telegraph
Joale, o hloka ho fumana bot ea rona ha u batla ka mabitso @MikrotikROuter_bot ebe o tobetsa konopo ea Qala.

Ka mor'a moo, o hloka ho bula sebatli ebe o kenya mohala o latelang:

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

Moo XXXXXXXXXXXXXXXXXXXXX e leng letšoao la bot ea hau.

Leqephe le tšoanang le le latelang le tla buleha:

Ts'ebetso e hole ea mengolo ea Mikrotik ho tsoa ho Telegraph

Re fumana lengolo le latelang ho eona:

"chat":{"id":631290,

Kahoo, re na le tlhaiso-leseling e hlokahalang bakeng sa ho ngola mangolo a Mikrotik, e leng:

Bot token: 265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4

Chat ID moo a lokelang ho ngola: 631290

Ho hlahloba, re ka feta ho sebatli:

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

E lokela ho fumana sephetho:

Ts'ebetso e hole ea mengolo ea Mikrotik ho tsoa ho Telegraph

Bakeng sa boiketlo ba rona, hang-hang re tla eketsa litaelo bakeng sa bot:

Ho fumana akhaonto e nang le lebitso @botfather
Ebe re mo ngolla taelo / setcommands

  • O tla botsa hore na ke bot efe

Re ngola:
@MikrotikROuter_bot

Kenya litaelo:

  • helloworld< - Molaetsa oa teko moqoqong oa 1
  • itsworking-Test Message ho chat 2
  • wolmypc-tsosa PC ea ka

Joale haeba u ngola "/" moqoqong, u lokela ho fumana:

Ts'ebetso e hole ea mengolo ea Mikrotik ho tsoa ho Telegraph

Joale a re feteleng pele ho MikroTik.

RouterOS e na le sesebelisoa sa ho kopitsa lifaele ka ftp kapa http / https, sesebelisoa se bitsoa fetch, ke seo re tla se sebelisa.

Bula terminal 'me u kene:

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

Ka kopo hlokomela hore MikroTik e hloka "»ho baleha sesupo «?' ho URL.

E lokela ho fumana sephetho:

Ts'ebetso e hole ea mengolo ea Mikrotik ho tsoa ho Telegraph

Joale ha re feteleng ho li-scripts:

Lefatše Lumela

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}

e sebetsa

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"

Se ke oa lebala ho hlakisa lebitso le nepahetseng la mac le sebopeho, hammoho le bot-token le chat_id.

Joale ke tla hlalosa hanyenyane seo ba se etsang:

Mongolo oa "helloworld" o romela molaetsa: "Lumela, lefatše!" moqoqong oa rona le bot.
Sengoliloeng sa "itsworking" se romella molaetsa: "Lekola OK, E ea Sebetsa!" moqoqong oa rona le bot.
Mengolo ena ke ea sepheo sa lipontšo.
Ke kentse sengoloa sa "wolmypc" e le e 'ngoe ea tse ka sebelisoang.
Ha sengoloa se phethiloe, bot e tla ngola "wol OK" moqoqong.
Ha e le hantle, u ka tsamaisa script ka ho feletseng.

Etsa mosebetsi:

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

Pono e Balehangha e hlake hore na ke hobane'ng, empa ho tsoa ho script e sebetsang ha e senole data ea lefats'e, e kentse script ha tsamaiso e qala.
Ho qala telegram

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

thelekramo

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

Joang mosebetsing ona

Khoasolla melaetsa ea rona ea "getUpdates" ka mor'a metsotsoana e meng le e meng e 30, ebe u bala ho tseba update_id (nomoro ea molaetsa) le mongolo (lihlopha tsa rona) le chat_id . Ka mokhoa o ikhethileng, GetUpdates e bonts'a ho tloha ho 1 ho isa ho 100 melaetsa, bakeng sa boiketlo, kamora ho bala taelo, re hlakola molaetsa. Telegraph api e re ho bala molaetsa o hloka nomoro ea molaetsa + 1

/getUpdates?offset=update_id + 1

Tsohle tse lekiloeng ho Mikrotik rb915 RouterOS 6.37.1
Haeba u romela litaelo tse ngata ka nako e le 'ngoe, kaofela li tla etsoa ka nako ea metsotsoana e 30.

PS Ke leboha haholo Kirill Kazakov bakeng sa khopolo le motsoalle oa ka Alexander bakeng sa thuso ka mangolo.

litšupiso

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

ntlafatsa:

03:11:16

Lingoliloeng tse ntlafalitsoeng:

Cheke e kentsoeng bakeng sa chat_id
Ha re sheba leoatla, haeba motho e mong a ngolla bot ea rona, o tla mo araba: "Ha ke khone ho bua le uena. ", e tla re araba ka mokhoa o tšoanang haeba e sa elelloe taelo.
Kamora ho phethahatsa taelo, bot e itokolla moqoqong (sheba script ea wolmypc)

PUSELETSO

E fumanoe le 7Mohlankana7 hore faele e nang le melaetsa e ka holimo ~ 14 ha e sa sebetsoa ka taelo ea ho fumana (mefokolo ea Mikrotik). Ka hona, nakong e tlang, ke tla fetola script ho lua, kea leboha 7Mohlankana7 bakeng sa sena, ke ne ke sa tsebe ka lua.

UPD 08.12.2016/XNUMX/XNUMX

ho Telegraph, ho hlakile hore ba ile ba fetola "exhaust" ea GetUpdate hanyane. joale ka har'a sengoloa sa mantlha u hloka ho lokisa molaetsa oa offset ho tloha ho 2 ho isa ho 1

fetola

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

заменить на :

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

Source: www.habr.com