From 2d7f764c69636a90034e096f387ac58b69b97618 Mon Sep 17 00:00:00 2001 From: Anatoly Bubenkov Date: Thu, 3 Nov 2022 23:43:08 +0100 Subject: [PATCH] Allow sms forward actions --- .gitignore | 1 + doc/sms-forward.md | 30 ++++++++++++++++++++++++++++++ global-config | 8 ++++++++ sms-forward | 27 +++++++++++++++++++++++++++ 4 files changed, 66 insertions(+) diff --git a/.gitignore b/.gitignore index f29d4e84..31a48c88 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *~ *.patch *.html +.DS_Store diff --git a/doc/sms-forward.md b/doc/sms-forward.md index 09dd36ad..ca20119c 100644 --- a/doc/sms-forward.md +++ b/doc/sms-forward.md @@ -39,6 +39,36 @@ Also 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 actions in form where the key of the array is the regular expression to match the SMS text and the value is the action to execute: + + ``` + :global SmsForwardHooks { + { match="command"; + allowed-number="1234"; + command=":put "command executed"" }; + # add more here... + }; + ``` + + in this example the command `:put "command executed"` will be executed + when a SMS with text starting with `command` is received from number `1234`. + + Useful use case is to send an SMS to order an additional internet package when the current one is about to deplete, and the automatic SMS is sent from the provider with the text that 80%, 100% of the package are used. Then, an automated SMS can be sent to a provider back to order a new package automatically. + For example, for the [KPN](https://kpn.com/) provider, the SMS text is `NL2000 AAN` sent to number `1266` to order an additional 2GB package. + Then, the following configuration can be used: + + ``` + :global SmsForwardHooks { + { match="80%"; + allowed-number="KPN"; + command="/tool/sms/send lte1 phone-number=1266 message=\"NL2000 AAN\";" }; + }; + ``` + This reads as: when the SMS with text `80%` is received from number `KPN`, the SMS with text `NL2000 AAN` is sent to the provider. + If you use unlimited plan, this is exactly what you need to do to order a new package automatically. + See also -------- diff --git a/global-config b/global-config index 6ed2a458..e2717065 100644 --- a/global-config +++ b/global-config @@ -147,6 +147,14 @@ # add more here... }; +# Run commands on SMS forward action. +:global SmsForwardHooks { + # { match="magic string"; + # allowed-number="1234"; + # 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/sms-forward b/sms-forward index aa2e71c4..a14356f0 100644 --- a/sms-forward +++ b/sms-forward @@ -18,6 +18,12 @@ :global SendNotification2; :global SymbolForNotification; :global WaitFullyConnected; +:global ValidateSyntax; +:global SmsForwardHooks; + +:if ([ :typeof $SmsForwardHooks ] != "array") do={ + :global SmsForwardHooks ({}); +} $ScriptLock $0; @@ -43,6 +49,27 @@ $WaitFullyConnected; $LogPrintExit2 debug $0 ("Removing SMS, which started a script.") false; /tool/sms/inbox/remove $Sms; } else={ + :local processed false; + $LogPrintExit2 debug $0 ("Processing message:" . $SmsVal->"message" . " " . $Phone) false; + :foreach Hook in=$SmsForwardHooks do={ + $LogPrintExit2 debug $0 ("Checking SMS forward hook '" . $Hook . "' on message: '" . $SmsVal->"message" . "'") false; + :local Code ($Hook->"command"); + :local Regex ($Hook->"match"); + :local AllowedNumber ($Hook->"allowed-number"); + :if ($processed = false && $Phone~$AllowedNumber && ($SmsVal->"message")~$Regex) do={ + :if ([ $ValidateSyntax $Code ] = true) do={ + :log info ("Acting on SMS forward hook '" . $Regex . "': " . $Code); + :delay 1s; + [ :parse $Code ]; + :log info ("Finished SMS forward hook '" . $Regex . "': " . $Code); + } else={ + $LogPrintExit2 warning $0 ("The code for forward hook '" . $Regex . "': '" . $Code . "' failed syntax validation!") false; + } + # $LogPrintExit2 debug $0 ("Removing SMS, which started a forward script.") false; + # /tool/sms/inbox/remove $Sms; + :set processed true; + } + } :set Messages ($Messages . "\n\nOn " . $SmsVal->"timestamp" . \ " type " . $SmsVal->"type" . ":\n" . $SmsVal->"message"); :set Delete ($Delete, $Sms);