Last active
March 2, 2018 06:06
-
-
Save imbrianj/af09af8cc06781e4f410062b38514c95 to your computer and use it in GitHub Desktop.
Half-working library-less Pushover function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To use: | |
* require('pushover').send({token: 'xxxx', userKey: 'xxxx', text: 'message', payload: myRawImage}); | |
*/ | |
module.exports = (function () { | |
'use strict'; | |
return { | |
/** | |
* Prepare a request for command execution. | |
*/ | |
postPrepare : function (config) { | |
return { | |
host : 'api.pushover.net', | |
port : 443, | |
path : '/1/messages.json', | |
method : 'POST', | |
headers : { | |
'Accept' : 'application/json', | |
'Content-Type' : 'multipart/form-data; boundary=' + config.boundary, | |
'Content-Length' : Buffer.byteLength(config.postRequest) | |
} | |
}; | |
}, | |
/** | |
* Prepare the POST data to be sent. | |
*/ | |
postData : function (pushover) { | |
var data = '--' + pushover.boundary + '\r\n' | |
+ 'Content-Disposition: form-data; name="user"\r\n\r\n' | |
+ pushover.userKey + '\r\n' | |
+ '--' + pushover.boundary + '\r\n' | |
+ 'Content-Disposition: form-data; name="token"\r\n\r\n' | |
+ pushover.token + '\r\n' | |
+ '--' + pushover.boundary + '\r\n' | |
+ 'Content-Disposition: form-data; name="message"\r\n\r\n' | |
+ pushover.text + '\r\n'; | |
if (pushover.payload) { | |
data += '--' + pushover.boundary + '\r\n' | |
+ 'Content-Disposition: form-data; name="attachment"; filename="image.jpg"\r\n' | |
+ 'Content-Type: image/jpeg\r\n\r\n' | |
+ pushover.payload + '\r\n'; | |
} | |
data += '\r\n--' + pushover.boundary + '--'; | |
return data; | |
}, | |
send : function (config) { | |
var https = require('https'), | |
pushover = {}, | |
request; | |
pushover.token = config.token; | |
pushover.userKey = config.userKey; | |
pushover.text = config.text || ''; | |
pushover.payload = config.payload || null; | |
pushover.boundary = Math.random().toString(16); | |
pushover.postRequest = this.postData(pushover); | |
request = https.request(this.postPrepare(pushover), function (response) { | |
response.setEncoding('utf8'); | |
response.once('data', function (response) { | |
console.log(response); | |
}); | |
}); | |
request.once('error', function (err) { | |
console.log(err); | |
}); | |
request.end(pushover.postRequest); | |
} | |
}; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment