Alexander Koryukin kpaliri m ime nke a
Na nkwupụta na otu n'ime VK otu site Kirill Kazakov:
Ee, ọ nweghị nchekwa ma ọlị. Ọ ga-akara m mma dee bot telegram nke na-anabata naanị iwu ịgbalite na akaụntụ m.
Ekpebiri m ide ụdị bot.
Yabụ, ihe mbụ ị ga - eme bụ imepụta bot na telegram.
- Anyị na-ahụ na nchọta akaụntụ aha @botfather
- Pịa na Malite bọtịnụ na ala nke ihuenyo
- Mgbe ahụ, anyị na-ede ya iwu / newbot
Anyị zaa ajụjụ abụọ dị mfe:
- Ajụjụ mbụ bụ aha bot a ga-emepụta. MyMikrotikRouter
- Ajụjụ nke abụọ bụ aha njirimara nke bot a na-emepụta (kwesịrị ịkwụsị na bot) MikrotikROuter_bot
Na nzaghachi, anyị ga-enweta akara ngosi nke bot anyị, n'ọnọdụ m ọ bụ:
Jiri akara ngosi a iji nweta HTTP API: 265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4
Mgbe ahụ, ịkwesịrị ịchọta bot anyị na nchọta aha @MikrotikROuter_bot wee pịa bọtịnụ Malite.
Mgbe nke ahụ gasịrị, ịkwesịrị imepe ihe nchọgharị ahụ wee tinye ahịrị ndị a:
https://api.telegram.org/botXXXXXXXXXXXXXXXXXX/getUpdates
Ebe XXXXXXXXXXXXXXXXXX bụ akara bot gị.
Ibe nke yiri nke a ga-emeghe:
Anyị na-ahụ ederede na-esonụ na ya:
"chat":{"id":631290,
Yabụ, anyị nwere ozi niile dị mkpa maka ide edemede maka Mikrotik, ya bụ:
Ihe ngosi bot: 265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4
Nkata nkata ebe o kwesịrị ide: 631290
Iji lelee, anyị nwere ike ịgafe na ihe nchọgharị:
https://api.telegram.org/bot265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4/sendmessage?chat_id=631290&text=test
Kwesịrị nweta nsonaazụ:
Maka ịdị mma anyị, anyị ga-agbakwunye iwu ozugbo maka bot:
Ịchọta akaụntụ nwere aha @nna nna
Anyị wee dee ya iwu / iwu
- Ọ ga-ajụ nke bot
Anyị na-ede:
@MikrotikROuter_bot
Tinye iwu:
- helloworld<-Nlele ozi na nkata 1
- Ozi ule na-arụ ọrụ na nkata 2
- wolmypc-kpọlie PC m
Ugbu a ọ bụrụ na ị pịnye "/" na nkata ahụ, ị ga-enweta:
Ugbu a ka anyị gaa na MikroTik.
RouterOS nwere akụrụngwa njikwa maka idetu faịlụ site na ftp ma ọ bụ http / https, akụrụngwa a na-akpọ fetch, nke bụ ihe anyị ga-eji.
Meghee Ọnụ ma banye:
/tool fetch url="https://api.telegram.org/bot265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4/sendmessage?chat_id=631290&text=test " keep-result=no
Biko mara na MikroTik chọrọ"» ịgbanahụ akara «?' na URL.
Kwesịrị nweta nsonaazụ:
Ugbu a, ka anyị gaa n'ihu na edemede:
Ndewo Ụwa
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}
na-arụ ọrụ ya
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"
Echefula ezipụta aha mac na interface ziri ezi yana bot-token na chat_id.
Ugbu a, m ga-akọwa ntakịrị ihe ha na-eme:
Edemede "helloworld" na-eziga ozi: "Ndewo, ụwa!" ka anyị na bot kparịta ụka.
Edemede "ọrụ ya" na-eziga ozi: "Nnwale OK, ọ na-arụ ọrụ!" ka anyị na bot kparịta ụka.
Edemede ndị a bụ maka ebumnuche ngosi.
Agbakwunyere m edemede "wolmypc" dịka otu n'ime mmejuputa a ga-ekwe omume.
Mgbe emechara edemede ahụ, bot ga-ede "wol OK" na nkata ahụ.
N'ezie, ị nwere ike na-agba ọsọ kpamkpam ọ bụla edemede.
Mepụta ọrụ:
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
Nlele enwere ike ịgụọ bụghị ihe doro anya ihe kpatara ya, ma site na edemede na-arụ ọrụ ọ naghị egosipụta data zuru ụwa ọnụ, gbakwunyere edemede ahụ mgbe akpụkpọ ụkwụ usoro.
Telegram mmalite
: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;
}
Olee otú nke a na-arụ ọrụ
Budata ozi “getUpdates” anyị kwa sekọnd 30, wee tụgharịa ka ịmata ya update_id (nọmba ozi) na ederede (ndị otu anyị) na nkata_id . Site na ndabara, getUpdates na-egosiputa site na ozi 1 ruo 100, maka ịdị mma, mgbe anyị gụchara iwu ahụ, anyị na-ehichapụ ozi ahụ. Telegram api kwuru ka ịgụọ ozi ị ga-achọ nọmba ozi + 1
/getUpdates?offset=update_id + 1
A nwalere niile na Mikrotik rb915 RouterOS 6.37.1
Ọ bụrụ na izipu ọtụtụ iwu n'otu oge, a ga-egbu ha niile n'otu oge na nkeji iri atọ.
PS Ọtụtụ ekele Kirill Kazakov maka echiche na enyi m Alexander maka enyemaka na edemede.
zoro
emelitere:
03:11:16
Ederede emelitere:
agbakwunyere nlele maka chat_id
Na-enyocha onye nzuzu, ọ bụrụ na mmadụ edegara bot anyị akwụkwọ, ọ ga-aza ya: "Enweghị m ike ịgwa gị okwu. ", ga-azakwa anyị n'otu aka ahụ ma ọ bụrụ na ọ mataghị iwu ahụ.
Mgbe emechara iwu ahụ, bot na-ewepụ aha na nkata ahụ (lee script wolmypc)
DUP
Achọtara ya
UPD 08.12.2016/XNUMX/XNUMX
na Telegram, o doro anya na ha gbanwetụrụ "agwụcha" nke getUpdate. Ugbu a na isi edemede ị ga-edozi ngbanwe nke ozi site na 2 ruo 1
gbanwee
:local message [:pick $content ($startLoc + 2) $endLoc] ;
заменить на :
:local message [:pick $content ($startLoc + 1) $endLoc] ;
isi: www.habr.com