Galluogi sgriptiau Mikrotik o Telegram o bell

Gwthiodd Alexander Koryukin fi i'r gweithrediad hwn GeXoGeN gyda'i gyhoeddiad "Trowch eich cyfrifiadur ymlaen o bell am ddim, heb SMS a heb gymylau, gan ddefnyddio Mikrotik".

A sylw yn un o'r grwpiau VK gan Kirill Kazakov:

Ydy, nid yw'n ddiogel o gwbl. Byddai'n well gennyf ysgrifennu bot telegram sydd ond yn derbyn gorchmynion actifadu o'm cyfrif.

Penderfynais ysgrifennu bot o'r fath.

Felly, y peth cyntaf y mae angen i chi ei wneud yw creu bot mewn telegram.

  • Rydyn ni'n dod o hyd i gyfrif gyda'r enw @botfather wrth chwilio
  • Cliciwch ar y botwm Start ar waelod y sgrin
  • Yna rydyn ni'n ysgrifennu'r gorchymyn / newbot iddo

Yna rydym yn ateb 2 gwestiwn syml:

  • Y cwestiwn cyntaf yw enw'r bot sy'n cael ei greu MyMikrotikRouter
  • Yr ail gwestiwn yw llysenw'r bot sy'n cael ei greu (rhaid gorffen gyda bot) MikrotikROuter_bot

Mewn ymateb, byddwn yn derbyn tocyn ein bot, yn fy achos i yw:

Defnyddiwch y tocyn hwn i gyrchu'r API HTTP: 265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4

Galluogi sgriptiau Mikrotik o Telegram o bell
Yna, mae angen ichi ddod o hyd i'n bot yn y chwiliad yn ôl enw @MikrotikROuter_bot a chliciwch ar y botwm Start.

Ar ôl hyn, mae angen ichi agor eich porwr a nodi'r llinell ganlynol:

 https://api.telegram.org/botXXXXXXXXXXXXXXXXXX/getUpdates

Lle XXXXXXXXXXXXXXX yw tocyn eich bot.

Bydd tudalen tebyg i’r canlynol yn agor:

Galluogi sgriptiau Mikrotik o Telegram o bell

Rydym yn dod o hyd i'r testun canlynol arno:

"sgwrs":{ "id":631290,

Felly, mae gennym yr holl wybodaeth angenrheidiol i ysgrifennu sgriptiau ar gyfer Mikrotik, sef:

Tocyn bot: 265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4

ID sgwrs lle dylai ysgrifennu: 631290

I wirio, gallwn fynd trwy'r porwr:

https://api.telegram.org/bot265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4/sendmessage?chat_id=631290&text=test

Dylech gael y canlyniad:

Galluogi sgriptiau Mikrotik o Telegram o bell

Er hwylustod i ni, gadewch i ni ychwanegu gorchmynion ar gyfer y bot ar unwaith:

Dod o hyd i gyfrif gyda'r enw @bottad
Yna rydyn ni'n ysgrifennu gorchymyn iddo / setiau

  • Ef a ofyn pa bot

Rydym yn ysgrifennu:
@MikrotikROuter_bot

Ychwanegu gorchmynion:

  • heloworld < — Profi'r neges ar sgwrs 1
  • ei waith — Prawf Neges ar sgwrs 2
  • wolmypc - deffro fy PC

Nawr os teipiwch “/” mewn sgwrs, dylech gael:

Galluogi sgriptiau Mikrotik o Telegram o bell

Nawr, gadewch i ni symud ymlaen i MikroTik.

Mae gan RouterOS gyfleustodau consol ar gyfer copïo ffeiliau trwy ftp neu http/https, gelwir y cyfleustodau'n fetch, sef yr hyn y byddwn yn ei ddefnyddio.

Ar agor terfynell a mynd i mewn:

/tool fetch url="https://api.telegram.org/bot265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4/sendmessage?chat_id=631290&text=test " keep-result=no

Sylwch fod angen "" i ddianc rhag yr arwydd "?" yn yr URL.

Dylech gael y canlyniad:

Galluogi sgriptiau Mikrotik o Telegram o bell

Nawr, gadewch i ni symud ymlaen at y sgriptiau:

Helo Byd

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}

ei waith

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}

gwolmypc

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"

Peidiwch ag anghofio nodi'r enw mac a rhyngwyneb cywir, yn ogystal â bot-token a chat_id.

Nawr byddaf yn esbonio ychydig beth maen nhw'n ei wneud:

Mae'r sgript "helloworld" yn anfon y neges: "Helo, fyd!" i'n sgwrs gyda'r bot.
Mae'r sgript “itworking” yn anfon y neges: “Profwch yn iawn, mae'n Gweithio!” i'n sgwrs gyda'r bot.
Mae'r sgriptiau hyn ar gyfer arddangos y gwaith.
Ychwanegais y sgript “wolmypc” fel un o'r gweithrediadau posibl.
Pan fydd y sgript wedi'i chwblhau, bydd y bot yn ysgrifennu “Wol OK” i'r sgwrs.
Mewn gwirionedd, gallwch chi redeg unrhyw sgript o gwbl.

Creu tasg:

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

Golygfa ddarllenadwyNid yw'n glir pam, ond nid yw'n cyhoeddi data byd-eang o'r sgript waith, ychwanegwyd y sgript pan fydd y system yn cychwyn.
Telegram-cychwyn

: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; 
} 

Sut mae hwn

Rydyn ni'n lawrlwytho ein negeseuon “getUpdates” bob 30 eiliad, yna'n eu dosrannu i ddarganfod diweddaru_id (rhif neges) a testun (ein timau) a sgwrs_id . Yn ddiofyn, mae getUpdates yn dangos o 1 i 100 o negeseuon; er hwylustod, ar ôl darllen y gorchymyn, mae'r neges yn cael ei dileu. mae'r ap Telegram yn dweud bod angen rhif y neges + 1 arnoch i ddarllen neges

/getUpdates?offset=update_id + 1

Profwyd popeth ar Mikrotik rb915 RouterOS 6.37.1
Os byddwch yn anfon llawer o orchmynion ar unwaith, byddant i gyd yn cael eu gweithredu fesul un gydag egwyl o 30 eiliad.

P.S. Diolch yn fawr i Kirill Kazakov am y syniad ac i fy ffrind Alexander am ei gymorth gyda'r sgriptiau.

cyfeiriadau

habrahabr.ru/post/313794
1spla.ru/index.php/blog/telegram_bot_for_mikrotik
core.telegram.org/bots/api
wiki.mikrotik.com/wiki/Manual:Sgript

diweddaru:

03:11:16

Gwell sgriptiau:

Ychwanegwyd siec am chat_id
Gwiriad ffôl, os bydd rhywun yn ysgrifennu at ein bot, bydd yn eu hateb: “Ni allaf siarad â chi.” “, yn ymateb i ni yn yr un modd os nad yw'n cydnabod y gorchymyn.
Ar ôl gweithredu'r gorchymyn, mae'r bot yn dad-danysgrifio i'r sgwrs (gweler sgript wolmypc)

DIWEDDARIAD

Wedi dod o hyd gyda 7Stuntman7 nad yw ffeil gyda mwy na ~14 o negeseuon bellach yn cael ei phrosesu gan y gorchymyn darganfod (cyfyngiadau Mikrotik). Felly, yn y dyfodol byddaf yn ail-wneud y sgript yn lu, diolch 7Stuntman7 am hyn, ni wyddwn am lu.

DIWEDDAR 08.12.2016

Mae'n debyg bod Telegram wedi newid y “gwacáu” getUpdate ychydig. Nawr yn y brif sgript mae angen i chi gywiro'r gwrthbwyso neges o 2 i 1

newidiadau

:local message [:pick $content ($startLoc + 2) $endLoc] ;

заменить на :

:local message [:pick $content ($startLoc + 1) $endLoc] ;

Ffynhonnell: hab.com