-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-weather.js
32 lines (28 loc) · 1.26 KB
/
get-weather.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Webhook for hook.io for DIY Weather Tracker
// CC BY-SA Teddy Lowe June 2016 - Lunchbox Electronics
// Based on code from monkbroc what to wear https://github.com/monkbroc/what-to-wear
function getForecast(url) {
const got = require("got"); // got makes it easy to make HTTP requests
return got(url)
.then(function (response) {
return JSON.parse(response.body); // parses out the body of the JSON
});
}
function hookBack(hook, condition){
hook.res.end(condition); // This forces the hook to end and respond with the condition
}
module['exports'] = function getWeather (hook) {
var location = hook.env.MY_LOCATION; // This pulls lat/long location from the environment variables
var api_key = hook.env.FORECASTIO_API_KEY; // This pulls the api key for forecast.io from the env vars
var url = "https://api.forecast.io/forecast/" + api_key + "/" + location; // This generates the URL for the call
getForecast(url)
.then(function (weather) {
var condition = weather.currently.icon; // All we want is the icon - a machine readable version of the current weather
console.log(condition);
hookBack(hook, condition);
})
.catch(function (error) {
console.error("Error getting forecast: " + error);
hook.res.end("error");
});
};