Skip to content
Snippets Groups Projects
Commit f4bbb865 authored by ryadav88's avatar ryadav88
Browse files

index

parent 8699f0e8
No related branches found
No related tags found
No related merge requests found
......@@ -4,32 +4,36 @@ var url = require('url');
var request = require('request');
var format = ".json";
var apikey = process.env.WU_ACCESS
var apikey = process.env.WU_ACCESS //WU API key; will be set in Heroku
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//use port is set in the environment variable, or 9001 if it isn’t set.
app.set('port', (process.env.PORT || 9001));
//for testing that the app is running
app.get('/', function(req, res){
res.send('It works!');
res.send('Running!!');
});
//app.post is triggered when a POST request is sent to the URL ‘/post’
app.post('/post', function(req, res){
//take a message from Slack slash command
var query = req.body.text
var parsed_url = url.format({
pathname: 'http://api.wunderground.com/api/' + apikey + '/conditions/q/' + req.body.text + format,
});
console.log(parsed_url);
request(parsed_url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var data = JSON.parse(body);
var first_url = data.current_observation.temperature_string;
var weatherC = data.current_observation.weather
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
......@@ -37,17 +41,19 @@ app.post('/post', function(req, res){
response_type: "in_channel",
"attachments": [
{
"text": "Location: " + location + "\n" + "Temperature: " + first_url + "\n" + "Condition: " + weatherC,
"image_url": icon_url,
"text": "Location: " + location + "\n"
+ "Temperature: " + temperature + "\n"
+ "Condition: " + weatherCondition,
"image_url": icon_url,
}
]
};
res.send(body);
}
});
});
//tells Node which port to listen on
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment