S
S
seel23042021-07-07 13:25:25
network hardware
seel2304, 2021-07-07 13:25:25

How to remake a script so that it does not execute a script, but a command?

Good afternoon everyone. I don’t remember where, I found quite a working script that parses a chat in a telegram and if it sees a command there, it executes a script with that name.

spoiler
:delay 10
:global mtIdentity [/system identity get name];
:global botID "botxxxxxx" ;
:global myChatID "-xxxxxxxx" ;
: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={}
};

Please help me rework it in such a way that it would be possible to write in the chat not the name of the script, but the command right away and it would be directly executed in the terminal, and in response from the bot I would receive (ideally) output from the terminal, or at least , message "%Microtname, external ip%: command %message% was executed".

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
poisons, 2021-07-07
@poisons

Looked at the script diagonally

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

Here is the area you are looking for. Those. the output should look something like this
: if ($chatId = $myChatID) do={
:$message;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question