diff --git a/index.js b/index.js index 40938f334bcb83f3f5ed501daf68de9204788d8b..3256f91151193dd2f9a79369c22875ac11693749 100644 --- a/index.js +++ b/index.js @@ -44,27 +44,30 @@ app.post('/post', function(req, res){ console.log(parsed_url); request(parsed_url, function (error, response, body) { - if (!error && response.statusCode == 200) { - var data = JSON.parse(body); - var temperature = data.current_observation.temperature_string; - var weatherCondition = data.current_observation.weather; - var icon_url = data.current_observation.icon_url; - var location = data.current_observation.display_location.full; + if ( error || 200 !== response.statusCode ) { + return res.sendStatus( 500 ); + } - var body = { - response_type: "in_channel", - "attachments": [ - { - "text": "Location: " + location + "\n" - + "Temperature: " + temperature + "\n" - + "Condition: " + weatherCondition, - "image_url": icon_url, - } - ] - }; + // Get current conditions first + var conditionData = JSON.parse( body ); + var temperature = conditionData.current_observation.temperature_string; + var weatherCondition = conditionData.current_observation.weather; + var icon_url = conditionData.current_observation.icon_url; + var location = conditionData.current_observation.display_location.full; - res.send(body); - } + var body = { + response_type: "in_channel", + "attachments": [ + { + "text": "Location: " + location + "\n" + + "Temperature: " + temperature + "\n" + + "Condition: " + weatherCondition, + "image_url": icon_url, + } + ] + }; + + res.send( body ); }); });