[Solved] Pushover Notification with Image

I've been trying to find a way to send Pushover notifications from Hubitat with an image attachment (Useful for sending camera snapshots for example), but couldn't find a working solution since Hubitat's httpPost doesn't natively support multipart forms with binary data.

The key was to build the entire multipart POST request as a byte array, rather than trying to combine string and binary parts.

Hopefully the code below will save some time for anyone trying to accomplish this.

def imageData

log.debug "Getting Notification Image"

//Get Notification Image
httpGet([
  uri: "[Notification Image URL]",
contentType: "*/*",
//Add HTTP Auth Header (If required)
headers: ["authorization": "Basic [Base64 Encoded Credentials]" ],
textParser: false])
{ response ->
  imageData = response.data
  log.debug "Notification Image Received (${imageData.available()})"
   
}

def bSize = imageData.available()
byte[] imageArr = new byte[bSize]
imageData.read(imageArr, 0, bSize)

//Pushover API Credentials
def apiUser="[Pushover API User]"
def apiToken="[Pushover API Token]"

//Top Part of the POST request Body
def postBodyTop = """----rjtvk266t3df6jkpb4\r\nContent-Disposition: form-data; name="user"\r\n\r\n$apiUser\r\n----rjtvk266t3df6jkpb4\r\nContent-Disposition: form-data; name="token"\r\n\r\n$apiToken\r\n----rjtvk266t3df6jkpb4\r\nContent-Disposition: form-data; name="title"\r\n\r\nNotification Title\r\n----rjtvk266t3df6jkpb4\r\nContent-Disposition: form-data; name="message"\r\n\r\nNotification Message\r\n----rjtvk266t3df6jkpb4\r\nContent-Disposition: form-data; name="attachment"; filename="image.jpg"\r\nContent-Type: image/jpeg\r\n\r\n"""

//Bottom Part of the POST request Body
def postBodyBottom = """\r\n----rjtvk266t3df6jkpb4--"""


byte[] postBodyTopArr = postBodyTop.getBytes("UTF-8")
byte[] postBodyBottomArr = postBodyBottom.getBytes("UTF-8")

//Combine different parts of the POST request body
ByteArrayOutputStream postBodyOutputStream = new ByteArrayOutputStream();

postBodyOutputStream.write(postBodyTopArr);
postBodyOutputStream.write(imageArr);
postBodyOutputStream.write(postBodyBottomArr);

byte[] postBody = postBodyOutputStream.toByteArray();

//Build HTTP Request Parameters
def params = [
	
	requestContentType: "application/octet-stream",
	headers: ["content-type": "multipart/form-data; boundary=--rjtvk266t3df6jkpb4"],
	uri: "https://api.pushover.net/1/messages.json",
	body: postBody
]

log.debug "Sending Notification Request"
httpPost(params){response ->
	if(response.status != 200) {
		log.error "HTTP Request error ${response.status}."
	}
}

log.debug "Notification Request Complete"
12 Likes

Awesome. I was trying to do the same thing and thought it wasnt possible and gave up. But I will definately try this code out tomorrow. Thanks for putting this together.

Works great. Now got images with my notificaitons.

Just in case you didn't know. I also added other options available to the api in so you can control things such as the priority ect.

This is the modified code (basically copied a line over and over and changed hte name/variable. Never got url/url_title working so going to do that a different way.

        def postBodyTop = """----rjtvk266t3df6jkpb4\r\n
Content-Disposition: form-data; name="user"\r\n\r\n${user}\r\n----rjtvk266t3df6jkpb4\r\n
Content-Disposition: form-data; name="token"\r\n\r\n${token}\r\n----rjtvk266t3df6jkpb4\r\n
Content-Disposition: form-data; name="title"\r\n\r\n${title}\r\n----rjtvk266t3df6jkpb4\r\n
Content-Disposition: form-data; name="message"\r\n\r\n${message}${(url?" "+url:"")}\r\n----rjtvk266t3df6jkpb4\r\n
Content-Disposition: form-data; name="device"\r\n\r\n${device}\r\n----rjtvk266t3df6jkpb4\r\n
Content-Disposition: form-data; name="sound"\r\n\r\n${sound}\r\n----rjtvk266t3df6jkpb4\r\n
Content-Disposition: form-data; name="priority"\r\n\r\n${priority}\r\n----rjtvk266t3df6jkpb4\r\n
Content-Disposition: form-data; name="retry"\r\n\r\n${retry}\r\n----rjtvk266t3df6jkpb4\r\n
Content-Disposition: form-data; name="expire"\r\n\r\n${expire}\r\n----rjtvk266t3df6jkpb4\r\n
Content-Disposition: form-data; name="timestamp"\r\n\r\n${timestamp}\r\n----rjtvk266t3df6jkpb4\r\n
Content-Disposition: form-data; name="attachment"; filename="image.jpg"\r\nContent-Type: image/jpeg\r\n\r\n"""
2 Likes

Excellent! This provided me with the final piece that needed to be able to read and write image files to the HE File Manager.

Thanks!

2 Likes

Geez, it's a never ending caldron of creativity in this forum.

I'm accomplishing the image capture & send via a POST to an Axis camera after an HE trigger. But I'm really curious about what's being done via this Pushover Notification path.

Could someone elaborate in a brief process outline what's happening here with respect to triggering, source, image transfer, and ultimate notification packaging?

Thanks

This is just a small snippit of code to include in a bigger app/driver.

All this code is doing is demonstrating what is required to download an image, format it as required and then send the information off to pushover.

On its own this code is not very useful as you cant just save it to a new driver and run it as its missing a lot of the other code required, but if you are doing something like writing your own pushover driver its very valuable as now you can include images with the notification.

Got it...thanks

so people ARE pulling images into the HE platform from a camera, to then send via the pushover notification.

Yup. That is what this bit of code solved for us. I personally pull the image from BlueIris. As long as your camera has an http call to get the image it should be possible with this code.

2 Likes

You know you've done something impressive when you've beaten @thebearmay to the addition of a previously unavailable feature with some custom code :grin:.

5 Likes

So this code needs to added into another driver? I'm a little confused about how to implement it. I'd love to get images in my notifications!

Anyone have any guidance on this?

2 Likes

Thanks for your awesome example, @Younes. I'm going to use this same approach to push Pushover image notifications from a couple of my camera integrations (UniFi Protect, Amcrest/Dahua). Great stuff!

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.