Ua koi ʻo Alexander Koryukin iaʻu i kēia hoʻokō
A he ʻōlelo ma kekahi o nā hui VK na Kirill Kazakov:
ʻAe, ʻaʻole paʻa loa. Makemake au e kākau i kahi bot telegram e ʻae wale i nā kauoha hoʻāla mai kaʻu moʻokāki.
Ua hoʻoholo wau e kākau i kahi bot.
No laila, ʻo ka mea mua e hana ai, ʻo ia ka hana ʻana i kahi bot ma ke kelepona.
- Loaʻa iā mākou ma ka ʻimi ʻana i kahi moʻolelo i kapa ʻia ʻo @botfather
- Kaomi ma ka pihi hoʻomaka ma lalo o ka pale
- A laila kākau mākou iā ia i ke kauoha / newbot
A laila pane mākou i nā nīnau maʻalahi 2:
- ʻO ka nīnau mua ka inoa o ka bot e hana ʻia. MyMikrotikROuter
- ʻO ka nīnau ʻelua ka inoa kapakapa o ka bot i hana ʻia (pono e pau me ka bot) MikrotikROuter_bot
I ka pane ʻana, e loaʻa iā mākou ka hōʻailona o kā mākou bot, i koʻu hihia ʻo ia:
E hoʻohana i kēia hōʻailona e komo i ka HTTP API: 265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4
A laila, pono ʻoe e ʻimi i kā mākou bot i ka ʻimi ma ka inoa @MikrotikROuter_bot a kaomi i ke pihi hoʻomaka.
Ma hope o kēlā, pono ʻoe e wehe i ka polokalamu kele pūnaewele a komo i kēia laina:
https://api.telegram.org/botXXXXXXXXXXXXXXXXXX/getUpdates
Aia ʻo XXXXXXXXXXXXXXXXXX ka hōʻailona o kāu bot.
E wehe ʻia kahi ʻaoʻao e like me kēia:
ʻIke mākou i kēia kikokikona ma luna ona:
"kamaʻilio": {"id":631290,
No laila, loaʻa iā mākou nā ʻike āpau e pono ai no ke kākau ʻana i nā palapala no Mikrotik, ʻo ia hoʻi:
Hōʻailona Bot: 265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4
Kāleka ID ma kahi e kākau ai: 631290
No ka nānā ʻana, hiki iā mākou ke hele ma o ka polokalamu kele pūnaewele:
https://api.telegram.org/bot265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4/sendmessage?chat_id=631290&text=test
Pono e loaʻa ka hopena:
No ko mākou ʻoluʻolu, e hoʻohui koke mākou i nā kauoha no ka bot:
Ke ʻimi nei i kahi moʻokāki me kahi inoa @botfather
A laila kākau mākou iā ia i kauoha / setcommands
- E nīnau ʻo ia i ka bot
Kākau mākou:
@MikrotikROuter_bot
Hoʻohui i nā kauoha:
- helloworld< — Ho'āʻo memo ma ke kamaʻilio 1
- kāna hana-Test Message ma ke kamaʻilio 2
- wolmypc-e ala i kaʻu PC
I kēia manawa inā ʻoe e paʻi "/" i ke kamaʻilio, pono ʻoe e loaʻa:
I kēia manawa e neʻe mākou i MikroTik.
Loaʻa iā RouterOS kahi mea hoʻohana console no ke kope ʻana i nā faila ma o ftp a i ʻole http / https, ua kapa ʻia ka mea pono e kiʻi, ʻo ia kā mākou e hoʻohana ai.
Wehe maʻi a komo:
/tool fetch url="https://api.telegram.org/bot265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4/sendmessage?chat_id=631290&text=test " keep-result=no
E ʻoluʻolu e hoʻomaopopo pono ʻo MikroTik ""e pakele i ka hōʻailona"?' ma ka URL.
Pono e loaʻa ka hopena:
I kēia manawa e neʻe kākou i nā ʻatikala:
aoanime
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}
kana hana ana
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"
Mai poina e kuhikuhi i ka mac pololei a me ka inoa interface, a me ka bot-token a me ka chat_id.
I kēia manawa e wehewehe iki au i kā lākou hana:
Hoʻouna ka palapala "helloworld" i kahi leka: "Aloha, honua!" i kā mākou kamaʻilio me ka bot.
Hoʻouna ka "itsworking" script i kahi leka: "Test OK, it's Working!" i kā mākou kamaʻilio me ka bot.
No ka hōʻike ʻana kēia mau palapala.
Ua hoʻohui au i ka palapala "wolmypc" ma ke ʻano he mea hiki ke hoʻokō.
Ma ka hoʻokō ʻana i ka palapala, e kākau ka bot i ka "wol OK" i ke kamaʻilio.
ʻOiaʻiʻo, hiki iā ʻoe ke holo i kēlā me kēia palapala.
Hana i kahi hana:
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
Nānā HeluheluʻAʻole maopopo i ke kumu, akā mai ka palapala hana ʻaʻole ia e hōʻike i ka ʻikepili honua, hoʻohui i ka palapala i ka wā e hoʻomaka ai ka ʻōnaehana.
Hoʻomaka 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;
}
Pehea hana i keia hana
Hoʻoiho i kā mākou mau memo "getUpdates" i kēlā me kēia 30 kekona, a laila e ʻimi e ʻike update_id (helu memo) a kikokikona (ko mākou mau hui) a kamaʻilio_id . Ma ka maʻamau, hōʻike ʻo getUpdates mai 1 a 100 mau memo, no ka maʻalahi, ma hope o ka heluhelu ʻana i ke kauoha, holoi mākou i ka memo. Wahi a Telegram api e heluhelu i kahi memo e pono ai ʻoe i ka helu memo + 1
/getUpdates?offset=update_id + 1
Hoʻāʻo ʻia nā mea āpau ma Mikrotik rb915 RouterOS 6.37.1
Inā hoʻouna ʻoe i nā kauoha he nui i ka manawa hoʻokahi, e hoʻokō ʻia lākou a pau me ka wā o 30 kekona.
PS Mahalo nui iā Kirill Kazakov no ka manaʻo a me koʻu hoa Alexander no ke kōkua ʻana i nā palapala.
kūmole
upd:
03:11:16
Nā palapala i hoʻomaikaʻi ʻia:
Hoʻohui ʻia ka māka no ka chat_id
Ke nānā nei i kahi naʻaupō, inā kākau kekahi i kā mākou bot, e pane ʻo ia iā ia: "ʻAʻole hiki iaʻu ke kamaʻilio pū me ʻoe. ", e pane like iā mākou inā ʻaʻole ia e ʻike i ke kauoha.
Ma hope o ka hoʻokō ʻana i ke kauoha, kāpae ka bot i ke kamaʻilio (e nānā i ka wolmypc script)
MEA HOU
Loaʻa me
UPD 08.12.2016/XNUMX/XNUMX
ma Telegram, ua hoʻololi iki lākou i ka "exhaust" o getUpdate. i kēia manawa i ka palapala nui pono ʻoe e hoʻoponopono i ka memo offset mai 2 a 1
ke hoʻololi
:local message [:pick $content ($startLoc + 2) $endLoc] ;
заменить на :
:local message [:pick $content ($startLoc + 1) $endLoc] ;
Source: www.habr.com