Galluogi sgriptiau Mikrotik o bell o Telegram v 2.0

Gwyliau hwyr hapus i bawb. Mae'r thema hon yn well fersiwn o'r hyn ysgrifennais yn Γ΄l yn 2016 yma.

Yn gyffredinol, nid yw'r egwyddor o weithredu wedi newid, a'r unig wahaniaeth yw ei fod nawr yn gweithio ar unwaith yn ddi-oed.

Rydyn ni'n uwchlwytho'r sgript i Mikrotik, yn newid y BotID a ChatID i'n rhai ni ac yn creu amserlen ar ei gyfer. Gosodwch y paramedr β€œAmser Cychwyn” i gychwyn (Rhedwch y sgript wrth gychwyn.)
"Cyfwng": 00:00:00
Fel arall mae popeth fel yr oedd.

Telegram- v2

:delay 10
:global mtIdentity [/system identity get name];
:global botID "botXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXX" ;
:global myChatID "YYYYYY" ;
:local chatId 0;
:local messageId 0;


:local parse do={
  :local startLoc ([:find $content $variable -1] + [:len $variable] + 2);
  :local commaLoc ([:find $content "," $startLoc] - 1 + 1);
  :local braceLoc ([:find $content "}" $startLoc] - 1 + 1);
  :local endLoc $commaLoc;
  :local startSymbol [:pick $content $startLoc]
  :if ($braceLoc != 0 and ($commaLoc = 0 or $braceLoc < $commaLoc)) do={
    :set endLoc $braceLoc;
  };
  :if ($startSymbol = "{") do={
    :set endLoc ($braceLoc + 1);
  };
  :if ($quotas = true) do={
    :set startLoc ($startLoc + 1);
    :set endLoc ($endLoc - 1);
  }
  :if ($endLoc < $startLoc) do={
    :set endLoc ($startLoc + 1);
  };
  :local message [:pick $content $startLoc $endLoc]
  #:log info $message;
  :return $message;
}


:while ( true ) do={
  :do {
    #:log info "https://api.telegram.org/$botID/getUpdates?offset=$messageId&limit=1&allowed_updates=message&timeout=60";
    :tool fetch url=("https://api.telegram.org/$botID/getUpdates?offset=$messageId&limit=1&allowed_updates=message&timeout=60") dst-path="getUpdates";
    :local content [/file get [/file find name=getUpdates] contents] ;
    #:log info $content;
    :if ([:len $content] > 30) do={
      :set messageId ([$parse content=$content variable="update_id"] + 1)
      :local message [$parse content=$content variable="text" quotas=true]
      :local chat [$parse content=$content variable="chat"]
      :local chatId [$parse content=$chat variable="id"]      
      
      :if (($chatId = $myChatID) and ([/system script find name=$message] != "")) do={
        :system script run $message;
      } else={
        :tool fetch url=("https://api.telegram.org/$botID/sendmessage?chat_id=$chatId&text=$mtIdentity: Unknown command: $message") keep-result=no
      }
    }
  } on-error={}
};

Ar Γ΄l ychydig o flynyddoedd o ddefnydd, darganfuwyd nam: am ryw reswm anhysbys, mae Mikrotik gwan yn atal y sgript, ond ar rai mwy pwerus mae'n gweithio heb stopio.

At y dibenion hyn, fe wnes i daflu bagl y WatchDog ymlaen. Yma rydym yn newid enw'r sgript i'r un a nodir uchod. a gosodwch yr egwyl ailgychwyn i 5 munud. Bob 5 munud bydd ein "corff gwarchod" yn gwirio'r sgript ac os na fydd yn gweithio, bydd yn ei redeg.

GwylioT.me

:global scriptname "t.me"
:if ([:len [/system script job find script=$"scriptname"]] > 0) do={
:log info "$scriptname Already Running - killing old script before continuing"
:foreach counter in=[/system script job find script=$"scriptname"] do={
/system script job remove $counter
}
}
/system script run $scriptname

Wel, ar gyfer pwdin, cymerwyd y sgript o fforwm Mikrotik.
Yn anfon pynciau pwysig o'r log i'n cart.

Rydym yn ychwanegu'r sgript at yr amserlen ac yn nodi'r egwyl ailgychwyn bob 5 munud, yn newid BotID a ChatID i'n rhai ni.

Hysbysu-log

:global lastTime
:global output
:global mtIdentity [/system identity get name];
:global botID "botXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXX" ;
:global myChatID "YYYYYY" ;

:local LogGet [ :toarray [ /log find topics~"critical" || message~"login failure" || message~"[Ff]ailure" ] ] ;
:local LogtLineCount [ :len $LogGet ] ;
if ($LogtLineCount > 0) do={
   :local currentTime "$[ /log get [ :pick $LogGet ($LogtLineCount -1) ] time ]";
   :if ([:len $currentTime] = 10 ) do={
      :set currentTime [ :pick $currentTime 0 10 ];
   }
   :set output "$currentTime - $[/log get [ :pick $LogGet ($LogtLineCount-1) ] message ]";
   :if (([:len $lastTime] < 1) || (([:len $lastTime] > 0) && ($lastTime != $currentTime))) do={
      :set lastTime $currentTime ;
         :tool fetch url=("https://api.telegram.org/$botID/sendmessage?chat_id=$myChatID&text="$mtIdentity" :  $output") keep-result=no
   }
}

Cawn y canlyniad:

Galluogi sgriptiau Mikrotik o bell o Telegram v 2.0

Ffynhonnell: hab.com

Ychwanegu sylw