From ea09a18d3f05b20ff7f1ec25aea74f32d68ca5e0 Mon Sep 17 00:00:00 2001 From: Anatoly Bubenkov Date: Thu, 3 Nov 2022 23:43:08 +0100 Subject: sms-forward: support hooks --- doc/sms-forward.md | 28 ++++++++++++++++++++++++++++ global-config | 8 ++++++++ global-config.changes | 1 + global-functions | 2 +- sms-forward | 22 ++++++++++++++++++++++ 5 files changed, 60 insertions(+), 1 deletion(-) diff --git a/doc/sms-forward.md b/doc/sms-forward.md index 3d9974d..73248eb 100644 --- a/doc/sms-forward.md +++ b/doc/sms-forward.md @@ -35,11 +35,39 @@ You have to enable receiving of SMS: /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 [e-mail](mod/notification-email.md), [matrix](mod/notification-matrix.md) and/or [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 -------- diff --git a/global-config b/global-config index d4ed298..fded46f 100644 --- a/global-config +++ b/global-config @@ -147,6 +147,14 @@ # 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. :global GpsTrackUrl "https://example.com/index.php"; diff --git a/global-config.changes b/global-config.changes index 216cfc6..357dede 100644 --- a/global-config.changes +++ b/global-config.changes @@ -94,6 +94,7 @@ 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."; 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 diff --git a/global-functions b/global-functions index d25d21d..0c584b7 100644 --- a/global-functions +++ b/global-functions @@ -10,7 +10,7 @@ :local 0 "global-functions"; # expected configuration version -:global ExpectedConfigVersion 85; +:global ExpectedConfigVersion 86; # global variables not to be changed by user :global GlobalFunctionsReady false; diff --git a/sms-forward b/sms-forward index 8dce464..802da48 100644 --- a/sms-forward +++ b/sms-forward @@ -1,6 +1,7 @@ #!rsc by RouterOS # RouterOS script: sms-forward # Copyright (c) 2013-2023 Christian Hesse +# Anatoly Bubenkov # https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md # # forward SMS to e-mail @@ -11,12 +12,14 @@ :while ($GlobalFunctionsReady != true) do={ :delay 500ms; } :global Identity; +:global SmsForwardHooks; :global IfThenElse; :global LogPrintExit2; :global ScriptLock; :global SendNotification2; :global SymbolForNotification; +:global ValidateSyntax; :global WaitFullyConnected; $ScriptLock $0; @@ -45,6 +48,25 @@ $WaitFullyConnected; } else={ :set Messages ($Messages . "\n\nOn " . $SmsVal->"timestamp" . \ " 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); } } -- cgit v1.2.3-70-g09d2