diff --git a/index.js b/index.js index 3256f91151193dd2f9a79369c22875ac11693749..490fc71c80ad1eff67d227e27469c8c036ee3507 100644 --- a/index.js +++ b/index.js @@ -41,7 +41,7 @@ app.post('/post', function(req, res){ pathname: 'http://api.wunderground.com/api/' + apikey + '/conditions/q/' + req.body.text + format, }); - console.log(parsed_url); + console.log( req.body.text ); request(parsed_url, function (error, response, body) { if ( error || 200 !== response.statusCode ) { @@ -55,19 +55,38 @@ app.post('/post', function(req, res){ var icon_url = conditionData.current_observation.icon_url; var location = conditionData.current_observation.display_location.full; - var body = { - response_type: "in_channel", - "attachments": [ - { - "text": "Location: " + location + "\n" - + "Temperature: " + temperature + "\n" - + "Condition: " + weatherCondition, - "image_url": icon_url, - } - ] - }; - - res.send( body ); + parsed_url = url.format({ + pathname: 'http://api.wunderground.com/api/' + apikey + '/forecast/q/' + req.body.text + format, + }); + + request( parsed_url, function( error, response, body ) { + // Basic response for the conditions data already obtained + var responseBody = { + "response_type": "in_channel", + "icon": icon_url, + "text": "Forecast", + "attachments": [ + { + "text": "Location: " + location + "\n" + + "Temperature: " + temperature + "\n" + + "Condition: " + weatherCondition, + "image_url": icon_url, + } + ] + }; + + // If forecast wasn't available, return just the conditions + if ( error || 200 !== response.statusCode ) { + responseBody.attachments[1] = { "text":"Could not retrieve forecast" }; + + return res.send( responseBody ); + } + + // Build forecast into a table + + res.send( responseBody ); + + }); }); });