Skip to content

Instantly share code, notes, and snippets.

@cmsunu28
Created June 21, 2017 00:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmsunu28/76ad2263c567817c2c6bcb4ce0b5f0c3 to your computer and use it in GitHub Desktop.
Save cmsunu28/76ad2263c567817c2c6bcb4ce0b5f0c3 to your computer and use it in GitHub Desktop.
module['exports'] = function echoHttp (hook) {
var https = require('https');
var querystring = require('querystring');
var env = hook.env;
var token = env.particle_access_token;
var deviceID = env.particle_doorbell_device_id;
// don't forget to define these secret variables at hook.io/env
var request = require('request');
var nickname = hook.params["name"];
var location = hook.params["loc"];
console.log('incoming: ',nickname);
console.log('location: ',location);
var func=nickname;
var arg=location;
if(deviceID != undefined && arg != undefined && func != undefined){
var path = "https://api.particle.io/v1/devices/" + deviceID + "/" + func
var data = querystring.stringify({
args: arg,
access_token: token
});
var options = {
hostname: 'api.particle.io',
port: 443,
path: path,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
};
var req = https.request(options, function(res) {
console.log('statusCode: ', res.statusCode);
console.log('headers: ', res.headers);
res.setEncoding('utf8');
res.on('data', function(d) {
});
res.on('end', function () {
// console.log(result);
hook.res.end(returnValue);
});
});
req.on('error', function(error){
hook.res.end("Error: " + returnValue);
});
// console.log(data);
req.write(data);
req.end();
}
else{
hook.res.end(returnValue);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment