-
-
Save darrenjrobinson/250720697d175c42802c42e6e50162e9 to your computer and use it in GitHub Desktop.
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
// 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