Community discussions

MikroTik App
 
User avatar
harvey
Member Candidate
Member Candidate
Topic Author
Posts: 131
Joined: Thu Apr 05, 2012 8:16 pm

Inbound SMS run script pass number

Sat Jul 04, 2020 12:05 am

Hi,

I'm trying to write a script so when the Mikrotik receives an SMS it runs the script, gathers some information from the Mikrotik, and then sends an SMS back to the number that sent the request.

Is there any way to pass the phone number of the incoming message to the script so it can be used within the script to send the reply?

I'm also exploring the option of the script just searching the inbox from the last message, extracting the number and then using that but trying to see if there is a more direct solution.

I appreciate any feedback.

Thanks.
 
User avatar
mutluit
Forum Veteran
Forum Veteran
Posts: 821
Joined: Wed Mar 25, 2020 4:04 am

Re: Inbound SMS run script pass number

Sat Jul 04, 2020 3:32 pm

I'm trying to write a script so when the Mikrotik receives an SMS it runs the script, gathers some information from the Mikrotik, and then sends an SMS back to the number that sent the request.

Is there any way to pass the phone number of the incoming message to the script so it can be used within the script to send the reply?
Yes, is possible: see documentation and example https://wiki.mikrotik.com/wiki/Manual:T ... s#Examples
 
User avatar
harvey
Member Candidate
Member Candidate
Topic Author
Posts: 131
Joined: Thu Apr 05, 2012 8:16 pm

Re: Inbound SMS run script pass number

Sat Jul 04, 2020 11:34 pm

I'm trying to write a script so when the Mikrotik receives an SMS it runs the script, gathers some information from the Mikrotik, and then sends an SMS back to the number that sent the request.

Is there any way to pass the phone number of the incoming message to the script so it can be used within the script to send the reply?
Yes, is possible: see documentation and example https://wiki.mikrotik.com/wiki/Manual:T ... s#Examples
Hi. I can see that variables can be passed but would that require the sender including their own number in the message they send?

Would prefer it to just use the number of the sender.

If I’m missing something, please let me know.

Thanks
 
User avatar
mutluit
Forum Veteran
Forum Veteran
Posts: 821
Joined: Wed Mar 25, 2020 4:04 am

Re: Inbound SMS run script pass number

Sun Jul 05, 2020 12:45 am

See https://shop.duxtel.com.au/article_info ... cles_id=25
It says:
RouterOS lists such modems as serial port that appears in '/port print' listing. The following command can be issued to send SMS:

/tool sms send port=port dst-smsc=smsc message=message

Example:
/tool sms send port=usb3 "04XXXXXXX" message="HELLO ALL"

SMS received by the RouterBoard can be read by following command
/tool sms print
With "/tool sms print" you can see the received SMSes and pick fields from them etc...
Maybe the above examples are outdated. Better use the examples from the link below:

This all can of course also be automated. Here are some examples:
https://aacable.wordpress.com/2012/11/2 ... gsm-modem/
Here they give these examples:
/tool sms inbox print
/tool sms send port=usb3 phone-number=03xxxxx message="ALERT: Mikrotik Routerboard CCR restarted now." channel=0

For questions regarding scripting you should ask in the Scripting section viewforum.php?f=9

Another link with examples for SMS and email: http://phallaccmt.blogspot.com/2016/11/ ... ic-on.html
 
User avatar
sindy
Forum Guru
Forum Guru
Posts: 10788
Joined: Mon Dec 04, 2017 9:19 pm

Re: Inbound SMS run script pass number  [SOLVED]

Sun Jul 05, 2020 12:31 pm

I guess that the part you are missing is how to fetch the sender number from the received message.

So you need something like
:local senderNumber [tool sms inbox get [find message~"a regexp to check that it is the expected query message, not a spam"] number]

and then you can send the response, using $senderNumber:

/tool sms send phone-number=$senderNumber message="the response" ...

Having no 3G modem at hand, I cannot check it, so I hope it is sufficient like this. However, a script for actual deployment would have to use a foreach cycle to create a snapshot of the list of received messages and process it regardless whether some further SMS eventually arrive during the processing.
/tool sms {
  :foreach msg in=[inbox find] do={
    :if ([inbox get $msg message]~"regexp") do={
      :local senderNumber [inbox get $msg phone]
      send phone-number=$senderNumber message="it works"
    }
    inbox remove $msg
  }
}
/
 
User avatar
harvey
Member Candidate
Member Candidate
Topic Author
Posts: 131
Joined: Thu Apr 05, 2012 8:16 pm

Re: Inbound SMS run script pass number

Sun Jul 05, 2020 2:21 pm

I guess that the part you are missing is how to fetch the sender number from the received message.

So you need something like
:local senderNumber [tool sms inbox get [find message~"a regexp to check that it is the expected query message, not a spam"] number]

and then you can send the response, using $senderNumber:

/tool sms send phone-number=$senderNumber message="the response" ...

Having no 3G modem at hand, I cannot check it, so I hope it is sufficient like this. However, a script for actual deployment would have to use a foreach cycle to create a snapshot of the list of received messages and process it regardless whether some further SMS eventually arrive during the processing.
/tool sms {
  :foreach msg in=[inbox find] do={
    :if ([inbox get $msg message]~"regexp") do={
      :local senderNumber [inbox get $msg phone]
      send phone-number=$senderNumber message="it works"
    }
    inbox remove $msg
  }
}
/
Hi @sindy, thanks for the idea, I've had another thread open trying to look at it from another angle and that solution seems like it will be suitable enough for me. Thanks for your input.

Other thread: viewtopic.php?f=9&t=163211&p=804132#p804132
 
User avatar
sindy
Forum Guru
Forum Guru
Posts: 10788
Joined: Mon Dec 04, 2017 9:19 pm

Re: Inbound SMS run script pass number

Sun Jul 05, 2020 3:13 pm

Hi @sindy, thanks for the idea, I've had another thread open trying to look at it from another angle and that solution seems like it will be suitable enough for me. Thanks for your input.
Well, the only difference is that in that other thread you use the print as-value approach for the same thing, although the (slightly simpler) get works for /tool sms inbox. My perfectionist sould aches because for some types of data, only get works, for other ones, only print as-value does, and for yet another ones, both methods are available.
 
User avatar
harvey
Member Candidate
Member Candidate
Topic Author
Posts: 131
Joined: Thu Apr 05, 2012 8:16 pm

Re: Inbound SMS run script pass number

Sun Jul 05, 2020 6:32 pm

Hi @sindy, thanks for the idea, I've had another thread open trying to look at it from another angle and that solution seems like it will be suitable enough for me. Thanks for your input.
Well, the only difference is that in that other thread you use the print as-value approach for the same thing, although the (slightly simpler) get works for /tool sms inbox. My perfectionist sould aches because for some types of data, only get works, for other ones, only print as-value does, and for yet another ones, both methods are available.
Thanks I’ll keep it all in mind
 
AlexRodac
just joined
Posts: 23
Joined: Tue Jun 30, 2020 6:37 pm

Re: Inbound SMS run script pass number

Wed Aug 19, 2020 9:44 pm

Hi,

I'm trying to write a script so when the Mikrotik receives an SMS it runs the script, gathers some information from the Mikrotik, and then sends an SMS back to the number that sent the request.

Is there any way to pass the phone number of the incoming message to the script so it can be used within the script to send the reply?

I'm also exploring the option of the script just searching the inbox from the last message, extracting the number and then using that but trying to see if there is a more direct solution.

I appreciate any feedback.

Thanks.

Hello, How are you?
I have the same doubt about the configuration of the sms, I have an LtAP LTE, with three ports for sim cards, currently I only have a single sim working, which is slot two, but I have not been able to get the messages to be sent , I had already made a configuration but for him to send me the gps information and signal status, I could achieve this through some scripts for email and also with telegram, but I have not yet been able to with the sms, and I want him to do what @harvey says.
If you can help me, I would really appreciate it.

Who is online

Users browsing this forum: AhmedQadir, Aljack, pssara1 and 32 guests