Skip to content

Instantly share code, notes, and snippets.

@darrenjrobinson
Last active September 21, 2017 00:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darrenjrobinson/250720697d175c42802c42e6e50162e9 to your computer and use it in GitHub Desktop.
Save darrenjrobinson/250720697d175c42802c42e6e50162e9 to your computer and use it in GitHub Desktop.
// Sample Google Home Custom Action API Call
// Darren J Robinson
'use strict';
const http = require('https');
const host = 'us.wio.seeed.io';
const wwoApiKey = '8443abbe61e5daaccccac3456789';
exports.insideTemp = (req, res) => {
// Call the WIO Link API
callWeatherApi().then((output) => {
// Return the results from the WIO Link API to API.AI
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({ 'speech': output, 'displayText': output }));
}).catch((error) => {
// If there is an error let the user know
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({ 'speech': error, 'displayText': error }));
});
};
function callWeatherApi () {
return new Promise((resolve, reject) => {
// Create the path for the HTTP request to get the weather
// https://us.wio.seeed.io/v1/node/GroveTempHumD1/temperature?access_token=8443abbe61e5daaccccac3456789
let path = '/v1/node/GroveTempHumD1/temperature?access_token=' + wwoApiKey;
console.log('API Request: ' + host + path);
// Make the HTTP request to get the weather
http.get({host: host, path: path}, (res) => {
let body = ''; // var to store the response chunks
res.on('data', (d) => { body += d; }); // store each response chunk
res.on('end', () => {
// After all the data has been received parse the JSON for desired data
let response = JSON.parse(body);
let temperature = response['celsius_degree'];
// Create response
let output = `Yo bro, the pilot brewery inside temperature is currently ${temperature} degrees celsius.`;
// Resolve the promise with the output text
console.log(output);
resolve(output);
});
res.on('error', (error) => {
reject(error);
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment