U-Alexander Koryukin ungiphushele kulokhu kuqaliswa
Futhi ukuphawula kwelinye lamaqembu e-VK kaKirill Kazakov:
Yebo, akuphephile nhlobo. Ngingathanda ukubhala ibhot yetelegram eyamukela kuphela imiyalo yokusebenzisa ku-akhawunti yami.
Nganquma ukubhala i-bot enjalo.
Ngakho-ke, into yokuqala okufanele uyenze ukudala i-bot ku-telegraph.
- Oseshweni sithola i-akhawunti ebizwa ngokuthi @botfather
- Chofoza inkinobho ethi Qala ngaphansi kwesikrini
- Bese simbhalela umyalo / newbot
Bese siphendula imibuzo emi-2 elula:
- Umbuzo wokuqala yigama le-bot ezokwakhiwa. I-MyMikrotikROuter
- Umbuzo wesibili yisidlaliso se-bot edalwayo (kufanele iphele nge-bot) I-MikrotikROuter_bot
Ekuphenduleni, sizothola ithokheni ye-bot yethu, kimina ithi:
Sebenzisa le tokheni ukuze ufinyelele i-HTTP API: 265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4
Bese, udinga ukuthola i-bot yethu ekusesheni ngegama @MikrotikROuter_bot bese ucindezela inkinobho Qala.
Ngemuva kwalokho, udinga ukuvula isiphequluli bese ufaka umugqa olandelayo:
https://api.telegram.org/botXXXXXXXXXXXXXXXXXX/getUpdates
Lapho i-XXXXXXXXXXXXXXXXXX iwuphawu lwebhothi yakho.
Ikhasi elifana nalokhu okulandelayo lizovuleka:
Sithola umbhalo olandelayo kuyo:
"chat":{"id":631290,
Ngakho-ke, sinayo yonke imininingwane edingekayo yokubhala imibhalo ye-Mikrotik, okungukuthi:
Ithokheni ye-Bot: 265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4
I-ID yengxoxo lapho kufanele abhale khona: 631290
Ukuhlola, singadlula esipheqululini:
https://api.telegram.org/bot265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4/sendmessage?chat_id=631290&text=test
Kufanele uthole umphumela:
Ukuze sibe lula, sizofaka ngokushesha imiyalo ye-bot:
Ukuthola i-akhawunti enegama @botfather
Bese simbhalela umyalo / setcommands
- Uzobuza ukuthi iyiphi i-bot
Siyabhala:
@MikrotikROuter_bot
Engeza imiyalo:
- helloworld< - Umlayezo wokuhlola engxoxweni 1
- I-itsworking-Test Message on chat 2
- wolmypc-vusa i-PC yami
Manje uma uthayipha u-"/" engxoxweni, kufanele uthole:
Manje ake sidlulele ku-MikroTik.
I-RouterOS inensiza ye-console yokukopisha amafayela nge-ftp noma i-http / https, insiza ibizwa ngokuthi ukulanda, okuyinto esizoyisebenzisa.
Vula Isikhumulo bese ufaka:
/tool fetch url="https://api.telegram.org/bot265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4/sendmessage?chat_id=631290&text=test " keep-result=no
Sicela uqaphele ukuthi iMikroTik idinga "Β»ukubalekela uphawu Β«?' ku-URL.
Kufanele uthole umphumela:
Manje ake sidlulele kumaskripthi:
Sawubona Mhlaba
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}
ukusebenza kwayo
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"
Ungakhohlwa ukucacisa igama elilungile le-mac nesixhumi esibonakalayo, kanye ne-bot-token ne-chat_id.
Manje ngizochaza kancane ukuthi benzani:
Umbhalo othi "helloworld" uthumela umlayezo: "Sawubona, mhlaba!" engxoxweni yethu ne-bot.
Umbhalo othi "ukusebenza kwayo" uthumela umlayezo: "Hlola KULUNGILE, Kuyasebenza!" engxoxweni yethu ne-bot.
Le mibhalo yenzelwe izinjongo zokubonisa.
Ngengeze iskripthi se-"wolmypc" njengenye yezinto ezingase zisetshenziswe.
Ngemva kokwenza iskripthi, i-bot izobhala "wol OK" engxoxweni.
Eqinisweni, ungasebenzisa ngokuphelele noma yisiphi isikripthi.
Dala umsebenzi:
I-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
Ukubuka Okufundekayoakucaci ukuthi kungani, kodwa kusukela kuskripthi esisebenzayo ayivezi idatha yomhlaba wonke, yengeze umbhalo lapho isistimu iqala.
Ukuqala kweTelegram
: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") ;
yocingo
: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;
}
Kanjani lo msebenzi
Landa imilayezo yethu ethi "getUpdates" njalo ngemizuzwana engu-30, bese uyayihlaziya ukuze uthole update_id (inombolo yomlayezo) kanye umbhalo (amaqembu ethu) kanye i-chat_id . Ngokuzenzakalelayo, i-getUpdates ibonisa kusuka kumilayezo engu-1 kuye kweyi-100, ukuze kube lula, ngemva kokufunda umyalo, sisusa umlayezo. I-Telegram api ithi ukuze ufunde umlayezo udinga inombolo yomlayezo + 1
/getUpdates?offset=update_id + 1
Konke kuhlolwe ku-Mikrotik rb915 RouterOS 6.37.1
Uma uthumela imiyalo eminingi ngesikhathi esisodwa, yonke izokwenziwa ngokushintshana ngesikhawu samasekhondi angu-30.
PS Ngibonga kakhulu kuKirill Kazakov ngombono kanye nomngane wami u-Alexander ngosizo ngemibhalo.
izithenjwa
buyekeza:
03:11:16
Imibhalo ethuthukisiwe:
Kwengezwe isheke le-chat_id
Ukuhlola isiwula, uma othile ebhalela i-bot yethu, uzomphendula: "Angikwazi ukukhuluma nawe. ", izosiphendula ngendlela efanayo uma ingawuboni umyalo.
Ngemva kokukhipha umyalo, i-bot izikhipha ohlwini lwengxoxo (bona umbhalo we-wolmypc)
I-UPD
Kutholwe nge
I-UPD 08.12.2016
kuTelegram, ngokusobala, bashintshe kancane "i-exhaust" ye-getUpdate. manje kusikripthi esikhulu udinga ukulungisa ukuguqulwa komlayezo ukusuka ku-2 kuye ku-1
shintsha
:local message [:pick $content ($startLoc + 2) $endLoc] ;
Π·Π°ΠΌΠ΅Π½ΠΈΡΡ Π½Π° :
:local message [:pick $content ($startLoc + 1) $endLoc] ;
Source: www.habr.com