sms-forward: support hooks

This commit is contained in:
Anatoly Bubenkov 2022-11-03 23:43:08 +01:00 committed by Christian Hesse
parent 88b34cfb39
commit ea09a18d3f
5 changed files with 60 additions and 1 deletions

View File

@ -35,11 +35,39 @@ You have to enable receiving of SMS:
/tool/sms/set receive-enabled=yes; /tool/sms/set receive-enabled=yes;
The configuration goes to `global-config-overlay`, this is the only parameter:
* `SmsForwardHooks`: an array with pre-defined hooks, where each hook consists
of `match` (which is matched against the received message), `allowed-number`
(which is matched against the sending phone number or name) and `command`.
For `match` and `allowed-number` regular expressions are supported.
Notification settings are required for Notification settings are required for
[e-mail](mod/notification-email.md), [e-mail](mod/notification-email.md),
[matrix](mod/notification-matrix.md) and/or [matrix](mod/notification-matrix.md) and/or
[telegram](mod/notification-telegram.md). [telegram](mod/notification-telegram.md).
Tips & Tricks
-------------
### Order new volume
Most broadband providers include a volume limit for their data plans. The
hook functionality can be used to order new volume automatically.
Let's assume an imaginary provider **ABC** sends a message when the available
volume is about to deplete. The message is sent from `ABC` and the text
contains the string `80%`. New volume can be ordered by sending a SMS back to
the phone number `1234` with the text `data-plan`.
:global SmsForwardHooks {
{ match="80%";
allowed-number="ABC";
command="/tool/sms/send lte1 phone-number=1234 message=\"data-plan\";" };
};
Adjust the values to your own needs.
See also See also
-------- --------

View File

@ -147,6 +147,14 @@
# add more here... # add more here...
}; };
# Run commands by hooking into SMS forward.
:global SmsForwardHooks {
{ match="magic string";
allowed-number="12345678";
command="/system/script/run ..." };
# add more here...
};
# This is the address used to send gps data to. # This is the address used to send gps data to.
:global GpsTrackUrl "https://example.com/index.php"; :global GpsTrackUrl "https://example.com/index.php";

View File

@ -94,6 +94,7 @@
83="Introduced new setting to disable news and change notifications, dropped version from configuration."; 83="Introduced new setting to disable news and change notifications, dropped version from configuration.";
84="Support for e-mail notifications moved to a module. It is installed automatically if required."; 84="Support for e-mail notifications moved to a module. It is installed automatically if required.";
85="Dropped 'netwatch-syslog', filtering in firewall is advised."; 85="Dropped 'netwatch-syslog', filtering in firewall is advised.";
86="Added support for hooks in 'sms-forward'. This now provides similar functionality to 'sms-action', but is more flexible.";
}; };
# Migration steps to be applied on script updates # Migration steps to be applied on script updates

View File

@ -10,7 +10,7 @@
:local 0 "global-functions"; :local 0 "global-functions";
# expected configuration version # expected configuration version
:global ExpectedConfigVersion 85; :global ExpectedConfigVersion 86;
# global variables not to be changed by user # global variables not to be changed by user
:global GlobalFunctionsReady false; :global GlobalFunctionsReady false;

View File

@ -1,6 +1,7 @@
#!rsc by RouterOS #!rsc by RouterOS
# RouterOS script: sms-forward # RouterOS script: sms-forward
# Copyright (c) 2013-2023 Christian Hesse <mail@eworm.de> # Copyright (c) 2013-2023 Christian Hesse <mail@eworm.de>
# Anatoly Bubenkov <bubenkoff@gmail.com>
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md # https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
# #
# forward SMS to e-mail # forward SMS to e-mail
@ -11,12 +12,14 @@
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; } :while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
:global Identity; :global Identity;
:global SmsForwardHooks;
:global IfThenElse; :global IfThenElse;
:global LogPrintExit2; :global LogPrintExit2;
:global ScriptLock; :global ScriptLock;
:global SendNotification2; :global SendNotification2;
:global SymbolForNotification; :global SymbolForNotification;
:global ValidateSyntax;
:global WaitFullyConnected; :global WaitFullyConnected;
$ScriptLock $0; $ScriptLock $0;
@ -45,6 +48,25 @@ $WaitFullyConnected;
} else={ } else={
:set Messages ($Messages . "\n\nOn " . $SmsVal->"timestamp" . \ :set Messages ($Messages . "\n\nOn " . $SmsVal->"timestamp" . \
" type " . $SmsVal->"type" . ":\n" . $SmsVal->"message"); " type " . $SmsVal->"type" . ":\n" . $SmsVal->"message");
:foreach Hook in=$SmsForwardHooks do={
:if ($Phone~($Hook->"allowed-number") && ($SmsVal->"message")~($Hook->"match")) do={
:if ([ $ValidateSyntax ($Hook->"command") ] = true) do={
$LogPrintExit2 info $0 ("Running hook '" . $Hook->"match" . "': " . \
$Hook->"command") false;
:do {
[ :parse ($Hook->"command") ];
:set Messages ($Messages . "\n\nRan hook '" . $Hook->"match" . "':\n" . \
$Hook->"command");
} on-error={
$LogPrintExit2 warning $0 ("The code for hook '" . $Hook->"match" . \
"' failed to run!") false;
}
} else={
$LogPrintExit2 warning $0 ("The code for hook '" . $Hook->"match" . \
"' failed syntax validation!") false;
}
}
}
:set Delete ($Delete, $Sms); :set Delete ($Delete, $Sms);
} }
} }