Alexander Koryukin tì mi si imuse yii
Ati asọye ninu ọkan ninu awọn ẹgbẹ VK nipasẹ Kirill Kazakov:
Bẹẹni, ko ni aabo rara. Emi yoo kuku kọ bot telegram kan ti o gba awọn aṣẹ imuṣiṣẹ nikan lati akọọlẹ mi.
Mo pinnu lati kọ iru bot kan.
Nitorinaa, ohun akọkọ lati ṣe ni lati ṣẹda bot ni telegram.
- A ri akọọlẹ kan ti a npè ni @botfather ninu wiwa
- Tẹ bọtini Bẹrẹ ni isalẹ iboju naa
- Lẹhinna a kọ aṣẹ naa / newbot
Lẹhinna a dahun awọn ibeere ti o rọrun meji:
- Ibeere akọkọ ni orukọ bot lati ṣẹda. MyMikrotikROuter
- Ibeere keji ni oruko apeso ti bot ti a ṣẹda (o yẹ ki o pari pẹlu bot) MikrotikROuter_bot
Ni idahun, a yoo gba ami ami bot wa, ninu ọran mi o jẹ:
Lo àmi yìí láti ráyè sí HTTP API: 265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4
Lẹhinna, o nilo lati wa bot wa ninu wiwa nipasẹ orukọ @MikrotikROuter_bot ki o si tẹ bọtini Bẹrẹ.
Lẹhin iyẹn, o nilo lati ṣii ẹrọ aṣawakiri ki o tẹ laini atẹle naa:
https://api.telegram.org/botXXXXXXXXXXXXXXXXXX/getUpdates
Nibo XXXXXXXXXXXXXXXXXX jẹ ami ami bot rẹ.
Oju-iwe kan ti o jọra si atẹle yii yoo ṣii:
A ri ọrọ wọnyi lori rẹ:
"iwiregbe":{"id":631290,
Nitorinaa, a ni gbogbo alaye pataki fun kikọ awọn iwe afọwọkọ fun Mikrotik, eyun:
Bot àmi: 265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4
Iwiregbe ID nibiti o yẹ ki o kọ: 631290
Lati ṣayẹwo, a le lọ nipasẹ ẹrọ aṣawakiri:
https://api.telegram.org/bot265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4/sendmessage?chat_id=631290&text=test
O yẹ ki o gba abajade:
Fun irọrun wa, a yoo ṣafikun awọn aṣẹ lẹsẹkẹsẹ fun bot:
Wiwa akọọlẹ kan pẹlu orukọ kan @baba baba
Lẹhinna a kọ aṣẹ fun u / awọn ilana
- Oun yoo beere eyi ti bot
A kọ:
@MikrotikROuter_bot
Ṣafikun awọn aṣẹ:
- helloworld<- Ifiranṣẹ idanwo lori iwiregbe 1
- Ifiranṣẹ idanwo-ṣiṣẹ rẹ lori iwiregbe 2
- wolmypc-ji PC mi soke
Bayi ti o ba tẹ "/" ninu iwiregbe, o yẹ ki o gba:
Bayi jẹ ki ká gbe lori si MikroTik.
RouterOS ni ohun elo console fun didakọ awọn faili nipasẹ ftp tabi http / https, ohun elo naa ni a pe ni fatch, eyiti a yoo lo.
A ṣii ebute ki o si wọle:
/tool fetch url="https://api.telegram.org/bot265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4/sendmessage?chat_id=631290&text=test " keep-result=no
Jọwọ ṣe akiyesi pe MikroTik nilo "» lati sa fun ami naa?' ninu URL naa.
O yẹ ki o gba abajade:
Bayi jẹ ki a lọ si awọn iwe afọwọkọ:
Mo ki O Ile Aiye
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}
iṣẹ rẹ
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"
Maṣe gbagbe lati pato mac ti o pe ati orukọ wiwo, bakanna bi bot-token ati chat_id.
Bayi Emi yoo ṣe alaye diẹ ohun ti wọn ṣe:
Iwe afọwọkọ "helloworld" fi ifiranṣẹ ranṣẹ: "Hello, aye!" si iwiregbe wa pẹlu bot.
Iwe afọwọkọ "ṣiṣẹ rẹ" fi ifiranṣẹ ranṣẹ: "Idanwo O dara, o Nṣiṣẹ!" si iwiregbe wa pẹlu bot.
Awọn iwe afọwọkọ wọnyi wa fun awọn idi ifihan.
Mo ṣafikun iwe afọwọkọ “wolmypc” bi ọkan ninu awọn imuse ti o ṣeeṣe.
Lori ipaniyan ti iwe afọwọkọ, bot yoo kọ “wol O dara” si iwiregbe naa.
Ni otitọ, o le ṣiṣẹ Egba eyikeyi iwe afọwọkọ.
Ṣẹda iṣẹ kan:
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
Wiwo kikako ṣe alaye idi ti, ṣugbọn lati inu iwe afọwọkọ iṣẹ ko ṣe afihan data agbaye, ṣafikun iwe afọwọkọ nigbati awọn bata eto.
Ibẹrẹ 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") ;
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;
}
Báwo ni ise yi
Ṣe igbasilẹ awọn ifiranṣẹ “getUpdates” wa ni gbogbo iṣẹju-aaya 30, lẹhinna ṣe itupalẹ lati wa imudojuiwọn_id (nọmba ifiranṣẹ) ati ọrọ (awọn ẹgbẹ wa) ati iwiregbe_id . Nipa aiyipada, awọn ifihan getUpdates lati awọn ifiranṣẹ 1 si 100, fun irọrun, lẹhin kika aṣẹ, a paarẹ ifiranṣẹ naa. Telegram api sọ pe lati ka ifiranṣẹ o nilo nọmba ifiranṣẹ + 1
/getUpdates?offset=update_id + 1
Gbogbo idanwo lori Mikrotik rb915 RouterOS 6.37.1
Ti o ba fi ọpọlọpọ awọn aṣẹ ranṣẹ ni ẹẹkan, gbogbo wọn yoo ṣiṣẹ ni titan pẹlu aarin iṣẹju-aaya 30.
PS Ọpọlọpọ ọpẹ si Kirill Kazakov fun imọran ati ọrẹ mi Alexander fun iranlọwọ pẹlu awọn iwe afọwọkọ.
jo
imudojuiwọn:
03:11:16
Awọn iwe afọwọkọ ti ilọsiwaju:
Ṣafikun ayẹwo fun chat_id
Ṣiṣayẹwo fun aṣiwere, ti ẹnikan ba kọwe si bot wa, yoo dahun fun u: “Emi ko le ba ọ sọrọ. “, Bakanna yoo dahun wa ti ko ba da aṣẹ naa mọ.
Lẹhin ṣiṣe pipaṣẹ naa, bot naa yọkuro si iwiregbe (wo iwe afọwọkọ wolmypc)
DUP
Ri pẹlu
UPD 08.12.2016/XNUMX/XNUMX
ni Telegram, nkqwe, nwọn die-die yi pada "eefi" ti getUpdate. bayi ninu iwe afọwọkọ akọkọ o nilo lati ṣe atunṣe aiṣedeede ifiranṣẹ lati 2 si 1
iyipada
:local message [:pick $content ($startLoc + 2) $endLoc] ;
заменить на :
:local message [:pick $content ($startLoc + 1) $endLoc] ;
orisun: www.habr.com