Alexander Koryukin akandisundidzira kukuita uku
Uye chirevo mune rimwe remapoka eVK naKirill Kazakov:
Ehe, haina kuchengetedzeka zvachose. Ini ndoda kunyora telegraph bot inongogamuchira activation mirairo kubva kuaccount yangu.
Ndakasarudza kunyora bot yakadaro.
Saka, chinhu chekutanga kuita kugadzira bot muteregiramu.
- Isu tinowana mukutsvaga account inonzi @botfather
- Dzvanya bhatani reKutanga pazasi pescreen
- Zvadaro tinomunyorera murairo / newbot
Zvadaro tinopindura mibvunzo miviri iri nyore:
- Mubvunzo wekutanga ndiro zita rebhoti kuti rigadzirwe. MyMikrotikROuter
- Mubvunzo wechipiri ndiro zita remadunhurirwa rekuti bot iri kugadzirwa (inofanira kupera ne bot) MikrotikROuter_bot
Mukupindura, isu tinogashira chiratidzo chebhoti yedu, mune yangu iri:
Shandisa chiratidzo ichi kuwana HTTP API: 265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4
Ipapo, iwe unofanirwa kutsvaga bot yedu mukutsvaga nemazita @MikrotikROuter_bot uye tinya bhatani rekutanga.
Mushure meizvozvo, iwe unofanirwa kuvhura browser uye isa mutsara unotevera:
https://api.telegram.org/botXXXXXXXXXXXXXXXXXX/getUpdates
Iko XXXXXXXXXXXXXXXXXX ndiyo chiratidzo chebhotoro rako.
Peji rakafanana neinotevera richavhurwa:
Tinowana chinyorwa chinotevera pairi:
"chat":{"id":631290,
Saka, isu tine ruzivo rwese rwunodiwa pakunyora zvinyorwa zveMikrotik, zvinoti:
Bot token: 265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4
Chat ID kwaanofanira kunyora: 631290
Kuti titarise, tinogona kuenda kuburikidza nebrowser:
https://api.telegram.org/bot265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4/sendmessage?chat_id=631290&text=test
Inofanira kuwana mhedzisiro:
Kuti zvitiitire nyore, isu tichakurumidza kuwedzera mirairo ye bot:
Kutsvaga account ine zita @botfather
Zvadaro tinomunyorera murairo / setcommands
- Achabvunza kuti bhoti ipi
Tinonyora:
@MikrotikROuter_bot
Wedzera mirairo:
- helloworld< - Yedza meseji pakutaura 1
- itsworking-Test Message pane chat 2
- wolmypc-mutsa PC yangu
Zvino kana ukanyora "/" muhurukuro, unofanira kuwana:
Zvino ngatipfuurirei kuMikroTik.
RouterOS ine koni yekushandisa yekukopa mafaera kuburikidza neftp kana http / https, iyo yekushandisa inonzi tora, ndizvo zvatichashandisa.
Vhura terminal uye pinda:
/tool fetch url="https://api.telegram.org/bot265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4/sendmessage?chat_id=631290&text=test " keep-result=no
Ndokumbira utarise kuti MikroTik inoda "Β» kutiza chiratidzo Β«?' mu URL.
Inofanira kuwana mhedzisiro:
Iye zvino ngatienderere mberi kune zvinyorwa:
donel
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}
itsworking
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"
Usakanganwa kutsanangura iyo chaiyo mac uye zita rekushandisa, pamwe nebot-token uye chat_id.
Zvino ini ndichatsanangura zvishoma zvavanoita:
Iyo "helloworld" script inotumira meseji: "Mhoro, nyika!" kune yedu hurukuro ne bot.
Chinyorwa che "itsworking" chinotumira meseji: "Edza OK, Zviri Kushanda!" kune yedu hurukuro ne bot.
Manyoro aya ndeezvinangwa zvekuratidzira.
Ini ndakawedzera iyo "wolmypc" script seimwe yezvinogoneka kuita.
Pakuitwa kweiyo script, iyo bot inonyora "wol OK" kune chat.
Muchokwadi, iwe unogona kumhanya zvachose chero script.
Gadzira basa:
Telegraph.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
Readable Viewhazvina kujeka kuti sei, asi kubva kune inoshanda script haina kuburitsa data yepasirese, yakawedzera script kana system boots.
Telegraph yekutanga
: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") ;
teregiramu
: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;
}
Sei basa iri
Dhawunirodha mameseji edu e "getUpdates" masekonzi makumi matatu ega ega, wobva waongorora kuti uzive update_id (nhamba yemeseji) uye chinyorwa (zvikwata zvedu) uye chat_id . Nekutadza, GetUpdates inoratidza kubva pa1 kusvika ku100 mameseji, kuitira nyore, mushure mekuverenga murairo, tinodzima meseji. Telegraph api inoti kuverenga meseji unoda iyo meseji nhamba + 1
/getUpdates?offset=update_id + 1
Zvese zvakaedzwa paMikrotik rb915 RouterOS 6.37.1
Kana iwe ukatumira mirairo yakawanda kamwechete, iyo yese inozoitwa pamwe chete nekufamba kwemasekonzi makumi matatu.
PS Kutenda kwakawanda kuna Kirill Kazakov kune iyo pfungwa uye shamwari yangu Alexander kuti abatsirwe nezvinyorwa.
nezvakanyorwa
upd:
03:11:16
Zvinyorwa zvakavandudzwa:
Yakawedzerwa cheki ye chat_id
Kutarisa benzi, kana mumwe munhu akanyorera bot yedu, anomupindura: "Handigone kutaura newe. ", ichatipindura zvakafanana kana isingazive murairo.
Mushure mekuita murairo, bhoti inoregeredza kune chat (ona wolmypc script)
DUP
Yakawanikwa ne
UPD 08.12.2016/XNUMX/XNUMX
muTeregiramu, sezviri pachena, vakachinja zvishoma "kupedza" kweGetUpdate. ikozvino mune huru script iwe unofanirwa kugadzirisa iyo meseji offset kubva 2 kusvika 1
change
:local message [:pick $content ($startLoc + 2) $endLoc] ;
Π·Π°ΠΌΠ΅Π½ΠΈΡΡ Π½Π° :
:local message [:pick $content ($startLoc + 1) $endLoc] ;
Source: www.habr.com