Community discussions

MikroTik App
 
User avatar
abbio90
Member
Member
Topic Author
Posts: 470
Joined: Fri Aug 27, 2021 9:16 pm
Location: Oristano
Contact:

Script SMS forward sms

Fri Apr 12, 2024 1:16 pm

Does anyone have a script that reads SMS and forwards them via SMS?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 13099
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script SMS forward sms

Fri Apr 12, 2024 6:01 pm

use search function on forum, replace send email with send sms
 
User avatar
hsd75
just joined
Posts: 16
Joined: Sun Jul 29, 2018 11:54 pm

Re: Script SMS forward sms

Fri May 03, 2024 10:22 am

Yes I did, here my script.
# Script to forward SMS from GSM Modem Inbox to Email

#to avoid running the script twice at the same time

:if ([/system script job print count-only where script="FORWARDSMS"] < 2) do={
	:local number1 "+33xxxxxxxxx"
	:local smsPhone
	:local smsTimeStamp
	:local smsMessage
	:local smsContent
	:local toNumber
	:local port "usb1"
	:local channel "2"
	:global compareNum

# loop through all the messages in the inbox #
:foreach i in=[/tool sms inbox find] do={
	/log info "SMS $i"
	:set smsPhone [/tool sms inbox get $i phone]
	:set smsTimeStamp [/tool sms inbox get $i timestamp]
	:set smsTimeStamp [:pick $smsTimeStamp 0 19]
	:set smsMessage [/tool sms inbox get $i message]
  
  :if (($smsMessage != "") && ($smsPhone != $number1)) do={
     :if ($compareNum != $smsPhone ) do={
      :set smsContent "From:\n$smsPhone\n$smsTimeStamp\n_____________________________"
      /tool sms send port=$port phone-number=$number1 message=$smsContent channel=$channel
    }	
    /tool sms send port=$port phone-number=$number1 message=$smsMessage channel=$channel
    :set compareNum $smsPhone
    }	
    /tool sms inbox remove $i
    delay 1
    }
  }
 
optio
Forum Guru
Forum Guru
Posts: 1106
Joined: Mon Dec 26, 2022 2:57 pm

Re: Script SMS forward sms

Fri May 03, 2024 4:36 pm

Be aware that this could happen when sending SMS over ROS.
 
User avatar
hsd75
just joined
Posts: 16
Joined: Sun Jul 29, 2018 11:54 pm

Re: Script SMS forward sms

Sat May 04, 2024 7:36 am

No problem on my side for years. I use 2 different modem, 1 Mikrotik LTE and 1 USB.
My script deletes the SMS immediately after forwading, including the empty lines.
What is important is not to overload the received SMS.
 
User avatar
hsd75
just joined
Posts: 16
Joined: Sun Jul 29, 2018 11:54 pm

Re: Script SMS forward sms

Sat May 04, 2024 8:39 am

Here my almost entire script. I execute it every 5 seconds without impact on ressources or performance.

It can execute a command only when it receive an SMS from my mobile number
it can send an SMS whoever you want with the command "send mobiletosend# message" eg: "send +3306070809# Message Test"
and obviously forward to your mobile any SMS received except from your number.
# Script to forward SMS

:if ([/system script job print count-only where script="SMS2EXEC"] < 2) do={
  :local number1 "+33your mobile"
  :local smsPhone
  :local smsTimeStamp
  :local smsMessage
  :local smsContent
  :local toNumber
  :local port "usb1"; #in mycase
  :local channel "2"; #empty when you user builtin modem LTE
  :local URL "URL or IP of another mikrotik router via API"
  :local userapi "user authorized"
  :local passwordapi "password"
  :global compareNum
  
# loop through all the messages in the inbox #
:foreach i in=[/tool sms inbox find] do={
	/log info "SMS $i"
	:set smsPhone [/tool sms inbox get $i phone]
	:set smsTimeStamp [/tool sms inbox get $i timestamp]
  :set smsTimeStamp [:pick $smsTimeStamp 0 19]
	:set smsMessage [/tool sms inbox get $i message]

:if ($smsPhone = $number1) do={
  :if ($smsMessage ="?") do={
    :set smsContent "***Command***\n>ip : Get IP\n>ipv6 : Get IPv6\n>reboot : Reboot router\n>send num# msg : Send message"
    /tool sms send port=$port phone-number=$number1 message=$smsContent channel=$channel
  }
  :if ($smsMessage ="ipv6") do={
    :local faiaddress6 [/tool fetch http-method=get mode=https url="https://$URL/rest/ip/cloud" output=user as-value \
    user=$userapi password=$passwordapi http-header-field="Content-Type: application/json"]
  
    :set faiaddress6 [:pick ($faiaddress6->"data") 0 ([:len ($faiaddress6->"data")]-1)]
    :set faiaddress6 ([:pick $faiaddress6 ([:find $faiaddress6 "public-address-ipv6"]+22) ([:find $faiaddress6 "status"]-3)])
    :set smsContent "Router IPv6:\n$faiaddress6"
    /tool sms send port=$port phone-number=$number1 message=$smsContent channel=$channel
    /tool sms inbox remove $i
  }
  :if ($smsMessage ="ip") do={
    :local faiaddress [/tool fetch http-method=get mode=https url="https://$URL/rest/ip/cloud" output=user as-value \
    user=$userapi password=$passwordapi http-header-field="Content-Type: application/json"]
  
    :set faiaddress [:pick ($faiaddress->"data") 0 ([:len ($faiaddress->"data")]-1)]
    :set faiaddress ([:pick $faiaddress ([:find $faiaddress "public-address"]+17) ([:find $faiaddress "public-address-ipv6"]-3)])
    :set smsContent "Router IP:\n$faiaddress"
    /tool sms send port=$port phone-number=$number1 message=$smsContent channel=$channel
    /tool sms inbox remove $i
  }
  :if ($smsMessage ="reboot") do={
    :set smsContent "Reboot"
    /tool sms send port=$port phone-number=$number1 message=$smsContent channel=$channel
    /tool sms inbox remove $i
    delay 3
    /system/reboot
  }
  :if ( [:pick $smsMessage 0 4 ] ="send") do={
    :if ($smsMessage ~"#") do={ 
      :set toNumber ([:pick $smsMessage 4 ([:find $smsMessage "#"])])
      :set smsContent ([:pick $smsMessage ([:find $smsMessage "#"]+2 ) ([:len $smsMessage]+1)])
      /tool sms send port=$port phone-number=$toNumber message=$smsContent channel=$channel
      :set smsContent "Message sent to $toNumber"
      /tool sms send port=$port phone-number=$number1 message=$smsContent channel=$channel
  } else={
  :set smsContent "No message sent!"
  /tool sms send port=$port phone-number=$number1 message=$smsContent channel=$channel
  } }
}
  
  :if (($smsMessage != "") && ($smsPhone != $number1)) do={
    :if (($compareNum != $smsPhone ) || ( $compareNum = $smsPhone )) do={
      :set smsContent "From:\n$smsPhone\n$smsTimeStamp\n_____________________________"
      /tool sms send port=$port phone-number=$number1 message=$smsContent channel=$channel
    }
    /tool sms send port=$port phone-number=$number1 message=$smsMessage channel=$channel
    :set compareNum $smsPhone
    }
    /tool sms inbox remove $i
    delay 1
    }
}
 
optio
Forum Guru
Forum Guru
Posts: 1106
Joined: Mon Dec 26, 2022 2:57 pm

Re: Script SMS forward sms

Sun May 05, 2024 6:36 pm

No problem on my side for years. I use 2 different modem, 1 Mikrotik LTE and 1 USB.
My script deletes the SMS immediately after forwading, including the empty lines.
What is important is not to overload the received SMS.
I'm referring to sent messages (outbox), these messages are not deleted from SIM when sent from ROS (unless AT command is used). It depends on SIM capacity and number of messages sent, maybe limit is not reached in your case.
 
User avatar
abbio90
Member
Member
Topic Author
Posts: 470
Joined: Fri Aug 27, 2021 9:16 pm
Location: Oristano
Contact:

Re: Script SMS forward sms

Fri May 17, 2024 9:29 pm

I tried the script on 7 routers and it doesn't work. so I tried to adapt it but it returned "message too long" error. I tried cutting it with :pick. The first two pieces are cut correctly, while in the third part he puts the header back.

#inserire numero di telefono 1

:local number1 "+39123456789"

#----------------------------------------

#inserire nome azienda

:local azienda "Mario Rossi - Milano"

#----------------------------------------



#----------NO MODIFY THIS SECTION----------




#----------------------------------------


:local smsPhone
:local port "lte1"
:local scheduleName "SMS-Forward-Scheduler"
:local myRunTime 00:30:00

:if ([:len [/system scheduler find name="$scheduleName"]] = 0) do={
 /log error "[SMS Forward script 1.0] Alert : lo Scheduler non esiste."
 /system scheduler add name=$scheduleName interval=$myRunTime start-date=Jan/01/1970 start-time=startup on-event="system script run SMS-Forward-script"
 /log warning "[SMS Forward script 1.0] Alert : Scheduler creato ."
}


#--------------------------------

:local tgPrefix "Cliente: $azienda \r\nIdentita': $[/system identity get name]\r\n"
:local smsPhone; 
:local smsTimeStamp; 
:local smsMessage; 
:local smsContent;
:local i;
:log info "SMS Forward script started"; 
/tool sms set receive-enabled=yes; delay 2
:foreach i in=[/tool sms inbox find] do={ 
:set $smsPhone [/tool sms inbox get $i phone]; 
:set $smsTimeStamp [/tool sms inbox get $i timestamp]; 
:set $smsMessage [/tool sms inbox get $i message];
:set $smsContent "$tgPrefix\nMittente:$smsPhone\nData e Ora: $smsTimeStamp\nMessage: $smsMessage \r\n"; 
:log info $smsContent;
:local date ([system clock get date])
:local time ([/system clock get time])


#---------------------------------
#INSERIRE QUI IL TESTO DELLA NOTIFICA
:local notify "$smsContent"
#--------------------------------

:local textMessage [:pick $notify 0 150]
:local textMessage2 [:pick $notify 150 300]
:local textMessage3 [:pick $notify 300 450]


/tool sms send port=$port phone-number=$number1 message=$textMessage
:delay 2s
:if ($textMessage2!="") do={
	/tool sms send port=$port phone-number=$number1 message=$textMessage2
}
:delay 2s
:if ($textMessage3!="") do={
	/tool sms send port=$port phone-number=$number1 message=$textMessage3
}

/tool sms inbox remove $i; delay 10; } 
/tool sms set receive-enabled=no; 
:log info "SMS Forward script complete"; 
 
FlowerShopGuy
newbie
Posts: 27
Joined: Thu Sep 14, 2023 10:21 am

Re: Script SMS forward sms

Sun Nov 03, 2024 11:01 pm

Hey, does anyone have script that can SMS forward to Email?
 
mikear
Frequent Visitor
Frequent Visitor
Posts: 63
Joined: Wed Mar 23, 2022 8:08 pm
Location: Utrecht, Netherlands

Re: Script SMS forward sms

Sat Nov 16, 2024 3:13 pm

Just recently wrote as script that, triggered by a scheduled task, forwards all new incoming sms messages to my e-mail address. The script removes the oldest sms messages until a max number of stored messages is left in the inbox.

The script stores the number of messages in the sms inbox in a file (nrSMSmessages.txt). When the script is run this number is compared to the current number of messages in the sms inbox. If there are more (new) messages in the inbox the new messages will be forwarded by e-mail. If all mails are sent successfully, the oldest messages are removed. If any of the e-mails fails, all messages are retained.

The script can also be triggered by an sms-command (":cmd secret script forward-incoming-sms").

I do not pretend this is a perfect script, nor that is works for all, but it helps me keeping me updated about the messages the provider sends to the SIM card in my router (about 1500km away).

Hope it will be useful for any...

# RouterOS script to forward all incoming sms messages to a specified e-mail address
# Retain a maximum number of sms messages in the /tool sms inbox
# In case e-mail fails (at least for one messages) no messages will be removed
# Run this script as a scheduled task e.g. every 10 minutes
# Author: Mike Bosschaert
# Date: 20241116
# Version: 1.0
# Platform: RouterOS 7.15.3 (Mikrotik Chateau LTE12)

# setting variables
    :local eMailTo "youremail@address.com"
    :local eMailSubject "SMS message from Mikrotik router:"
    :local maxMessages 10
    :local filename "flash/nrSMSmessages.txt" ; #make sure it is on a persistent disk and does NOT contain a leading "/"!
    
# check if $filename exists, if not, create the file
    :if ([:len [/file find name=$filename]] = 0) do={ /file add type="file" name=$filename content=0; :log info "$filename created"} 
    
# get the list of messages and store it in an array ($inboxData) and the current number of messages in the sms inbox
    :local inboxData [/tool sms inbox print as-value]
    :local messageCount ([:len $inboxData] )
 
# get the number of previously retained sms messages
    :local previousNrSMS [/file get $filename contents]
# store the current number of sms messages in the file $filename
    /file set $filename contents=$messageCount

    :local messagesToSend ($messageCount - $previousNrSMS)
    :log info "SMS inbox previously stored nr of messages: $previousNrSMS, current nr of messages: $messageCount, still to send: $messagesToSend"

# interate through the list of sms messages starting at the first new message
     :local eMailSuccess true
     :local index ($previousNrSMS + 1)
     
     :while ($index < ($messageCount)) do={
        # retrieve the sender, message body and timestamp from the $inboxData array
        # as the sms inbox messages-array start with index "0" we need to substract 1 from the index value
        :local sender ([:pick $inboxData ($index-1)]->"phone")   
        :local message ([:pick $inboxData ($index-1)]->"message") 
        :local timestamp ([:pick $inboxData ($index-1)]->"timestamp") 
        
        # send the information by e-mail 
        :log info "sending SMS index $index to $eMailTo"
        /tool e-mail send to="$eMailTo" body="$message" subject="$eMailSubject $timestamp $sender"
        
        # check if the e-mail was sent succesfully, if not, make sure NO SMS messages are deleted
        :local ExitDoLoop false
        :while ($ExitDoLoop = false) do={
          :local EMailStatus [/tool e-mail get last-status];
          #:log info "checking sendstatus: $EMailStatus"
          :if ($EMailStatus != "failed" && $EMailStatus != "succeeded") do={ delay 1s; put "."} else={
            :set ExitDoLoop true;
            :if ($EMailStatus != "succeeded") do={
              :set eMailSuccess false
              :log info "sending e-mail not successful, sms messages will be retained and send during the next run of this script"
            }
          }
        }        
        
        # wait 10 seconds before checking for the next message to send
        :delay 10s
        :set index ($index +1)
      }

# check if there are too many stored sms messages, if so sequentially remove message with index 0 (the oldest messsage)
# ony run this loop if ALL messages have been sent succesfully ($eMailSuccess = true)
     :if ($eMailSuccess = true ) do={ 
        :log info "sms messages have been sent successfully, removing oldest messages if the max number to store is exceeded"
        :while ($maxMessages < ( $messageCount )) do={
          # remove the oldes message (index=0)
          /tool sms inbox remove 0
          # wait 20 seconds to enable RouterOS to re-index the sms-inbox 
          :delay 20s
          # get the new number of messages in the sms inbox, if it exceeds the max number to retain, delete the next message with index=0
          :local inboxData [/tool sms inbox print as-value]
          :set  messageCount ([:len $inboxData] )
         }
         # make sure the number of stored sms messages equals $messageCount after cleaning up
         /file set $filename contents=$messageCount
      } else={
        :log info "sending e-mails failed, reverting number of stored messages"
        /file set $filename contents=$previousNrSMS
     }

 
BillyVan
newbie
Posts: 43
Joined: Tue Sep 04, 2018 10:29 pm
Location: Greece

Re: Script SMS forward sms

Tue Nov 26, 2024 12:42 pm

@mikear
Perfect thank you
Can you add into script system identify info for the router?
I try to edit and add
DeviceName [/system identity get name]
without success
 
mikear
Frequent Visitor
Frequent Visitor
Posts: 63
Joined: Wed Mar 23, 2022 8:08 pm
Location: Utrecht, Netherlands

Re: Script SMS forward sms

Tue Nov 26, 2024 5:39 pm

@BillyVan nice to hear you can use the script
To ad the router identity your're nearly there. You could first store the system identity in a local variable like this:
:local deviceName [/system identity get name]
and then use the variable ( $deviceName) elswheere in the scirpt. For instance you could change the first part of the script, with the local variable definitions with:
# setting variables
    :local deviceName [/system identity get name]
    :local eMailTo "your@mailaddress.com"
    :local eMailSubject "Mikrotik router ($deviceName) SMS message:"
    :local maxMessages 10
    :local filename "flash/nrSMSmessages.txt"; #make sure it is on a persisntent disk and does NOT contain a leading "/"!
 
FlowerShopGuy
newbie
Posts: 27
Joined: Thu Sep 14, 2023 10:21 am

Re: Script SMS forward sms

Tue Nov 26, 2024 7:36 pm

@mikear

Thats what i'm looking for! Mega thanks!
Sadenley this wont woked for me, coz i need to reseve SMS imideniatly. I tried to add 1 in :local maxMessages but it's just skips the SMS with log message "sms messages have been sent successfully, removing oldest messages if the max number to store is exceeded" and removing the last one from inbox witout sending email.
 
mikear
Frequent Visitor
Frequent Visitor
Posts: 63
Joined: Wed Mar 23, 2022 8:08 pm
Location: Utrecht, Netherlands

Re: Script SMS forward sms

Tue Nov 26, 2024 9:21 pm

Not sure what's wrong at your side, and actually I have not tried maxMessages 1, but it should work.
And from our message I understand that you want to receive the e-mail 'immediately' when it is received. Unfortunately I have not found a way to get the script triggered by an incoming SMS message. You should arrange that with a schedule, in /script/schedule. I'm running it every 10 minutes, but you could also run it more frequenty, of course.

A assume you checked all the following:
- the file flash/nrSMSmessages.txt was created? If you set it not to save in flash or an attached USB-card, it will go away after a reboot
- that you are actualy receiving sms-es in your /tool/sms/inbox (sorry, this is obvious, and I assume you checked it ;) )
- your mail settings are correct and you can send a testmail from /tool/e-mail send

Please realize that the first time you run the script it is not aware of the number of SMS messages which were already there. It should, in that case, forward all earlier received sms messages. Also be aware that when you set to store the sms messages (you can find the location with /tool sms print, on the sim card they are available for anyone who has access to your sim-card. I store them in router, with the disadvantage that when you reboot they are lost.

Please if you cannot solve the problem, I'll try some different settings on my system and hopefully can solve it for you
 
FlowerShopGuy
newbie
Posts: 27
Joined: Thu Sep 14, 2023 10:21 am

Re: Script SMS forward sms

Tue Nov 26, 2024 9:54 pm

Not sure what's wrong at your side, and actually I have not tried maxMessages 1, but it should work.
And from our message I understand that you want to receive the e-mail 'immediately' when it is received. Unfortunately I have not found a way to get the script triggered by an incoming SMS message. You should arrange that with a schedule, in /script/schedule. I'm running it every 10 minutes, but you could also run it more frequenty, of course.

A assume you checked all the following:
- the file flash/nrSMSmessages.txt was created? If you set it not to save in flash or an attached USB-card, it will go away after a reboot
- that you are actualy receiving sms-es in your /tool/sms/inbox (sorry, this is obvious, and I assume you checked it ;) )
- your mail settings are correct and you can send a testmail from /tool/e-mail send

Please realize that the first time you run the script it is not aware of the number of SMS messages which were already there. It should, in that case, forward all earlier received sms messages. Also be aware that when you set to store the sms messages (you can find the location with /tool sms print, on the sim card they are available for anyone who has access to your sim-card. I store them in router, with the disadvantage that when you reboot they are lost.

Please if you cannot solve the problem, I'll try some different settings on my system and hopefully can solve it for you

Thanks for your help! My ROS is 7.15.2 btw
Current settings: maxMessages 1, Schedule script interval 00:01:00
Yes, SMS and Email is working, and nrSMSmessages.txt apears as well.
So, I Fugured out, It sends email only if there 3 SMS in inbox with these setting (If I send +2 SMS within one minute). So if in inbox 3 SMS it will sent emal for the last only, then it delete 1st and keep the 2nd sms. And if there only +1 sms (total 2 in inbox) in will just delete the first one without sending email and keep last sended sms.

And the logs
" SMS inbox previously stored nr of messages: 1, current nr of messages: 2, still to send: 1 "
" sms messages have been sent successfully, removing oldest messages if the max number to store is exceeded "
Last edited by FlowerShopGuy on Tue Nov 26, 2024 10:37 pm, edited 2 times in total.
 
mikear
Frequent Visitor
Frequent Visitor
Posts: 63
Joined: Wed Mar 23, 2022 8:08 pm
Location: Utrecht, Netherlands

Re: Script SMS forward sms

Tue Nov 26, 2024 10:22 pm

OK apparently some is wrong in the counting of messages, etc, Will dive into it, but please give me some time....
 
BillyVan
newbie
Posts: 43
Joined: Tue Sep 04, 2018 10:29 pm
Location: Greece

Re: Script SMS forward sms

Tue Nov 26, 2024 11:24 pm

OK apparently some is wrong in the counting of messages, etc, Will dive into it, but please give me some time....
Take your time.
By the way i try many many times with
:local deviceName [/system identity get name]
    :local eMailTo "your@mailaddress.com"
    :local eMailSubject "Mikrotik router ($deviceName) SMS message:"
    :local maxMessages 10
    :local filename "flash/nrSMSmessages.txt"; #make sure it is on a persisntent disk and does NOT contain a leading "/"!
in log i have message to send email but no email received

i try from scratch without device name and test again i receive only one mail
send many sms and waiting but everytime only one sms send it in email
i dont know why.

i have Wap4g-lte6 ros 7.15.3

Thank you for your time
 
mikear
Frequent Visitor
Frequent Visitor
Posts: 63
Joined: Wed Mar 23, 2022 8:08 pm
Location: Utrecht, Netherlands

Re: Script SMS forward sms

Wed Nov 27, 2024 2:27 pm

It appears that the order of incoming SMS messages in the /tool/sms/inbox is inconsistent. Not sure if it is a ROS-version problem, or a hardware problem. But to solve the problem I first need to setup a sorting routine and possibly have to rewrite the script. Will take some time...
 
mikear
Frequent Visitor
Frequent Visitor
Posts: 63
Joined: Wed Mar 23, 2022 8:08 pm
Location: Utrecht, Netherlands

Re: Script SMS forward sms

Sat Nov 30, 2024 9:07 pm

Please find an updated version of the script to forward SMS messages to an e-mail address. The sorting problem found on some platforms is solved, at least for the hardware and RoS versions I'm using. Please use it and adapt it to your taste.
# RouterOS script to forward all incoming sms messages to a specified e-mail address
# Retain a maximum number of sms messages in the /tool sms inbox
# In case e-mail fails (at least for one messages) no messages will be removed
# Run this script as a scheduled task e.g. every 10 minutes
# Author: Mike Bosschaert
# Date: 202411130
# Version: 2.0
# - solved problem with inbox not being indexed correctly on some RoS versions/MTik devices
# Platform: RouterOS 7.15.3, 7.16.1 7.16.2 (Mikrotik Chateau LTE12, Ltap_2HnD)

# setting variables
:local deviceName [/system identity get name as-string]
:local eMailTo "your@email.com"
:local eMailSubject "Mikrotik router ($deviceName) SMS message:"
:local maxMessages 10
:local filename "flash/nrSMSmessages.txt"; #make sure it is on a persisntent disk and does NOT contain a leading "/"!

# check if $filename exists, if not, create the file
:if ([:len [/file find name=$filename]] = 0) do={ /file add type="file" name=$filename content=0; :log info "$filename created"} 
    
# get the list of messages and store it in an array ($inboxData) and the current number of messages in the sms inbox
:local inboxData [/tool sms inbox print as-value]
:local messageCount ([:len $inboxData] )
:local sortkey "srt"; 

#### SORT THE INBOXDATA ARRAY, on some RoS/Mtik devices, the inbox is not sorted correctly by
#### default so the array is forcefully sorted here based on the timestamp
# create the sortkey based on the timestamp field of the SMS inbox messages
for msg from=0 to=([:len $inboxData]-1) do={
    :set ($inboxData->($msg)->($sortkey)) ([:pick ($inboxData->($msg)->"timestamp") 0 4 ] . [:pick ($inboxData->($msg)->"timestamp") 5 7 ] . [:pick ($inboxData->($msg)->"timestamp") 8 10 ] . [:pick ($inboxData->($msg)->"timestamp") 11 13 ] . [:pick ($inboxData->($msg)->"timestamp") 14 16] . [:pick ($inboxData->($msg)->"timestamp") 17 19] )
}

# sort the inboxData on the key srt
:local n [ :len $inboxData ];
:local swapped;
do {
    :set swapped false;
    :for i from=1 to=($n - 1) do={
        :if ( $inboxData->($i-1)->$sortkey  >  $inboxData->($i)->$sortkey) do={
          :set inboxData (([ :pick $inboxData 0 ($i-1) ]), ([ :pick $inboxData $i ($i+1)]) , ([ :pick $inboxData ($i-1) ($i)]), ([ :pick $inboxData ($i+1) ( [:len $inboxData] )]));
          :set swapped true;
        }
    }
    :set n ($n-1)
} while=($swapped);
#### END OF SORTING

# get the number of previously retained sms messages
:local previousNrSMS [/file get $filename contents]

# store the current number of sms messages in the file $filename
/file set $filename contents=$messageCount

:local messagesToSend ($messageCount - $previousNrSMS)
:log info "SMS inbox previously stored nr of messages: $previousNrSMS, current nr of messages: $messageCount, still to send: $messagesToSend"

# interate through the list of sms messages starting at the first NEW message
:local eMailSuccess
:local index ($previousNrSMS + 1)
:while ($index < ($messageCount +1 )) do={
  # retrieve the sender, message body and timestamp from the $inboxData array
  # as the sms inbox messages-array start with index "0" we need to substract 1 from the index value
  :local sender ([:pick $inboxData ($index-1)]->"phone")   
  :local message ([:pick $inboxData ($index-1)]->"message") 
  :local timestamp ([:pick $inboxData ($index-1)]->"timestamp") 
  
  # send the information by e-mail 
  :log info "Sending new SMS message with  index $index to $eMailTo"
  /tool e-mail send to="$eMailTo" body="$message" subject="$eMailSubject $timestamp $sender "
  
  # check if the e-mail was sent succesfully, if not, make sure NO SMS messages are deleted
  :local ExitDoLoop false
  :while ($ExitDoLoop = false) do={
    :local EMailStatus [/tool e-mail get last-status];
    # wait until the 'last-status' is updated
    :if ($EMailStatus != "failed" && $EMailStatus != "succeeded") do={ delay 1s; put "."} else={
      :set ExitDoLoop true;
      :if ($EMailStatus != "succeeded") do={
        # the last mail was not sent succesfully, quit further attempts and do not remove any messages from the SMS inbox
        :set eMailSuccess false
        :log info "Sending e-mail not successful, sms messages will be retained and send during the next run of this script"
      }  else={ 
        # the mail was succesfully, continue with the next message or the next step of the script
        :set eMailSuccess true 
      }
    }
  }        
  
  # wait some seconds before checking for the next message to send
  :delay 3s
  :set index ($index +1)
}

# check if there are too many stored sms messages, if so sequentially remove message with index 0 (the oldest messsage)
# ony run this loop if ALL messages have been sent succesfully ($eMailSuccess = true)
:if ($eMailSuccess = true || $eMailSuccess = false ) do={ 
    :if ($eMailSuccess = true) do={
      :log info "All sms messages have been sent successfully, removing oldest messages if the max number to store is exceeded"
      :if ($maxMessages <  $messageCount ) do={
          :for i from=0 to=($messageCount - $maxMessages - 1) do={
            :local removeID ($inboxData->($i)->".id")
            /tool sms inbox remove [ find where .id=($removeID) ]
          }
        }
        # make sure the number of stored sms messages equals $messageCount after cleaning up
        /file set $filename contents=[:len [/tool sms inbox print as-value] ]
      } else={      
        :log info "Sending mail not succesful, no messages have been deleted" 
        /file set $filename contents=$previousNrSMS
      }
} else={
  :log info "No new SMS messages found, nothing forwarded, no messages deleted"
  /file set $filename contents=$previousNrSMS
}
 
optio
Forum Guru
Forum Guru
Posts: 1106
Joined: Mon Dec 26, 2022 2:57 pm

Re: Script SMS forward sms

Sat Nov 30, 2024 9:16 pm

How it works with long messages (+1600 chars) using maxMessages=10 limit? :)
 
mikear
Frequent Visitor
Frequent Visitor
Posts: 63
Joined: Wed Mar 23, 2022 8:08 pm
Location: Utrecht, Netherlands

Re: Script SMS forward sms

Sat Nov 30, 2024 9:43 pm

As far as I know messages which exceed the size of an SMS (160) are split over various inbox entries. I guess a message of 1600 charactes will show as 10 messages which are handled as separate messages (sent in separate email messages). The script does not check if messages originally have the same origin. It may be possible to concatenate messages which have the same timestamp, but I'm not sure if the correct order is maintained by the RoS indexing or sorting algorithm.
 
optio
Forum Guru
Forum Guru
Posts: 1106
Joined: Mon Dec 26, 2022 2:57 pm

Re: Script SMS forward sms

Sat Nov 30, 2024 9:58 pm

Try with this script
sms-read.rsc
instead using /tool/sms/inbox for reading. It concatenates splitted long messages to single one and supports UCS-2 encoded messages with conversion to Utf8 encoding and email will have in body correct characters outside Ascii encoding, like cyrillic, diacritics...
You do not have the required permissions to view the files attached to this post.
Last edited by optio on Sun Dec 01, 2024 4:07 pm, edited 1 time in total.
 
FlowerShopGuy
newbie
Posts: 27
Joined: Thu Sep 14, 2023 10:21 am

Re: Script SMS forward sms

Sun Dec 01, 2024 12:14 am

Please find an updated version of the script to forward SMS messages to an e-mail address. The sorting problem found on some platforms is solved, at least for the hardware and RoS versions I'm using. Please use it and adapt it to your taste.
# RouterOS script to forward all incoming sms messages to a specified e-mail address
# Retain a maximum number of sms messages in the /tool sms inbox
# In case e-mail fails (at least for one messages) no messages will be removed
# Run this script as a scheduled task e.g. every 10 minutes
# Author: Mike Bosschaert
# Date: 202411130
# Version: 2.0
# - solved problem with inbox not being indexed correctly on some RoS versions/MTik devices
# Platform: RouterOS 7.15.3, 7.16.1 7.16.2 (Mikrotik Chateau LTE12, Ltap_2HnD)

# setting variables
:local deviceName [/system identity get name as-string]
:local eMailTo "your@email.com"
:local eMailSubject "Mikrotik router ($deviceName) SMS message:"
:local maxMessages 10
:local filename "flash/nrSMSmessages.txt"; #make sure it is on a persisntent disk and does NOT contain a leading "/"!

# check if $filename exists, if not, create the file
:if ([:len [/file find name=$filename]] = 0) do={ /file add type="file" name=$filename content=0; :log info "$filename created"} 
    
# get the list of messages and store it in an array ($inboxData) and the current number of messages in the sms inbox
:local inboxData [/tool sms inbox print as-value]
:local messageCount ([:len $inboxData] )
:local sortkey "srt"; 

#### SORT THE INBOXDATA ARRAY, on some RoS/Mtik devices, the inbox is not sorted correctly by
#### default so the array is forcefully sorted here based on the timestamp
# create the sortkey based on the timestamp field of the SMS inbox messages
for msg from=0 to=([:len $inboxData]-1) do={
    :set ($inboxData->($msg)->($sortkey)) ([:pick ($inboxData->($msg)->"timestamp") 0 4 ] . [:pick ($inboxData->($msg)->"timestamp") 5 7 ] . [:pick ($inboxData->($msg)->"timestamp") 8 10 ] . [:pick ($inboxData->($msg)->"timestamp") 11 13 ] . [:pick ($inboxData->($msg)->"timestamp") 14 16] . [:pick ($inboxData->($msg)->"timestamp") 17 19] )
}

# sort the inboxData on the key srt
:local n [ :len $inboxData ];
:local swapped;
do {
    :set swapped false;
    :for i from=1 to=($n - 1) do={
        :if ( $inboxData->($i-1)->$sortkey  >  $inboxData->($i)->$sortkey) do={
          :set inboxData (([ :pick $inboxData 0 ($i-1) ]), ([ :pick $inboxData $i ($i+1)]) , ([ :pick $inboxData ($i-1) ($i)]), ([ :pick $inboxData ($i+1) ( [:len $inboxData] )]));
          :set swapped true;
        }
    }
    :set n ($n-1)
} while=($swapped);
#### END OF SORTING

# get the number of previously retained sms messages
:local previousNrSMS [/file get $filename contents]

# store the current number of sms messages in the file $filename
/file set $filename contents=$messageCount

:local messagesToSend ($messageCount - $previousNrSMS)
:log info "SMS inbox previously stored nr of messages: $previousNrSMS, current nr of messages: $messageCount, still to send: $messagesToSend"

# interate through the list of sms messages starting at the first NEW message
:local eMailSuccess
:local index ($previousNrSMS + 1)
:while ($index < ($messageCount +1 )) do={
  # retrieve the sender, message body and timestamp from the $inboxData array
  # as the sms inbox messages-array start with index "0" we need to substract 1 from the index value
  :local sender ([:pick $inboxData ($index-1)]->"phone")   
  :local message ([:pick $inboxData ($index-1)]->"message") 
  :local timestamp ([:pick $inboxData ($index-1)]->"timestamp") 
  
  # send the information by e-mail 
  :log info "Sending new SMS message with  index $index to $eMailTo"
  /tool e-mail send to="$eMailTo" body="$message" subject="$eMailSubject $timestamp $sender "
  
  # check if the e-mail was sent succesfully, if not, make sure NO SMS messages are deleted
  :local ExitDoLoop false
  :while ($ExitDoLoop = false) do={
    :local EMailStatus [/tool e-mail get last-status];
    # wait until the 'last-status' is updated
    :if ($EMailStatus != "failed" && $EMailStatus != "succeeded") do={ delay 1s; put "."} else={
      :set ExitDoLoop true;
      :if ($EMailStatus != "succeeded") do={
        # the last mail was not sent succesfully, quit further attempts and do not remove any messages from the SMS inbox
        :set eMailSuccess false
        :log info "Sending e-mail not successful, sms messages will be retained and send during the next run of this script"
      }  else={ 
        # the mail was succesfully, continue with the next message or the next step of the script
        :set eMailSuccess true 
      }
    }
  }        
  
  # wait some seconds before checking for the next message to send
  :delay 3s
  :set index ($index +1)
}

# check if there are too many stored sms messages, if so sequentially remove message with index 0 (the oldest messsage)
# ony run this loop if ALL messages have been sent succesfully ($eMailSuccess = true)
:if ($eMailSuccess = true || $eMailSuccess = false ) do={ 
    :if ($eMailSuccess = true) do={
      :log info "All sms messages have been sent successfully, removing oldest messages if the max number to store is exceeded"
      :if ($maxMessages <  $messageCount ) do={
          :for i from=0 to=($messageCount - $maxMessages - 1) do={
            :local removeID ($inboxData->($i)->".id")
            /tool sms inbox remove [ find where .id=($removeID) ]
          }
        }
        # make sure the number of stored sms messages equals $messageCount after cleaning up
        /file set $filename contents=[:len [/tool sms inbox print as-value] ]
      } else={      
        :log info "Sending mail not succesful, no messages have been deleted" 
        /file set $filename contents=$previousNrSMS
      }
} else={
  :log info "No new SMS messages found, nothing forwarded, no messages deleted"
  /file set $filename contents=$previousNrSMS
}

It works now. Thank you so much! :D ROS 7.15.2 (Hap AC3)
 
mikear
Frequent Visitor
Frequent Visitor
Posts: 63
Joined: Wed Mar 23, 2022 8:08 pm
Location: Utrecht, Netherlands

Re: Script SMS forward sms

Sun Dec 01, 2024 1:48 pm

Try with this script sms-read.rsc instead using /tool/sms/inbox for reading. It concatenates splitted long messages to single one and supports UCS-2 encoded messages with conversion to Utf8 encoding and email will have in body correct characters outside Ascii encoding, like cyrillic, diacritics...
Thanks for sharing, and indeed an interesting script. For my purpose this goes way to far but studying the script was very useful to extend my understanding of the MT scripting language. :D
 
BillyVan
newbie
Posts: 43
Joined: Tue Sep 04, 2018 10:29 pm
Location: Greece

Re: Script SMS forward sms

Sun Dec 01, 2024 10:13 pm

New version works very well

wap R ac lte6 with 7.15.3

Thanks
 
FlowerShopGuy
newbie
Posts: 27
Joined: Thu Sep 14, 2023 10:21 am

Re: Script SMS forward sms

Mon Dec 09, 2024 12:11 am

Please find an updated version of the script to forward SMS messages to an e-mail address. The sorting problem found on some platforms is solved, at least for the hardware and RoS versions I'm using. Please use it and adapt it to your taste.
# RouterOS script to forward all incoming sms messages to a specified e-mail address
# Retain a maximum number of sms messages in the /tool sms inbox
# In case e-mail fails (at least for one messages) no messages will be removed
# Run this script as a scheduled task e.g. every 10 minutes
# Author: Mike Bosschaert
# Date: 202411130
# Version: 2.0
# - solved problem with inbox not being indexed correctly on some RoS versions/MTik devices
# Platform: RouterOS 7.15.3, 7.16.1 7.16.2 (Mikrotik Chateau LTE12, Ltap_2HnD)

# setting variables
:local deviceName [/system identity get name as-string]
:local eMailTo "your@email.com"
:local eMailSubject "Mikrotik router ($deviceName) SMS message:"
:local maxMessages 10
:local filename "flash/nrSMSmessages.txt"; #make sure it is on a persisntent disk and does NOT contain a leading "/"!

# check if $filename exists, if not, create the file
:if ([:len [/file find name=$filename]] = 0) do={ /file add type="file" name=$filename content=0; :log info "$filename created"} 
    
# get the list of messages and store it in an array ($inboxData) and the current number of messages in the sms inbox
:local inboxData [/tool sms inbox print as-value]
:local messageCount ([:len $inboxData] )
:local sortkey "srt"; 

#### SORT THE INBOXDATA ARRAY, on some RoS/Mtik devices, the inbox is not sorted correctly by
#### default so the array is forcefully sorted here based on the timestamp
# create the sortkey based on the timestamp field of the SMS inbox messages
for msg from=0 to=([:len $inboxData]-1) do={
    :set ($inboxData->($msg)->($sortkey)) ([:pick ($inboxData->($msg)->"timestamp") 0 4 ] . [:pick ($inboxData->($msg)->"timestamp") 5 7 ] . [:pick ($inboxData->($msg)->"timestamp") 8 10 ] . [:pick ($inboxData->($msg)->"timestamp") 11 13 ] . [:pick ($inboxData->($msg)->"timestamp") 14 16] . [:pick ($inboxData->($msg)->"timestamp") 17 19] )
}

# sort the inboxData on the key srt
:local n [ :len $inboxData ];
:local swapped;
do {
    :set swapped false;
    :for i from=1 to=($n - 1) do={
        :if ( $inboxData->($i-1)->$sortkey  >  $inboxData->($i)->$sortkey) do={
          :set inboxData (([ :pick $inboxData 0 ($i-1) ]), ([ :pick $inboxData $i ($i+1)]) , ([ :pick $inboxData ($i-1) ($i)]), ([ :pick $inboxData ($i+1) ( [:len $inboxData] )]));
          :set swapped true;
        }
    }
    :set n ($n-1)
} while=($swapped);
#### END OF SORTING

# get the number of previously retained sms messages
:local previousNrSMS [/file get $filename contents]

# store the current number of sms messages in the file $filename
/file set $filename contents=$messageCount

:local messagesToSend ($messageCount - $previousNrSMS)
:log info "SMS inbox previously stored nr of messages: $previousNrSMS, current nr of messages: $messageCount, still to send: $messagesToSend"

# interate through the list of sms messages starting at the first NEW message
:local eMailSuccess
:local index ($previousNrSMS + 1)
:while ($index < ($messageCount +1 )) do={
  # retrieve the sender, message body and timestamp from the $inboxData array
  # as the sms inbox messages-array start with index "0" we need to substract 1 from the index value
  :local sender ([:pick $inboxData ($index-1)]->"phone")   
  :local message ([:pick $inboxData ($index-1)]->"message") 
  :local timestamp ([:pick $inboxData ($index-1)]->"timestamp") 
  
  # send the information by e-mail 
  :log info "Sending new SMS message with  index $index to $eMailTo"
  /tool e-mail send to="$eMailTo" body="$message" subject="$eMailSubject $timestamp $sender "
  
  # check if the e-mail was sent succesfully, if not, make sure NO SMS messages are deleted
  :local ExitDoLoop false
  :while ($ExitDoLoop = false) do={
    :local EMailStatus [/tool e-mail get last-status];
    # wait until the 'last-status' is updated
    :if ($EMailStatus != "failed" && $EMailStatus != "succeeded") do={ delay 1s; put "."} else={
      :set ExitDoLoop true;
      :if ($EMailStatus != "succeeded") do={
        # the last mail was not sent succesfully, quit further attempts and do not remove any messages from the SMS inbox
        :set eMailSuccess false
        :log info "Sending e-mail not successful, sms messages will be retained and send during the next run of this script"
      }  else={ 
        # the mail was succesfully, continue with the next message or the next step of the script
        :set eMailSuccess true 
      }
    }
  }        
  
  # wait some seconds before checking for the next message to send
  :delay 3s
  :set index ($index +1)
}

# check if there are too many stored sms messages, if so sequentially remove message with index 0 (the oldest messsage)
# ony run this loop if ALL messages have been sent succesfully ($eMailSuccess = true)
:if ($eMailSuccess = true || $eMailSuccess = false ) do={ 
    :if ($eMailSuccess = true) do={
      :log info "All sms messages have been sent successfully, removing oldest messages if the max number to store is exceeded"
      :if ($maxMessages <  $messageCount ) do={
          :for i from=0 to=($messageCount - $maxMessages - 1) do={
            :local removeID ($inboxData->($i)->".id")
            /tool sms inbox remove [ find where .id=($removeID) ]
          }
        }
        # make sure the number of stored sms messages equals $messageCount after cleaning up
        /file set $filename contents=[:len [/tool sms inbox print as-value] ]
      } else={      
        :log info "Sending mail not succesful, no messages have been deleted" 
        /file set $filename contents=$previousNrSMS
      }
} else={
  :log info "No new SMS messages found, nothing forwarded, no messages deleted"
  /file set $filename contents=$previousNrSMS
}

I sometimes get messages like this

"????????? ???????! ?????? ?????? ????????? ????? ????????? ?? 09.12"

Its not the scrist issue, just brobably because of russian language (mikrotik does not understand Cyrillic), is there there some chanсe to make readble with russian langage?
there some other SMS script that can also decode russian Cyrillic, but it obsolete with ROS7, and not works anymore.
https://forummikrotik.ru/viewtopic.php?t=11655
 
optio
Forum Guru
Forum Guru
Posts: 1106
Joined: Mon Dec 26, 2022 2:57 pm

Re: Script SMS forward sms

Mon Dec 09, 2024 6:46 pm

Look here
 
FlowerShopGuy
newbie
Posts: 27
Joined: Thu Sep 14, 2023 10:21 am

Re: Script SMS forward sms

Thu Dec 12, 2024 12:50 am

Look here

I don't have such the skill to something to do with it! ¯\_(ツ)_/¯
Only can paste my email in some ready script. lol
 
optio
Forum Guru
Forum Guru
Posts: 1106
Joined: Mon Dec 26, 2022 2:57 pm

Re: Script SMS forward sms

Thu Dec 12, 2024 9:40 pm

Here is script which I'm using if you want to use it:
:local toEmail "<EMAIL_ADDRESS>"
:local commandReg "^:cmd .*"
:local statusInProgress "in-progress"
:local statusSucceeded "succeeded"
:local emailSendTimeoutSec 30

:global smsToEmailRunning
:local smsInbox [/tool/sms/inbox find]

:if ([:len $smsInbox] > 0 && !$smsToEmailRunning) do={
  :set smsToEmailRunning true
  /system/script/run sms-read
  :global smsRead
  :local smsList [$smsRead]
  :set smsRead
  :local allowedNumbers [/tool/sms get allowed-number]
  :local allowedSize [:len $allowedNumbers]
  :local deviceName [/system/identity get name]
  :local subjectPrefix "[ROUTER $deviceName] SMS message from: "

  :foreach sms in=$smsList do={
    :local sender ($sms->"sender")
    :local message ($sms->"message")
    :local send true

    :if (($allowedSize > 0) && ($message~$commandReg)) do={
      :local i 0
      :while ($send && ($i < $allowedSize)) do={
        :if ($sender = [:pick $allowedNumbers $i]) do={
          :set send false
        } else={ :set i ($i + 1) }
      }
    }

    :if ($send) do={
      /tool/e-mail/send to=$toEmail subject="$subjectPrefix$sender" body=$message
      :delay 1s
      :local sec 0
      :while ($sec < $emailSendTimeoutSec && [/tool/e-mail get last-status] = $statusInProgress) do={
        :delay 1s
        :set sec ($sec + 1)
      }

      :if ([/tool/e-mail get last-status] != $statusSucceeded) do={
        :local msg
        :if ($sec >= $emailSendTimeoutSec) do={
          :set msg "SMS to email: Aborting, sending email to '$toEmail' timed out, check e-mail topic logs or rise 'emailSendTimeoutSec' variable value if this is due to slow Internet connection"
        } else={
          :set msg "SMS to email: Aborting, error sending email to '$toEmail', check e-mail topic logs"
        }
        :log error $msg
        :set smsToEmailRunning
        :error $msg
      }
    } else={
      :log debug "SMS to email: Skipping SMS command '$message'"
    }
  }

  /tool/sms/inbox
  :foreach sms in=$smsInbox do={ remove $sms }
  :set smsToEmailRunning
}
This script uses sms-read script from my post #22. Key difference from @mikear's script is that this script doesn't keep sent SMS messages, if they are sucessfuly sent to email it will be deleted from SMS inbox. Also it doesn't send SMS commands from allowed numbers. I'm running this script from scheduler in 1min interval from startup.

If you want to use this, keep in mind that sms-read is a large script and I have issue adding/editing it using Winbox over Wine, it's crashing Winbox and I had to use WebFig for adding it. Maybe when running Winbox from WIndows there is no such issue, idk.
 
maigonis
Member Candidate
Member Candidate
Posts: 238
Joined: Sat Jul 20, 2019 8:16 pm

Re: Script SMS forward sms

Mon Mar 03, 2025 8:04 pm

Here is script which I'm using if you want to use it:
:local toEmail "<EMAIL_ADDRESS>"
:local commandReg "^:cmd .*"
:local statusInProgress "in-progress"
:local statusSucceeded "succeeded"
:local emailSendTimeoutSec 30

:global smsToEmailRunning
:local smsInbox [/tool/sms/inbox find]

:if ([:len $smsInbox] > 0 && !$smsToEmailRunning) do={
  :set smsToEmailRunning true
  /system/script/run sms-read
  :global smsRead
  :local smsList [$smsRead]
  :set smsRead
  :local allowedNumbers [/tool/sms get allowed-number]
  :local allowedSize [:len $allowedNumbers]
  :local deviceName [/system/identity get name]
  :local subjectPrefix "[ROUTER $deviceName] SMS message from: "

  :foreach sms in=$smsList do={
    :local sender ($sms->"sender")
    :local message ($sms->"message")
    :local send true

    :if (($allowedSize > 0) && ($message~$commandReg)) do={
      :local i 0
      :while ($send && ($i < $allowedSize)) do={
        :if ($sender = [:pick $allowedNumbers $i]) do={
          :set send false
        } else={ :set i ($i + 1) }
      }
    }

    :if ($send) do={
      /tool/e-mail/send to=$toEmail subject="$subjectPrefix$sender" body=$message
      :delay 1s
      :local sec 0
      :while ($sec < $emailSendTimeoutSec && [/tool/e-mail get last-status] = $statusInProgress) do={
        :delay 1s
        :set sec ($sec + 1)
      }

      :if ([/tool/e-mail get last-status] != $statusSucceeded) do={
        :local msg
        :if ($sec >= $emailSendTimeoutSec) do={
          :set msg "SMS to email: Aborting, sending email to '$toEmail' timed out, check e-mail topic logs or rise 'emailSendTimeoutSec' variable value if this is due to slow Internet connection"
        } else={
          :set msg "SMS to email: Aborting, error sending email to '$toEmail', check e-mail topic logs"
        }
        :log error $msg
        :set smsToEmailRunning
        :error $msg
      }
    } else={
      :log debug "SMS to email: Skipping SMS command '$message'"
    }
  }

  /tool/sms/inbox
  :foreach sms in=$smsInbox do={ remove $sms }
  :set smsToEmailRunning
}
This script uses sms-read script from my post #22. Key difference from @mikear's script is that this script doesn't keep sent SMS messages, if they are sucessfuly sent to email it will be deleted from SMS inbox. Also it doesn't send SMS commands from allowed numbers. I'm running this script from scheduler in 1min interval from startup.

If you want to use this, keep in mind that sms-read is a large script and I have issue adding/editing it using Winbox over Wine, it's crashing Winbox and I had to use WebFig for adding it. Maybe when running Winbox from WIndows there is no such issue, idk.
Tnx all - works like a charm.
 
RomadiagKomi
just joined
Posts: 1
Joined: Thu Apr 24, 2025 9:09 am

Re: Script SMS forward sms

Thu Apr 24, 2025 10:08 pm

Optio, thank you very much! Here is a script based on yours, it sends SMS to telegram-chat. Maybe someone will come in handy.
# SMS_to_telegram by https://forum.mikrotik.com/viewtopic.php?t=206689#p1112154

/tool sms set receive-enabled=no;

/tool sms set receive-enabled=yes;
:delay 5s
:local telegramToken "your telegram token"
:local chatId "your telegram chat"
:local commandReg "^:cmd .*"
:local smsInbox [/tool/sms/inbox find]

:global smsToTelegramRunning

:if ([:len $smsInbox] > 0 && !$smsToTelegramRunning) do={
  :set smsToTelegramRunning true
  /system/script/run sms-read
  :global smsRead
  :local smsList [$smsRead]
  :set smsRead
  :local allowedNumbers [/tool/sms get allowed-number]
  :local allowedSize [:len $allowedNumbers]
  :local deviceName [/system/identity get name]
  :local subjectPrefix "[ROUTER $deviceName] SMS message from: "

  :foreach sms in=$smsList do={
    :local sender ($sms->"sender")
    :local message ($sms->"message")
    :local send true

    :if (($allowedSize > 0) && ($message~$commandReg)) do={
      :local i 0
      :while ($send && ($i < $allowedSize)) do={
        :if ($sender = [:pick $allowedNumbers $i]) do={
          :set send false
        } else={ :set i ($i + 1) }
      }
    }

    :if ($send) do={
      :local telegramUrl "https://api.telegram.org/bot$telegramToken/sendMessage"
      :local params "chat_id=$chatId&text=$subjectPrefix$sender%0A$message"
      :local response [/tool/fetch url="$telegramUrl?$params" as-value output=user]

      :if ($response->"status" != "finished") do={
        :log error "Error sending SMS to Telegram: $response->\"status\""
      } else={
        :log info "SMS sent to Telegram: $subjectPrefix$sender"
      }
    } else={
      :log debug "SMS to Telegram: Skipping SMS command '$message'"
    }
  }

  /tool/sms/inbox
  :foreach sms in=$smsInbox do={ remove $sms }
  :set smsToTelegramRunning
}
/tool sms set receive-enabled=no;
I am not a programmer and there may be some shortcomings, but it works on lhgg lte6 kit routeros 7.18.2
You also need a script sms-read
Last edited by RomadiagKomi on Thu Apr 24, 2025 10:35 pm, edited 3 times in total.