Community discussions

MikroTik App
 
User avatar
dasiu
Trainer
Trainer
Topic Author
Posts: 231
Joined: Fri Jan 30, 2009 11:41 am
Location: Reading, UK
Contact:

PUSHOVER - ready MikroTik script to send messages

Fri Jun 29, 2018 12:19 am

I've just written a script (function) to send the Pushover messages directly from MikroTik, not using e-mail but direct API (faster). Just run once on bootup to "install" the functions:
:global urlEncode do={
  :local output ""
  :local input [:toarray $1]
  :if ([:len $input] > 0) do={
    :local input1 [:tostr [:pick $input 0]]
    :local conversion {" "="%20";"!"="%21";"\""="%22";"#"="%23";"\$"="%24";"%"="%25";"&"="%26";"'"="%27";"-"="%2D";"("="%28";")"="%29";"*"="%2A";"+"="%2B";","="%2C";"/"="%2F";":"="%3A";";"="%3B";"<"="%3C";">"="%3E";"="="%3D";"?"="%3F";"@"="%40";"["="%5B";"]"="%5D";"{"="%7B";"}"="%7D"};
    :for i from=0 to=([:len $input1] - 1) do={ 
      :local char [:pick $input1 $i]
      :if ([:typeof ($conversion->[:tostr $char])]!="nothing") do={:set $char ($conversion->[:tostr $char]);}
      :set output ($output . $char)
    }
    :set output [:tostr $output]
    :set output [:toarray $output]
  }
  :return $output
}

:global pushover do={
:global urlEncode;
  :if ([:typeof $message]!="nothing") do={
    :local api "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    :local user "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    :local urlmessage [$urlEncode $message];
    :local string "token=$api&user=$user&message=$urlmessage";
    :if ([:typeof $title]!="nothing") do={:set $string ($string . "&title=$[$urlEncode $title]");}
    :if ([:typeof $priority]!="nothing") do={:set $string ($string . "&priority=$priority");}
    :if ([:typeof $sound]!="nothing") do={:set $string ($string . "&sound=$sound");}
    /tool fetch mode=https url="https://api.pushover.net/1/messages.json" http-method=post http-data="$string";
  }
}
Of course, in "api" and "user" variables I have my keys from Pushover (you need to create application in the Pushover panel). Then I can just do:
$pushover message="This is a simple Pushover message."
Or more advanced:
:local title "Important message from router $[/system identity get name]";                              
:local message "Just writing to inform you that my temperature is $[/system health get temperature] degrees Celsius!" 
$pushover title=$title message=$message priority=1 sound="tugboat"
I used the urlEncode function written by few people in this thread.
 
cantanko
newbie
Posts: 39
Joined: Mon Apr 05, 2010 12:53 am

Re: PUSHOVER - ready MikroTik script to send messages

Wed Apr 03, 2019 9:12 pm

Thank you for this - it's proven to be a very helpful chunk of code!
 
danielcada
just joined
Posts: 3
Joined: Sat Jan 27, 2018 10:02 am

Re: PUSHOVER - ready MikroTik script to send messages

Tue Mar 03, 2020 7:16 pm

Works well on FW 6.45.5 including getting PUSHOVER from NETWATCH.

On FW 6.46.4 works only to send PUSHOVER manualy runing script, but doesn't work to send PUSHOVER from NETWATCH.

Any suggestion?
 
User avatar
dreamind
just joined
Posts: 10
Joined: Thu Apr 18, 2013 9:15 pm
Location: Germany
Contact:

Re: PUSHOVER - ready MikroTik script to send messages

Wed Apr 15, 2020 2:15 am

If you set keep-result=no on the fetch command in the script it works.

Also make sure you set the policy to something like policy=read,write,test on scripts that you want to run from netwatch.
 
Belikearu
just joined
Posts: 4
Joined: Mon Dec 30, 2019 5:53 am
Location: General Santos City, Philippines

Re: PUSHOVER - ready MikroTik script to send messages

Wed Apr 15, 2020 2:37 am

How do you exactly do this? pasting the script on the winbox terminal? and what about the API Keys?
Would you mind elaborating this one? thankyou
 
lacibsd
Frequent Visitor
Frequent Visitor
Posts: 60
Joined: Thu Mar 10, 2016 9:48 pm

Re: PUSHOVER - ready MikroTik script to send messages

Sun May 02, 2021 3:43 am

@Belikearu, In Winbox or webintraface you can place it under System->Scripts
@dasiu, awesome script, I was looking for a pushover integration
 
tmiklas
just joined
Posts: 9
Joined: Mon Apr 02, 2007 12:06 pm

Re: PUSHOVER - ready MikroTik script to send messages

Mon May 03, 2021 1:04 pm

Amazing - thank you!
 
Lilarcor
Frequent Visitor
Frequent Visitor
Posts: 56
Joined: Sun Oct 08, 2017 3:16 am

Re: PUSHOVER - ready MikroTik script to send messages

Fri May 21, 2021 9:06 am

I tried to use it in net watch, but it didn't work even I add keep-result=no in script
 
User avatar
anav
Forum Guru
Forum Guru
Posts: 21226
Joined: Sun Feb 18, 2018 11:28 pm
Location: Nova Scotia, Canada
Contact:

Re: PUSHOVER - ready MikroTik script to send messages

Sat May 22, 2021 4:33 am

I use telegram to send me messages from my router.
Is pushover easier/better to use??
 
Lilarcor
Frequent Visitor
Frequent Visitor
Posts: 56
Joined: Sun Oct 08, 2017 3:16 am

Re: PUSHOVER - ready MikroTik script to send messages

Thu Oct 14, 2021 8:35 am

$pushover message="xyz" does work in terminal, but it doesn't work in scripts

update:
add
:global pushover
and I can use $pushover now
 
RandyRiver88
just joined
Posts: 14
Joined: Fri May 15, 2020 7:28 pm

Re: PUSHOVER - ready MikroTik script to send messages

Thu May 26, 2022 11:52 pm

$pushover message="xyz" does work in terminal, but it doesn't work in scripts

update:
add
:global pushover
and I can use $pushover now
Hi,

Where did you fix this? I am able to send using terminal but not in from inside of scripts….

Can you tell me which line to changed?

Thanks.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12438
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: PUSHOVER - ready MikroTik script to send messages

Fri May 27, 2022 12:02 am

Simply READ carefully pervious post...
 
RandyRiver88
just joined
Posts: 14
Joined: Fri May 15, 2020 7:28 pm

Re: PUSHOVER - ready MikroTik script to send messages

Fri May 27, 2022 5:03 am

Simply READ carefully pervious post...
The post is not clear. It does not state where to add it.

The correct answer would have been:

Add :global pushover in the script you are calling $pushover in.

No need to be a dick.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12438
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: PUSHOVER - ready MikroTik script to send messages

Fri May 27, 2022 10:29 am

I was helpful, seen? Now you have read it carefully...
 
esj
just joined
Posts: 21
Joined: Fri Feb 17, 2023 5:54 pm

Re: PUSHOVER - ready MikroTik script to send messages

Mon Jul 01, 2024 3:41 pm

Thank you for the script.

Working perfectly. To test I run the script below

:global pushover
$pushover message="This is a simple Pushover message."
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4089
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: PUSHOVER - ready MikroTik script to send messages

Mon Jul 01, 2024 11:47 pm

Pushover also support JSON as an input. And in 5 years since the OP, Mikrotik added JSON support RouterOS scripting (7.13+).

While I'm sure the existing script is fine, I re-wrote to take an RouterOS array with ANY of the allowed by pushover's API. Since new [:serialize] will deal with types and escaping when converting the RouterOS array into JSON needed by pushover... the code gets dramatically simplier:

npushover code

:global npushover do={
    :local url "https://api.pushover.net/1/messages.json"
    :local headers "Content-Type: application/json"
    :local reqdata [:toarray ""]
    :if ([:typeof $1]="array") do={:set reqdata $1} else={
        :error "\$$0 requires an array of values to set, see https://pushover.net/api"
    }
    :local json [:serialize to=json $reqdata]
    :local resp [/tool/fetch url=$url http-data=$json http-header-field=$headers output=user as-value]
    :local respdata [:deserialize from=json ($resp->"data")]
    :if (($respdata->"status")=1) do={
        /log/debug "$0 $[:put "successfully sent request $($respdata->"request")"]"
    } else={
        /log/warning "$0 failed, got: $[:tostr $resp]"
        :error $resp
    }
}
To send a message using pushover using the $npushover <array> above, and using an array allows any of the attributes supports like url or priority:
$npushover ({ 
        user="u8xxxxxxxxxxx"
        token="acyqxxxxxxxxxxxxxxx"
        message="Perhaps some <b>HTML</b>"
        title="Test Message"
        html=1
        sound="magic"
        priority=0
        url="https://router.lan/rest/system/resource"
        "url_title"="/system/resources"
}) 
See https://pushover.net/api for possible/allowed values. No validation is provided, kinda by design. Errors should be output and logged, likely giving a clue as the right values. And stuff like "priority" has additional allowed attributes like "retry", so validation might block that (or get very complex).
 
davorjs
just joined
Posts: 12
Joined: Mon Oct 12, 2020 1:04 am

Re: PUSHOVER - ready MikroTik script to send messages

Sat Jul 06, 2024 4:28 am

Thank you very much Ammo, your script is very timely as I only discovered Pushover two days ago.

Much simpler than Telegram

Cheers Dave.
 
esj
just joined
Posts: 21
Joined: Fri Feb 17, 2023 5:54 pm

Re: PUSHOVER - ready MikroTik script to send messages

Sun Jul 07, 2024 7:50 pm

To send a message using pushover using the $npushover <array> above, and using an array allows any of the attributes supports like url or priority:
$npushover ({ 
        user="u8xxxxxxxxxxx"
        token="acyqxxxxxxxxxxxxxxx"
        message="Perhaps some <b>HTML</b>"
        title="Test Message"
        html=1
        sound="magic"
        priority=0
        url="https://router.lan/rest/system/resource"
        "url_title"="/system/resources"
}) 
See https://pushover.net/api for possible/allowed values. No validation is provided, kinda by design. Errors should be output and logged, likely giving a clue as the right values. And stuff like "priority" has additional allowed attributes like "retry", so validation might block that (or get very complex).
Thank you Amm0. one question. How can I add info from RouterOS in the message. e.g. interface/lte/monitor lte1

I've tried
:global npushover
$npushover ({ 
        user="private"
        token="private"
        message="Mikrotik SXT Rebooted <b>nPushover</b> $[interface/lte/monitor lte1 duration=2]"
        title="MikroTik SXTR"
        html=1
        sound="magic"
        priority=0
        url="https://192.168.x.1"
        "url_title"="MikroTik"
})
.. and it executes, but no info from the LTE is included in the message. Any pointers appreciated.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4089
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: PUSHOVER - ready MikroTik script to send messages

Tue Jul 09, 2024 4:02 am

:global npushover
$npushover ({ 
        user="private"
        token="private"
        message="Mikrotik SXT Rebooted <b>nPushover</b> $[interface/lte/monitor lte1 duration=2]"
        title="MikroTik SXTR"
        html=1
        sound="magic"
        priority=0
        url="https://192.168.x.1"
        "url_title"="MikroTik"
})
.. and it executes, but no info from the LTE is included in the message. Any pointers appreciated.

All of the commands with "monitor" are tricky, since it's like a "for loop"... So you need to remove "duration" and use "once" instead.
:global npushover
$npushover ({ 
        user="private"
        token="private"
        message="Mikrotik SXT Rebooted <b>nPushover</b> $[interface/lte/monitor lte1 once as-value]"
        title="MikroTik SXTR"
        html=1
        sound="magic"
        priority=0
        url="https://192.168.x.1"
        "url_title"="MikroTik"
})
The "once" cause the /interface/lte/monitor to only return one set of value to insert into the message. You could use a duration, but that requires collecting the LTE monitor data into a new variable, and then using the new variable in the message.
Last edited by Amm0 on Tue Jul 09, 2024 10:37 pm, edited 1 time in total.
 
esj
just joined
Posts: 21
Joined: Fri Feb 17, 2023 5:54 pm

Re: PUSHOVER - ready MikroTik script to send messages

Tue Jul 09, 2024 12:57 pm


All of the commands with "monitor" are tricky, since it's like a "for loop"... So you need to remove "duration" and use "once" instead.
:global npushover
$npushover ({ 
        user="private"
        token="private"
        message="Mikrotik SXT Rebooted <b>nPushover</b> $[interface/lte/monitor lte1 once]"
        title="MikroTik SXTR"
        html=1
        sound="magic"
        priority=0
        url="https://192.168.x.1"
        "url_title"="MikroTik"
})
The "once" cause the /interface/lte/monitor to only return one set of value to insert into the message. You could use a duration, but that requires collecting the LTE monitor data into a new variable, and then using the new variable in the message.
Thank you for this. Either version of the code $[interface/lte/monitor lte1 once] doesn't add data/content at all to the message (not even the code is included). It this the correct way to pull MikroTik data into the message that is sent?

The received message is as attached (sorry can't seem to attach message)

Image
https://imgur.com/xlI5yVq

UPDATE: This works though

message="Mikrotik SXT Rebooted <b>nPushover</b> $[/system clock get time] $[/system clock get time] $[/system identity get name]"
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4089
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: PUSHOVER - ready MikroTik script to send messages

Tue Jul 09, 2024 10:40 pm


Thank you for this. Either version of the code $[interface/lte/monitor lte1 once] doesn't add data/content at all to the message (not even the code is included). It this the correct way to pull MikroTik data into the message that is sent?

The received message is as attached (sorry can't seem to attach message)
My bad. I forgot to add the "as-value" — that's critical to returning the data for the string. So it should be "$[/interface/lte/monitor lte1 once as-value]".
 
esj
just joined
Posts: 21
Joined: Fri Feb 17, 2023 5:54 pm

Re: PUSHOVER - ready MikroTik script to send messages

Sat Jul 13, 2024 8:12 pm

My bad. I forgot to add the "as-value" — that's critical to returning the data for the string. So it should be "$[/interface/lte/monitor lte1 once as-value]".
Hey Ammo, many thanks for changing this, but unfortunately this doesn't work and I get no message at all. The clock and system names work, as I said in my previous examples. These are 1 line results, though.
e.g.
message="Mikrotik SXT Rebooted <b>nPushover</b> $[/system clock get time] $[/system identity get name]"
I'm wondering if it is because the output of LTE monitor has many line of info ... that it is not escaping or processing the lines properly :?

Does this code work on your device pushover? or is there a multi-line output we could check?
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4089
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: PUSHOVER - ready MikroTik script to send messages

Sat Jul 13, 2024 9:42 pm

It's not the multi-line output per se. It the data type returned by "monitor" is a RouterOS array type. And one rule (which I forgot in my quick example of LTE monitor) is array cannot be interpolated, so a ":tostr" is needed. This will get rather ugly output, but should work:
$npushover ({ 
        user="private"
        token="private"
        message="Mikrotik SXT Rebooted <b>nPushover</b><br><pre> $[:tostr [/interface/lte/monitor lte1 once as-value]]</pre>"
        title="MikroTik SXTR"
        html=1
        sound="magic"
        priority=0
        url="https://192.168.x.1"
        "url_title"="MikroTik"
})

Alternatively, you can collect the LTE monitor BEFORE build the PUSHOVER message, and use invidudual data items from LTE monitor in the string, like this:
{
:global npushover
:local mdata [/interface/lte/monitor lte1 once as-value]
$npushover ({ 
        user="private"
        token="private"
        message="Mikrotik SXT Rebooted <b>nPushover</b><br><pre>$($mdata->"primary-band") sinr: $($mdata->"sinr") rsrq: $($mdata->"rsrq") rsrp: $($mdata->"rsrp")</pre>"
        title="MikroTik SXTR"
        html=1
        sound="magic"
        priority=0
        url="https://192.168.x.1"
        "url_title"="MikroTik"
})
}
 
esj
just joined
Posts: 21
Joined: Fri Feb 17, 2023 5:54 pm

Re: PUSHOVER - ready MikroTik script to send messages

Sun Jul 14, 2024 2:49 am

Ammo. You really are a very clever person. Both work for me :D
 
tores
just joined
Posts: 3
Joined: Sun Oct 20, 2024 9:45 pm

Re: PUSHOVER - ready MikroTik script to send messages

Sun Oct 20, 2024 9:57 pm

Hi,
I have a question, not so much about pushover.

I use TheDude and pushover - and it worked perfectly up until version 7.6 - in notifications it was enough to call the function "$pushover message="Service [Probe.Name] on [Device.Name] is now [Service.Status]";" just like other scripts.

Unfortunately, in version 7.15 - 7.16 calling $pushover does not work - scripts are called but not pushover - maybe you have an idea to fix it.

Best regards.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4089
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: PUSHOVER - ready MikroTik script to send messages

Mon Oct 21, 2024 4:52 am

Hi,
I have a question, not so much about pushover.

I use TheDude and pushover - and it worked perfectly up until version 7.6 - in notifications it was enough to call the function "$pushover message="Service [Probe.Name] on [Device.Name] is now [Service.Status]";" just like other scripts.

Unfortunately, in version 7.15 - 7.16 calling $pushover does not work - scripts are called but not pushover - maybe you have an idea to fix it.

Best regards.
I wrote for 7.15 that a bit cleaner, see npushover above.

But your issue may be permissions on RouterOS side.... You likely have put the script in /system/script with "Do not require permissions" with "policy" and "test" rights to run /tool/fetch. I believe there should be some log about it but perhaps not.
policy - policy that grants user management rights. Should be used together with the write policy. Allows also to see global variables created by other users (requires also 'test' policy).
test - policy that grants rights to run ping, traceroute, bandwidth-test, wireless scan, snooper, fetch, email and other test commands
(from https://help.mikrotik.com/docs/spaces/R ... 78504/User), which is linked from Scripting, referring to "etc" to mean Dude)
Note: Only scripts (including schedulers, netwatch, etc) with equal or higher permission rights can execute other scripts.
(from: https://help.mikrotik.com/docs/spaces/R ... repository)

But permissions is what's changed from 7.6.
 
tores
just joined
Posts: 3
Joined: Sun Oct 20, 2024 9:45 pm

Re: PUSHOVER - ready MikroTik script to send messages

Mon Oct 21, 2024 2:41 pm



But your issue may be permissions on RouterOS side.... You likely have put the script in /system/script with "Do not require permissions" with "policy" and "test" rights to run /tool/fetch. I believe there should be some log about it but perhaps not.

It's not a permissions issue.
On RouterOS in the terminal or in other scripts the $pushover function works - and the firmware version /7.6, 7.15, 7.16/ doesn't matter, only calling the $pushover function/script in TheDude in version 7.15, 7.16 doesn't work.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4089
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: PUSHOVER - ready MikroTik script to send messages

Mon Oct 21, 2024 6:04 pm

It's not a permissions issue.
Well I do not use notification with Dude, so IDK. But I'm not sure your Terminal test is exactly same user context as Dude running a notification. I cannot say for sure, but when Dude run calls RouterOS... that's more similar to netwatch (which uses a *sys user), than Terminal from a logged in admin user.

Best you can do is add debug logging and perhaps something shows up when Dude call pushover. Because if it works in Terminal it ain't the script code itself, thus the theory on permissions - since there are not a lot of other options...

Does calling a more basic script work from Dude? i.e. one that does not use /tool/fetch or global variables...
 
tores
just joined
Posts: 3
Joined: Sun Oct 20, 2024 9:45 pm

Re: PUSHOVER - ready MikroTik script to send messages

Mon Oct 21, 2024 7:52 pm

It's not a permissions issue.
Well I do not use notification with Dude, so IDK. But I'm not sure your Terminal test is exactly same user context as Dude running a notification. I cannot say for sure, but when Dude run calls RouterOS... that's more similar to netwatch (which uses a *sys user), than Terminal from a logged in admin user.

Best you can do is add debug logging and perhaps something shows up when Dude call pushover. Because if it works in Terminal it ain't the script code itself, thus the theory on permissions - since there are not a lot of other options...

Does calling a more basic script work from Dude? i.e. one that does not use /tool/fetch or global variables...

In this form it works and that's enough for me. Thank you.

/tool fetch mode=https url="https://api.pushover.net/1/messages.json" http-method=post http-data="token=axxxxxxx&user=uxxxxx1&message=Service [Probe.Name] on [Device.Name] is now [Service.Status]" output=none;
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4089
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: PUSHOVER - ready MikroTik script to send messages

Mon Oct 21, 2024 7:57 pm


Does calling a more basic script work from Dude? i.e. one that does not use /tool/fetch or global variables...

In this form it works and that's enough for me. Thank you.
/tool fetch mode=https url="https://api.pushover.net/1/messages.json" http-method=post http-data="token=axxxxxxx&user=uxxxxx1&message=Service [Probe.Name] on [Device.Name] is now [Service.Status]" output=none;
No :global variables maybe... glad it works.

Who is online

Users browsing this forum: No registered users and 11 guests