Pushover API

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
User avatar
Grovkillen
Automation Wizard
Posts: 665
Joined: Fri Aug 10, 2012 2:38 pm
Location: Hudiksvall, Sweden
Contact:

Pushover API

Post by Grovkillen » Fri Jan 18, 2019 7:23 am

Code: Select all

//token needs to be generated by adding a application to your user
//user key is automatically generated to your user, but if you want to push info to multiple
//users you need to make a group key
Let>pushover_token=YOUR_APPLICATION_TOKEN
Let>pushover_user_key=YOUR_USER_KEY

Let>pushover_api_url=https://api.pushover.net/1/messages.json

Let>pushover_title=title text
Let>pushover_message=test message body
Let>pushover_device=your_device_name
Let>pushover_priority=1

//You can add all keys found here: https://pushover.net/api
Let>DISABLE_COMPLEX_EXPRESSIONS=1
Let>pushover_json={ "token":"%pushover_token%"%COMMA%
Concat>pushover_json,"user":"%pushover_user_key%"%COMMA%
Concat>pushover_json,"priority":"%pushover_priority%"%COMMA%
Concat>pushover_json,"device":"%pushover_device%"%COMMA%
Concat>pushover_json,"title":"%pushover_title%"%COMMA%
Concat>pushover_json,"message":"%pushover_message%" }
Let>DISABLE_COMPLEX_EXPRESSIONS=0

Let>HTTP_TIMEOUT=5
Let>HTTP_REDIRECTS=1
Let>HTTP_SSL=1
Let>HTTP_POSTJSON=1
HTTPRequest>pushover_api_url,,POST,pushover_json,api_response
//MDL>api_response
Let>ME=%Script%

Running: 15.0.18
version history

NickD
Pro Scripter
Posts: 56
Joined: Fri Sep 23, 2016 2:17 pm

Re: Pushover API

Post by NickD » Mon Jan 13, 2020 4:23 pm

Thanks for posting this Grovekillen, you saved me a job :D
It looks like the API has changed slightly since you posted, but I managed to get it working with a few minor tweaks. Updated code below:

Code: Select all

//token needs to be generated by adding a application to your user
//user key is automatically generated to your user, but if you want to push info to multiple
//users you need to make a group key

Let>equals={("=")}
Let>and={("&")}
Let>plus={("+")}

Let>pushover_token=YOUR_APPLICATION_TOKEN
Let>pushover_user_key=YOUR_USER_KEY
Let>pushover_api_url=https://api.pushover.net/1/messages.json
Let>pushover_title=My Test Title
Let>pushover_message=My test message body
StringReplace>pushover_title,SPACE,%plus%,pushover_title
StringReplace>pushover_message,SPACE,%plus%,pushover_message
Let>pushover_device=your_device_name
Let>pushover_priority=1
//You can add all keys found here: https://pushover.net/api
Let>DISABLE_COMPLEX_EXPRESSIONS=1
Let>pushover_json=token%equals%%pushover_token%%and%
Concat>pushover_json,user%equals%%pushover_user_key%%and%
Concat>pushover_json,priority%equals%%pushover_priority%%and%
Concat>pushover_json,device%equals%%pushover_device%%and%
Concat>pushover_json,title%equals%%pushover_title%%and%
Concat>pushover_json,message%equals%%pushover_message%
Let>DISABLE_COMPLEX_EXPRESSIONS=0
Let>HTTP_TIMEOUT=5
Let>HTTP_REDIRECTS=1
Let>HTTP_SSL=1
Let>HTTP_POSTJSON=1
HTTPRequest>pushover_api_url,,POST,pushover_json,api_response
//MDL>api_response

User avatar
Grovkillen
Automation Wizard
Posts: 665
Joined: Fri Aug 10, 2012 2:38 pm
Location: Hudiksvall, Sweden
Contact:

Re: Pushover API

Post by Grovkillen » Tue Jan 14, 2020 9:46 pm

Thanks for the feedback! 👍
Let>ME=%Script%

Running: 15.0.18
version history

mightycpa
Automation Wizard
Posts: 335
Joined: Mon Jan 12, 2004 4:07 pm
Location: Vienna, VA

Re: Pushover API

Post by mightycpa » Wed Aug 12, 2020 9:52 pm

Hi,

I wonder if you guys might help me out. Thanks for the script work, by the way.

I've copied the substance of your script. I added a MessageModal that displays the JSON request string. I am not posting to pushover_api, but I am posting to a similar service.

I get back a 400 error code using Macro Scheduler. But when I test the exact JSON string created by the script at https://reqbin.com/req/4rwevrqh/post-json-string I get a valid result. So the problem would appear to be in the HTTPRequest.

My ssl libraries, while old, are installed. I do notice that you have two variables that I don't:

HTTP_POSTJSON
HTTP_REDIRECTS

Is this a MS version problem I'm having?

EDIT: YEP. That's what it is.
"A facility for quotation covers the absence of original thought." - Lord Peter Wimsey

User avatar
Marcus Tettmar
Site Admin
Posts: 7335
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Pushover API

Post by Marcus Tettmar » Thu Aug 13, 2020 11:41 am

400 means bad request. Which suggests you are sending data in the wrong format. Could be the format of your post body is wrong and/or you need to set some headers.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

mightycpa
Automation Wizard
Posts: 335
Joined: Mon Jan 12, 2004 4:07 pm
Location: Vienna, VA

Re: Pushover API

Post by mightycpa » Thu Aug 13, 2020 3:42 pm

Hi Marcus, long time.

No, it's not the format. I proved that when I did this part:
But when I test the exact JSON string created by the script at https://reqbin.com/req/4rwevrqh/post-json-string I get a valid result.
That website is a great tool, btw.

Nope, looks like I'm going to have to get off my wallet.
"A facility for quotation covers the absence of original thought." - Lord Peter Wimsey

User avatar
Marcus Tettmar
Site Admin
Posts: 7335
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Pushover API

Post by Marcus Tettmar » Mon Aug 17, 2020 4:08 pm

I didn't necessarily mean the JSON is wrong. I'm saying 400 means 'bad request'. So since I'm not able to see the docs, let's look at some reasons why a request might be 'bad'.

If it expects JSON and your JSON is 'correct' and it still says 'bad request' then maybe it thinks you're sending something else. It could be that you haven't set the correct headers to say 'I'm sending json' for example. You will need the HTTP_POSTJSON option set if it expects raw JSON in the body. You may need other headers. It's really hard to help without seeing the spec of the API you are dealing with.

If you send JSON without setting the right headers the server may not SEE it as JSON.

To have any chance of helping further I'd need to see the docs for this API and be able to try it. What do the docs say? Do they give you a curl or Python example? Maybe a postman or swagger definition?
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

mightycpa
Automation Wizard
Posts: 335
Joined: Mon Jan 12, 2004 4:07 pm
Location: Vienna, VA

Re: Pushover API

Post by mightycpa » Mon Aug 17, 2020 4:57 pm

Thanks Marcus. I clearly haven't been clear.
You will need the HTTP_POSTJSON option set if it expects raw JSON in the body.
Exactly. But I didn't know about that option when I first posted. After I noticed that line in the code, I looked at when this feature was introduced. My installed version of MS was pre-option, and I didn't have it. The solution was to purchase a newer version, which I did last week. I haven't tried it yet, but I expect to do it over the next couple of days and I also expect that everything will be ok. I don't need any other headers, just the JSON.
"A facility for quotation covers the absence of original thought." - Lord Peter Wimsey

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts