mirror of
https://github.com/nymurbd/MikroTik-scripts.git
synced 2025-08-02 09:14:30 +02:00
Merge branch 'deserialize' into next
This commit is contained in:
commit
e107247c16
6 changed files with 16 additions and 78 deletions
|
@ -4,7 +4,7 @@ Send notifications via Matrix
|
|||
[](https://github.com/eworm-de/routeros-scripts/stargazers)
|
||||
[](https://github.com/eworm-de/routeros-scripts/network)
|
||||
[](https://github.com/eworm-de/routeros-scripts/watchers)
|
||||
[](https://mikrotik.com/download/changelogs/)
|
||||
[](https://mikrotik.com/download/changelogs/)
|
||||
[](https://t.me/routeros_scripts)
|
||||
[](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=A4ZXBD6YS2W8J)
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ Send notifications via Telegram
|
|||
[](https://github.com/eworm-de/routeros-scripts/stargazers)
|
||||
[](https://github.com/eworm-de/routeros-scripts/network)
|
||||
[](https://github.com/eworm-de/routeros-scripts/watchers)
|
||||
[](https://mikrotik.com/download/changelogs/)
|
||||
[](https://mikrotik.com/download/changelogs/)
|
||||
[](https://t.me/routeros_scripts)
|
||||
[](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=A4ZXBD6YS2W8J)
|
||||
|
||||
|
|
|
@ -56,7 +56,6 @@
|
|||
:global MkDir;
|
||||
:global NotificationFunctions;
|
||||
:global ParseDate;
|
||||
:global ParseJson;
|
||||
:global ParseKeyValueStore;
|
||||
:global PrettyPrint;
|
||||
:global RandomDelay;
|
||||
|
@ -830,66 +829,6 @@
|
|||
"day"=[ :tonum [ :pick $Date 8 10 ] ] });
|
||||
}
|
||||
|
||||
# parse JSON into array
|
||||
# Warning: This is not a complete parser!
|
||||
:set ParseJson do={
|
||||
:local Input [ :tostr $1 ];
|
||||
|
||||
:local InLen;
|
||||
:local Return ({});
|
||||
:local Skip 0;
|
||||
|
||||
:if ([ :pick $Input 0 ] = "{") do={
|
||||
:set Input [ :pick $Input 1 ([ :len $Input ] - 1) ];
|
||||
}
|
||||
:set Input [ :toarray $Input ];
|
||||
:set InLen [ :len $Input ];
|
||||
|
||||
:for I from=0 to=$InLen do={
|
||||
:if ($Skip > 0 || $Input->$I = "\n" || $Input->$I = "\r\n") do={
|
||||
:if ($Skip > 0) do={
|
||||
:set $Skip ($Skip - 1);
|
||||
}
|
||||
} else={
|
||||
:local Done false;
|
||||
:local Key ($Input->$I);
|
||||
:local Val1 ($Input->($I + 1));
|
||||
:local Val2 ($Input->($I + 2));
|
||||
:if ($Val1 = ":") do={
|
||||
:set Skip 2;
|
||||
:set ($Return->$Key) $Val2;
|
||||
:set Done true;
|
||||
}
|
||||
:if ($Done = false && $Val1 = ":[") do={
|
||||
:local Last false;
|
||||
:set Skip 1;
|
||||
:set ($Return->$Key) ({});
|
||||
:do {
|
||||
:set Skip ($Skip + 1);
|
||||
:local ValX ($Input->($I + $Skip));
|
||||
:if ([ :pick $ValX ([ :len $ValX ] - 1) ] = "]") do={
|
||||
:set Last true;
|
||||
:set ValX [ :pick $ValX 0 ([ :len $ValX ] - 1) ];
|
||||
}
|
||||
:set ($Return->$Key) (($Return->$Key), $ValX);
|
||||
} while=($Last = false && $I + $Skip < $InLen);
|
||||
:set Done true;
|
||||
}
|
||||
:if ($Done = false && $Val1 = ":[]") do={
|
||||
:set Skip 1;
|
||||
:set ($Return->$Key) ({});
|
||||
:set Done true;
|
||||
}
|
||||
:if ($Done = false) do={
|
||||
:set Skip 1;
|
||||
:set ($Return->$Key) [ :pick $Val1 1 [ :len $Val1 ] ];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
:return $Return;
|
||||
}
|
||||
|
||||
# parse key value store
|
||||
:set ParseKeyValueStore do={
|
||||
:local Source $1;
|
||||
|
|
|
@ -4,6 +4,8 @@
|
|||
# Christian Hesse <mail@eworm.de>
|
||||
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
|
||||
#
|
||||
# requires RouterOS, version=7.13
|
||||
#
|
||||
# send notifications via Matrix
|
||||
# https://git.eworm.de/cgit/routeros-scripts/about/doc/mod/notification-matrix.md
|
||||
|
||||
|
@ -183,9 +185,7 @@
|
|||
:local User [ :tostr $1 ];
|
||||
:local Pass [ :tostr $2 ];
|
||||
|
||||
:global CharacterReplace;
|
||||
:global LogPrint;
|
||||
:global ParseJson;
|
||||
|
||||
:global MatrixAccessToken;
|
||||
:global MatrixHomeServer;
|
||||
|
@ -194,7 +194,7 @@
|
|||
:do {
|
||||
:local Data ([ /tool/fetch check-certificate=yes-without-crl output=user \
|
||||
("https://" . $Domain . "/.well-known/matrix/client") as-value ]->"data");
|
||||
:set MatrixHomeServer ([ $ParseJson ([ $ParseJson [ $CharacterReplace $Data " " "" ] ]->"m.homeserver") ]->"base_url");
|
||||
:set MatrixHomeServer ([ :deserialize from=json value=$Data ]->"m.homeserver"->"base_url");
|
||||
$LogPrint debug $0 ("Home server is: " . $MatrixHomeServer);
|
||||
} on-error={
|
||||
$LogPrint error $0 ("Failed getting home server!");
|
||||
|
@ -209,7 +209,7 @@
|
|||
:local Data ([ /tool/fetch check-certificate=yes-without-crl output=user \
|
||||
http-method=post http-data=("{\"type\":\"m.login.password\", \"user\":\"" . $User . "\", \"password\":\"" . $Pass . "\"}") \
|
||||
("https://" . $MatrixHomeServer . "/_matrix/client/r0/login") as-value ]->"data");
|
||||
:set MatrixAccessToken ([ $ParseJson $Data ]->"access_token");
|
||||
:set MatrixAccessToken ([ :deserialize from=json value=$Data ]->"access_token");
|
||||
$LogPrint debug $0 ("Access token is: " . $MatrixAccessToken);
|
||||
} on-error={
|
||||
$LogPrint error $0 ("Failed logging in (and getting access token)!");
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
# Copyright (c) 2013-2024 Christian Hesse <mail@eworm.de>
|
||||
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
|
||||
#
|
||||
# requires RouterOS, version=7.13
|
||||
#
|
||||
# send notifications via Telegram
|
||||
# https://git.eworm.de/cgit/routeros-scripts/about/doc/mod/notification-telegram.md
|
||||
|
||||
|
@ -19,7 +21,6 @@
|
|||
|
||||
:global IsFullyConnected;
|
||||
:global LogPrint;
|
||||
:global ParseJson;
|
||||
:global UrlEncode;
|
||||
|
||||
:if ([ $IsFullyConnected ] = false) do={
|
||||
|
@ -43,7 +44,7 @@
|
|||
"&reply_to_message_id=" . ($Message->"replyto") . "&disable_web_page_preview=true" . \
|
||||
"&parse_mode=MarkdownV2&text=" . [ $UrlEncode ($Message->"text") ]) as-value ]->"data");
|
||||
:set ($TelegramQueue->$Id);
|
||||
:set ($TelegramMessageIDs->([ $ParseJson ([ $ParseJson $Data ]->"result") ]->"message_id")) 1;
|
||||
:set ($TelegramMessageIDs->[ :tostr ([ :deserialize from=json value=$Data ]->"result"->"message_id") ]) 1;
|
||||
} on-error={
|
||||
$LogPrint debug $0 ("Sending queued Telegram message failed.");
|
||||
:set AllDone false;
|
||||
|
@ -75,7 +76,6 @@
|
|||
:global EitherOr;
|
||||
:global IfThenElse;
|
||||
:global LogPrint;
|
||||
:global ParseJson;
|
||||
:global SymbolForNotification;
|
||||
:global UrlEncode;
|
||||
|
||||
|
@ -144,7 +144,7 @@
|
|||
http-data=("chat_id=" . $ChatId . "&disable_notification=" . ($Notification->"silent") . \
|
||||
"&reply_to_message_id=" . ($Notification->"replyto") . "&disable_web_page_preview=true" . \
|
||||
"&parse_mode=MarkdownV2&text=" . [ $UrlEncode $Text ]) as-value ]->"data");
|
||||
:set ($TelegramMessageIDs->([ $ParseJson ([ $ParseJson $Data ]->"result") ]->"message_id")) 1;
|
||||
:set ($TelegramMessageIDs->[ :tostr ([ :deserialize from=json value=$Data ]->"result"->"message_id") ]) 1;
|
||||
} on-error={
|
||||
$LogPrint info $0 ("Failed sending telegram notification! Queuing...");
|
||||
|
||||
|
|
|
@ -34,7 +34,6 @@
|
|||
:global MAX;
|
||||
:global MIN;
|
||||
:global MkDir;
|
||||
:global ParseJson;
|
||||
:global RandomDelay;
|
||||
:global ScriptLock;
|
||||
:global SendTelegram2;
|
||||
|
@ -86,18 +85,18 @@
|
|||
:error false;
|
||||
}
|
||||
|
||||
:local JSON [ :deserialize from=json value=$Data ];
|
||||
:local UpdateID 0;
|
||||
:local Uptime [ /system/resource/get uptime ];
|
||||
:foreach UpdateArray in=([ $ParseJson $Data ]->"result") do={
|
||||
:local Update [ $ParseJson $UpdateArray ];
|
||||
:foreach Update in=($JSON->"result") do={
|
||||
:set UpdateID ($Update->"update_id");
|
||||
:local Message [ $ParseJson ($Update->"message") ];
|
||||
:local Message ($Update->"message");
|
||||
:local IsReply [ :len ($Message->"reply_to_message") ];
|
||||
:local IsMyReply ($TelegramMessageIDs->([ $ParseJson ($Message->"reply_to_message") ]->"message_id"));
|
||||
:local IsMyReply ($TelegramMessageIDs->[ :tostr ($Message->"reply_to_message"->"message_id") ]);
|
||||
:if (($IsMyReply = 1 || $TelegramChatOffset->0 > 0 || $Uptime > 5m) && $UpdateID >= $TelegramChatOffset->2) do={
|
||||
:local Trusted false;
|
||||
:local Chat [ $ParseJson ($Message->"chat") ];
|
||||
:local From [ $ParseJson ($Message->"from") ];
|
||||
:local Chat ($Message->"chat");
|
||||
:local From ($Message->"from");
|
||||
|
||||
:foreach IdsTrusted in=($TelegramChatId, $TelegramChatIdsTrusted) do={
|
||||
:if ($From->"id" = $IdsTrusted || $From->"username" = $IdsTrusted) do={
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue